473,394 Members | 1,709 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

how to naturally put an int into an array of bytes

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;

Dec 19 '06 #1
7 1781
JohnShade wrote:
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);

Dec 19 '06 #2
JohnShade wrote:
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

Dec 19 '06 #3
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" <pr**************@poczta.onet.plwrote in message
news:11**********************@t46g2000cwa.googlegr oups.com...
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;

Dec 19 '06 #4
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
Dec 19 '06 #5

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" <pr**************@poczta.onet.plwrote in message
news:11**********************@t46g2000cwa.googlegr oups.com...
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

Dec 19 '06 #6
JohnShade wrote:
>
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);

Dec 19 '06 #7
JohnShade <pr**************@poczta.onet.plwrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 19 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
4
by: Bryan Parkoff | last post by:
I want to allocate pointer array into memory so pointer array contains ten pointers. It would be 4 bytes per pointer to be total 40 bytes. Looks like below for example. unsigned char* A = new...
6
by: Herrcho | last post by:
in K&R Chapter 6.3 it mentions two methods to calculate NKEYS. and points out the first one which is to terminate the list of initializers with a null pointer, then loop along keytab until the...
22
by: Wynand Winterbach | last post by:
I think every C programmer can relate to the frustrations that malloc allocated arrays bring. In particular, I've always found the fact that the size of an array must be stored separately to be a...
3
by: Nick | last post by:
I have found a class that compresses and uncompresses data but need some help with how to use part of it below is the deflate method which compresses the string that I pass in, this works OK. At...
8
by: intrepid_dw | last post by:
Hello, all. I've created a C# dll that contains, among other things, two functions dealing with byte arrays. The first is a function that returns a byte array, and the other is intended to...
1
by: Geoffrey | last post by:
Hello, I have one array of Bytes , and I want to add some Bytes at the start of the array. Is it possible to move all the bytes of the array to right , so I can insert my Bytes like Bytes, ......
27
by: lovecreatesbea... | last post by:
This code snippet is an exercise on allocating two dimension array dynamically. Though this one is trivial, is it a correct one? Furthermore, when I tried to make these changes to the original...
2
by: O.B. | last post by:
When using Marshal to copy data from a byte array to the structure below, only the first byte of the "other" array is getting copied from the original byte array. What do I need to specify to get...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.