Converting hex array to integer.

  • Thread starter Thread starter Newbie
  • Start date Start date
N

Newbie

Hi,

I have a hex array in big endian format.
like -
byte[0]=0x07
byte[1]=0xC3.
I need to interpret it as 0x07C3 and convert it to it's integer value.

How can I do this?

Thanks,
Susan
 
Newbie said:
I have a hex array in big endian format.
like -
byte[0]=0x07
byte[1]=0xC3.

Note that there's no such thing as a "hex" array - here you have an
array of bytes (assuming you've got a real identifier like "array"
rather than "byte" which is a keyword). There's no difference between
the above and

byte[0]=7;
byte[1]=195;
I need to interpret it as 0x07C3 and convert it to it's integer value.

How can I do this?

The easiest way in this case is just:

ushort value = (array[0] << 8) | array[1];

There's also BitConverter.ToUInt16 but that depends on the endianness
of the system.
 
Jon Skeet said:
Newbie said:
I have a hex array in big endian format.
like -
byte[0]=0x07
byte[1]=0xC3.

Note that there's no such thing as a "hex" array - here you have an
array of bytes (assuming you've got a real identifier like "array"
rather than "byte" which is a keyword). There's no difference between
the above and

byte[0]=7;
byte[1]=195;
I need to interpret it as 0x07C3 and convert it to it's integer value.

How can I do this?

The easiest way in this case is just:

ushort value = (array[0] << 8) | array[1];

There's also BitConverter.ToUInt16 but that depends on the endianness
of the system.

Thanks for the reply.
I understood your solution.
But in my case the number of elements in the array is variable.
It can vary upto the maximum number of bytes an integer can take.
E.g. if integer is 3-bytes long then array would hold something like 0x01, 0x07,
0xC3.

Is my assumption that an integer can atmost take 4 bytes correct?
If I make this assumtion then Int32 is the type I can take the result into.

E.g. for 3 bytes i need to do -
((array[0] << 8) | array[1]) << 16) | array[2]

So, taking the above logic, is the following code correct?
Int32 result = array[0];
for (int i =1; i<NoOfBytesInInt - 1; i++)
result = (result << 8*i) | array[i+1];

Thanks,
Susan
 
Newbie said:
Thanks for the reply.
I understood your solution.
But in my case the number of elements in the array is variable.
It can vary upto the maximum number of bytes an integer can take.
E.g. if integer is 3-bytes long then array would hold something like 0x01, 0x07,
0xC3.

Is my assumption that an integer can atmost take 4 bytes correct?
If I make this assumtion then Int32 is the type I can take the result into.

E.g. for 3 bytes i need to do -
((array[0] << 8) | array[1]) << 16) | array[2]

So, taking the above logic, is the following code correct?
Int32 result = array[0];
for (int i =1; i<NoOfBytesInInt - 1; i++)
result = (result << 8*i) | array[i+1];

Nope - not quite: because you're shifting every time, you don't need to
multiply the amount you're shifting by i. You've also got at least one
off-by-one error (array[1] is never used).

This should work:

int result=0;
for (int i=0; i < array.Length; i++)
{
result = (result<<8) | array;
}
 
Jon Skeet said:
Newbie said:
Thanks for the reply.
I understood your solution.
But in my case the number of elements in the array is variable.
It can vary upto the maximum number of bytes an integer can take.
E.g. if integer is 3-bytes long then array would hold something like 0x01, 0x07,
0xC3.

Is my assumption that an integer can atmost take 4 bytes correct?
If I make this assumtion then Int32 is the type I can take the result into.

E.g. for 3 bytes i need to do -
((array[0] << 8) | array[1]) << 16) | array[2]

So, taking the above logic, is the following code correct?
Int32 result = array[0];
for (int i =1; i<NoOfBytesInInt - 1; i++)
result = (result << 8*i) | array[i+1];

Nope - not quite: because you're shifting every time, you don't need to
multiply the amount you're shifting by i. You've also got at least one
off-by-one error (array[1] is never used).

This should work:

int result=0;
for (int i=0; i < array.Length; i++)
{
result = (result<<8) | array;
}

You are correct. ( Ofcourse MVPs are correct most of the time :-) )
Thank you for pointing out the mistake I was making.
Regards,
Susan
 
Back
Top