how to convert bytes array into int
-
hello i want to read sequenece numbers from the packets that are in a dump file. problem is that i hav read the header into byte array. and want to convert the sequence numbers into integer. my code is unsigned char buff[2]; buff[0]=header[22]; buff[1]=header[23]; unsigned int t; n=(buff[0]<<24)|(buff[1]<<16); header is byte array containing header. byet 22 and 23 is sequqnce number and i want to convert it into int. but this code is not working properly. can anybody help me.
-
hello i want to read sequenece numbers from the packets that are in a dump file. problem is that i hav read the header into byte array. and want to convert the sequence numbers into integer. my code is unsigned char buff[2]; buff[0]=header[22]; buff[1]=header[23]; unsigned int t; n=(buff[0]<<24)|(buff[1]<<16); header is byte array containing header. byet 22 and 23 is sequqnce number and i want to convert it into int. but this code is not working properly. can anybody help me.
1/ Read on the posting guidlines - particularly the pre tag. It marks out what is code, and what is text. 2/ You declare t, then don't use it. 3/ You use n, but don;t declare it. Without knowing things like type, we can;t help you much. 3/ buff [0] is a uchar. (buf [0] << 24) is also a uchar - and only has room for 8 bits, and will therefore end up as 0. 4/ Lastly, some help:
DWORD dwResult = 0;
dwResult |= header [22]; // assuming header is a big 'ol array of bytes.
dwResult <<8; // shift it across a bit.
dwResult |= header [23;
dwResult <<= 16; // shift it some more.Enjoy, Iain.
Iain Clarke appears because CPallini still cares.
-
hello i want to read sequenece numbers from the packets that are in a dump file. problem is that i hav read the header into byte array. and want to convert the sequence numbers into integer. my code is unsigned char buff[2]; buff[0]=header[22]; buff[1]=header[23]; unsigned int t; n=(buff[0]<<24)|(buff[1]<<16); header is byte array containing header. byet 22 and 23 is sequqnce number and i want to convert it into int. but this code is not working properly. can anybody help me.
So, if I understand you well, you have an array of two bytes and you want to 'convert' this two bytes into ONE integer ? If yes, then simply do something like this:
int Value = buff[0] + buff[1] * 256;
Depending on the byte ordering, you will perhaps have to invert them.Cédric Moonen Software developer
Charting control [v1.3] -
hello i want to read sequenece numbers from the packets that are in a dump file. problem is that i hav read the header into byte array. and want to convert the sequence numbers into integer. my code is unsigned char buff[2]; buff[0]=header[22]; buff[1]=header[23]; unsigned int t; n=(buff[0]<<24)|(buff[1]<<16); header is byte array containing header. byet 22 and 23 is sequqnce number and i want to convert it into int. but this code is not working properly. can anybody help me.
It depends on byte ordering of your packet, i.e Big Endian:
unsigned int n = (buff[0] << 8) | buff[1];
Little Endian:
unsigned int n = (buff[1] << 8) | buff[0];
:)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarkemodified on Thursday, April 17, 2008 8:27 AM
-
1/ Read on the posting guidlines - particularly the pre tag. It marks out what is code, and what is text. 2/ You declare t, then don't use it. 3/ You use n, but don;t declare it. Without knowing things like type, we can;t help you much. 3/ buff [0] is a uchar. (buf [0] << 24) is also a uchar - and only has room for 8 bits, and will therefore end up as 0. 4/ Lastly, some help:
DWORD dwResult = 0;
dwResult |= header [22]; // assuming header is a big 'ol array of bytes.
dwResult <<8; // shift it across a bit.
dwResult |= header [23;
dwResult <<= 16; // shift it some more.Enjoy, Iain.
Iain Clarke appears because CPallini still cares.
Are you sure he wants the original number times
65536
? :-DIf the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke -
Are you sure he wants the original number times
65536
? :-DIf the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain ClarkeNo - but he did do a shift by 24 bits on one byte, and 16 bits on another. Given that I was guessing at his code (ie, taking his word for which was the high byte, etc), it was one of the few bits of solid data he'd actually provided. Iain.
Iain Clarke appears because CPallini still cares.
-
hello i want to read sequenece numbers from the packets that are in a dump file. problem is that i hav read the header into byte array. and want to convert the sequence numbers into integer. my code is unsigned char buff[2]; buff[0]=header[22]; buff[1]=header[23]; unsigned int t; n=(buff[0]<<24)|(buff[1]<<16); header is byte array containing header. byet 22 and 23 is sequqnce number and i want to convert it into int. but this code is not working properly. can anybody help me.
If I understand correctly, your sequence number you want is in bytes buf[22] and buf[23]. If that's true, you can use a cast to retrieve the value:
unsigned short SeqNo;
SeqNo = *(short*)&buf[22];Note that if it's only two bytes, it's a short and not an int. Hope that helps.
Karl - WK5M PP-ASEL-IA (N43CS) PGP Key: 0xDB02E193 PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193