Computing.Net > Forums > Programming > C++: Convert Multibyte buffer to integer

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

C++: Convert Multibyte buffer to integer

Reply to Message Icon

Name: steve mcrae
Date: May 15, 2002 at 16:51:23 Pacific
Comment:

I am developing in vc++ 7 (.net) and am having a brain fart.

I receie a byte buffer (bRecBuf) containing 00 64 90

The 90 referes to a success code so we can forget about the 3rd byte.

The first and second byte are a multibyte hex that needs to be converted to an integer.

so I have

bRecBuf[0]; // 0x00
bRecBuf[1]; // 0x64

and I need to convert it to integer 100

I can do this in my head but cant programatically? any help is appreciated/

Thanks,

Steve



Sponsored Link
Ads by Google

Response Number 1
Name: Jim
Date: May 15, 2002 at 17:05:35 Pacific
Reply:

int Result;

Result = (int)bRecBuf[0] * 0x100 + (int)bRecBuf[1];


0

Response Number 2
Name: steve mcrae
Date: May 15, 2002 at 17:30:51 Pacific
Reply:

Thanks Jim but sorry I was not clear.

The bytes in the buffer are hex code 0x00 and 0x64 and either can range from 0x00 to 0xFF. In this case 00 64 would translate to 100 and 00 01 would translate to 1. Thanks for the quick response.


0

Response Number 3
Name: Jim
Date: May 15, 2002 at 21:42:52 Pacific
Reply:

> Thanks Jim but sorry I was not clear.

Pretty sure that code will do what you want.

> The bytes in the buffer are hex code 0x00 and 0x64 and either can range from 0x00 to 0xFF. In this case 00 64 would translate to 100 and 00 01 would translate to 1.

Yes. It's in "Hi-Lo" order. You have to take the first byte and multiply it by 256 (0x100) and add the second byte. If it were in "Lo-Hi" order, you could just typecast the whole thing as a word. 02 10 would translate into 528, right?

The (int) typecast will turn the byte into an integer. I expect your byte type is an unsigned char.



0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: C++: Convert Multibyte buffer to integer

convert excel file to grpah www.computing.net/answers/programming/convert-excel-file-to-grpah/14349.html

c: convert char a[] to int b[] www.computing.net/answers/programming/c-convert-char-a-to-int-b/4572.html

Determine max. real number from external file www.computing.net/answers/programming/determine-max-real-number-from-external-file/20304.html