how to naturally put an int into an array of bytes

J

JohnShade

sorry for my bad english


i ve array of bytes, byte[] data
i wanna put an int into this array on some arbitrary position
how is the better way to do this?

in this moment i only can do some thing like that

int pos=17;
int myintval = 0xaabbccdd;

data[pos+0]= (byte) (myintva>>24)%256;
data[pos+1]= (byte) (myintva>>16)%256;
data[pos+2]= (byte) (myintva>>8)%256;
data[pos+3]= (byte) (myintva>>0)%256;
 
C

Chris Dunaway

JohnShade said:
int pos=17;
int myintval = 0xaabbccdd;

data[pos+0]= (byte) (myintva>>24)%256;
data[pos+1]= (byte) (myintva>>16)%256;
data[pos+2]= (byte) (myintva>>8)%256;
data[pos+3]= (byte) (myintva>>0)%256;

You could try something like this but it's not necessarily the fastest
way:

int pos = 17;
int myintval = 0xaabbccdd;

byte[] intArray = BitConverter(GetBytes(myintval));
Array.Reverse(intArray);

Array.Copy(intArray, 0, data, pos, 4);
 
K

ktrvnbq02

JohnShade said:
i ve array of bytes, byte[] data
i wanna put an int into this array on some arbitrary position
how is the better way to do this?

in this moment i only can do some thing like that

int pos=17;
int myintval = 0xaabbccdd;

data[pos+0]= (byte) (myintva>>24)%256;
data[pos+1]= (byte) (myintva>>16)%256;
data[pos+2]= (byte) (myintva>>8)%256;
data[pos+3]= (byte) (myintva>>0)%256;

I'd be inclined to mask and shift, rather than shift and use the
modulus operator, but otherwise that's probably about as fast as you're
going to get without resorting to unsafe/unmanaged code.

data[pos+0] = (byte) ((myintval & 0xFF000000) >> 24)
data[pos+1] = (byte) ((myintval & 0x00FF0000) >> 16)
data[pos+2] = (byte) ((myintval & 0x0000FF00) >> 8)
data[pos+3] = (byte) (myintval & 0x000000FF)


Regards,

Matt
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

What is wrong with that? It sure is fast

The other only possible solution is to convert the int to byte using
BitConverter.GetBytes() and then array.Copy
 
J

Jay

Do you get a sign-extended shift with an int? If so, data[pos+0] looks suspect to me. The following
would get around this problem...

data[pos+0]= (byte) ((myintva>>24) & 0xFF);
data[pos+1]= (byte) ((myintva>>16) & 0xFF);
data[pos+2]= (byte) ((myintva>>8) & 0xFF);
data[pos+3]= (byte) ((myintva>>0) & 0xFF);

If you don't get a sign-extended shift, then the first can be simplified to
data[pos+0]= (byte) (myintva>>24);



Replying to...
I'd be inclined to mask and shift, rather than shift and use the
modulus operator, but otherwise that's probably about as fast as you're
going to get without resorting to unsafe/unmanaged code.

data[pos+0] = (byte) ((myintval & 0xFF000000) >> 24)
data[pos+1] = (byte) ((myintval & 0x00FF0000) >> 16)
data[pos+2] = (byte) ((myintval & 0x0000FF00) >> 8)
data[pos+3] = (byte) (myintval & 0x000000FF)


Regards,

Matt
 
J

JohnShade

Ignacio Machin ( .NET/ C# MVP ) napisal(a):
Hi,

What is wrong with that? It sure is fast

The other only possible solution is to convert the int to byte using
BitConverter.GetBytes() and then array.Copy


--
Ignacio Machin
machin AT laceupsolutions com

JohnShade said:
sorry for my bad english


i ve array of bytes, byte[] data
i wanna put an int into this array on some arbitrary position
how is the better way to do this?

in this moment i only can do some thing like that

int pos=17;
int myintval = 0xaabbccdd;

data[pos+0]= (byte) (myintva>>24)%256;
data[pos+1]= (byte) (myintva>>16)%256;
data[pos+2]= (byte) (myintva>>8)%256;
data[pos+3]= (byte) (myintva>>0)%256;

above is my endian-type mistake, couse it should be

data[2]=(byte) ((myintva>>00)%256);
data[3]=(byte) ((myintva>>08)%256);
data[4]=(byte) ((myintva>>16)%256);
data[5]=(byte) ((myintva>>24)%256);

or

data[2]=(byte) ((myintva & 0x000000FF) >>00 );
data[3]=(byte) ((myintva & 0x0000FF00) >>08);
data[4]=(byte) ((myintva & 0x00FF0000)>>16);
data[5]=(byte) ((myintva & 0xFF000000) >>24);

which is far better to read i agree : )

I am suprised that in c# it probably is nothing that is "shorter in
text"
and would do that what i can write as byte[] << int32
like *((int32*) &data[17]) = 0xaabbccdd or what it was like in c

but whatever, TNX
JSh
 
C

Chris Dunaway

JohnShade said:
above is my endian-type mistake, couse it should be

data[2]=(byte) ((myintva & 0x000000FF) >>00 );
data[3]=(byte) ((myintva & 0x0000FF00) >>08);
data[4]=(byte) ((myintva & 0x00FF0000)>>16);
data[5]=(byte) ((myintva & 0xFF000000) >>24);

I am suprised that in c# it probably is nothing that is "shorter in
text"

If you're not trying to convert endianness, then you can use
Array.Copy:

int pos = 17;
int myintval = 0xaabbccdd;

byte[] bytes = BitConverter(GetBytes(myintval));
Array.Copy(bytes, 0, data, pos, bytes.Length);
 
J

Jon Skeet [C# MVP]

JohnShade said:
sorry for my bad english


i ve array of bytes, byte[] data
i wanna put an int into this array on some arbitrary position
how is the better way to do this?

in this moment i only can do some thing like that

int pos=17;
int myintval = 0xaabbccdd;

data[pos+0]= (byte) (myintva>>24)%256;
data[pos+1]= (byte) (myintva>>16)%256;
data[pos+2]= (byte) (myintva>>8)%256;
data[pos+3]= (byte) (myintva>>0)%256;

You might want to use EndianBitConverter from my MiscUtil library:
http://www.pobox.com/~skeet/csharp/miscutil

1) You can choose the endianness
2) You can convert into an existing byte array with the CopyBytes
family of methods.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top