Click here to Skip to main content
16,004,479 members
Home / Discussions / C#
   

C#

 
GeneralRe: advice ? Pin
jtmtv182-Feb-03 15:24
jtmtv182-Feb-03 15:24 
GeneralProblem with threading Pin
leppie2-Feb-03 12:37
leppie2-Feb-03 12:37 
GeneralRe: Problem with threading Pin
Philip Fitzsimons3-Feb-03 2:24
Philip Fitzsimons3-Feb-03 2:24 
GeneralRe: Problem with threading Pin
leppie3-Feb-03 7:28
leppie3-Feb-03 7:28 
Generalconversion from long[] to byte[] Pin
Michal Januszczyk2-Feb-03 11:02
sussMichal Januszczyk2-Feb-03 11:02 
GeneralRe: conversion from long[] to byte[] Pin
David Stone2-Feb-03 12:06
sitebuilderDavid Stone2-Feb-03 12:06 
GeneralRe: conversion from long[] to byte[] Pin
Member 2540883-Feb-03 1:22
Member 2540883-Feb-03 1:22 
GeneralRe: conversion from long[] to byte[] Pin
Richard Deeming3-Feb-03 3:00
mveRichard Deeming3-Feb-03 3:00 
You can copy the long[] to a byte[] and back using the static methods of the Buffer class, but you will end up with two copies of the data in memory.

As an alternative, you could use a pseudo-union to get two views of the same data. You need to be very careful when you access the arrays, since the length of one array will be overwritten with the length of the other:
using System;
using System.Runtime.InteropServices;
 
[StructLayout(LayoutKind.Explicit)]
public struct IntArray
{
    public const int ElementSize = 4;
    
    [FieldOffset(0)]
    private int _size;
    
    // NB: Be very careful when accessing this array
    // The Length property will be 4 times the actual
    // length!
    [FieldOffset(4), MarshalAs(UnmanagedType.ByValArray)]
    public readonly int[] intArray;
    
    [FieldOffset(4), MarshalAs(UnmanagedType.ByValArray)]
    public readonly byte[] byteArray;
    
    public IntArray(int size)
    {
        _size = size;
        
        // NB: The order is critical here.
        // The size of the int[] array will
        // be overwritten with the size of
        // the byte[] array. If you reverse
        // these lines, you will only be
        // able to access 1/4 of the array.
        intArray = new int[size];
        byteArray = new byte[size * ElementSize];
    }
    
    /// <summary>
    /// The actual size of the intArray.
    /// </summary>
    public int IntSize
    {
        get { return _size; }
    }
    
    /// <summary>
    /// The actual size of the byteArray.
    /// </summary>
    public int ByteSize
    {
        get { return ElementSize * _size; }
    }
    
    /// <summary>
    /// Returns a copy of the byteArray.
    /// </summary>
    public byte[] GetSafeBytes()
    {
        byte[] ret = new byte[ElementSize * _size];
        Buffer.BlockCopy(byteArray, 0, ret, 0, ElementSize * _size);
        return ret;
    }
    
    /// <summary>
    /// Returns a copy of the intArray.
    /// </summary>
    public int[] GetSafeInts()
    {
        int[] ret = new int[_size];
        Buffer.BlockCopy(byteArray, 0, ret, 0, ElementSize * _size);
        return ret;
    }
}



"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
General.NET runtime environments Pin
Anonymous2-Feb-03 9:52
Anonymous2-Feb-03 9:52 
GeneralRe: .NET runtime environments Pin
Furty2-Feb-03 10:01
Furty2-Feb-03 10:01 
GeneralRe: .NET runtime environments Pin
Stephane Rodriguez.2-Feb-03 10:07
Stephane Rodriguez.2-Feb-03 10:07 
GeneralRe: .NET runtime environments Pin
Anonymous2-Feb-03 12:27
Anonymous2-Feb-03 12:27 
GeneralRe: .NET runtime environments Pin
ian mariano3-Feb-03 12:37
ian mariano3-Feb-03 12:37 
QuestionDetecting true or open or other type a font is? Pin
Member 1490692-Feb-03 7:23
Member 1490692-Feb-03 7:23 
QuestionAdding Controls to MenuItems? Pin
Jon Newman2-Feb-03 7:01
Jon Newman2-Feb-03 7:01 
AnswerRe: Adding Controls to MenuItems? Pin
Furty2-Feb-03 10:11
Furty2-Feb-03 10:11 
GeneralRe: Adding Controls to MenuItems? Pin
Jon Newman2-Feb-03 10:56
Jon Newman2-Feb-03 10:56 
GeneralRe: Adding Controls to MenuItems? Pin
Furty2-Feb-03 11:54
Furty2-Feb-03 11:54 
GeneralRe: Adding Controls to MenuItems? Pin
Jon Newman2-Feb-03 12:11
Jon Newman2-Feb-03 12:11 
GeneralRe: Adding Controls to MenuItems? Pin
Furty2-Feb-03 12:43
Furty2-Feb-03 12:43 
GeneralRe: Adding Controls to MenuItems? Pin
jtmtv182-Feb-03 14:36
jtmtv182-Feb-03 14:36 
GeneralSending Mail in C# Pin
Sassan Komeili Zadeh1-Feb-03 20:44
Sassan Komeili Zadeh1-Feb-03 20:44 
GeneralRe: Sending Mail in C# Pin
Vasudevan Deepak Kumar1-Feb-03 21:31
Vasudevan Deepak Kumar1-Feb-03 21:31 
GeneralRe: Sending Mail in C# Pin
leppie1-Feb-03 22:02
leppie1-Feb-03 22:02 
GeneralRe: Sending Mail in C# Pin
leppie1-Feb-03 22:10
leppie1-Feb-03 22:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.