Click to See Complete Forum and Search --> : combining 2 bytes into an int then convert to 2 byte hex!


Vonny232
June 16th, 2006, 01:50 PM
Hi, i have got a tricky problem that I will try to explain.

I am supporting an application that recieves a data packet (with crc) from a modem, the program should store the supplied crc result and compare it with its own generated crc result!

the application generated CRC is generated as an integer that when converted to HEX represents 2 bytes.

eg 64409 converts to FB99 hex!

this is all fine! the crc function is a polynomial ccitt crc i got off the web!

anyways..

I want to compare this application generated CRC with the CRC attached to the end of the data packet being sent to me from the modem.

the problem is the CRC is contained in a byte array (0-1).

I cannot fathom how to turn a 2 byte array to an int value that can be compared!

Adding the values dont work, and concatinating the bytes as string then converting to int doesent work!

I think using the bit shift operator may work but I havent had any success yet, can anyone help!



recap :

byte array[0] = FB
array[1] = 99

I want to be able to convert both bytes to a single int so the result is a combined 64409 integer

Dan

darwen
June 16th, 2006, 03:10 PM
Have a look at the left shift operator <<.

And the source code :


byte array[] = new byte[];
array[0] = 0xFB;
array[1] = 0x99;
int nValue = array[0] + (array[1] << 8);


I.e. left shift by 8 bits the value in array[1] and add the value in array [0].

Just to add a little clarity here's a DWORD represented by a byte array :


byte array[] = new byte[4];
array[0] = 0x12;
array[1] = 0x44;
array[2] = 0x64;
array[3] = 0xAA;

uint nValue = array[0] + (array[1] << 8) + (array[2] << 16) + (array[3] << 24);


Darwen.