By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,381 Members | 1,573 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,381 IT Pros & Developers. It's quick & easy.

Extracting part of integer

P: 1
I need a way to access the different bytes that makes up an integer. I use an image-processing library which gives the RGBA-value of a pixel as a single 32-bit integer. The first byte holds the Alpha, the secong byte holds the Blue, the third the Green, and the fourth byte the Red. I am interested in obtaining the four bytes separately and storing them in short-values.
May 28 '07 #1
Share this Question
Share on Google+
2 Replies


Expert 10K+
P: 11,448
I need a way to access the different bytes that makes up an integer. I use an image-processing library which gives the RGBA-value of a pixel as a single 32-bit integer. The first byte holds the Alpha, the secong byte holds the Blue, the third the Green, and the fourth byte the Red. I am interested in obtaining the four bytes separately and storing them in short-values.
You can cast the address of your int to an unsigned char pointer:
Expand|Select|Wrap|Line Numbers
  1. int yourInt;
  2. unsigned char* rgba= (unsigned char*)&yourInt;
Note though that the results will differ for big and little endian machines. Maybe
the best thing to is to copy your integer to network byte order (htonl) and use that.

kind regards,

Jos
May 28 '07 #2

weaknessforcats
Expert Mod 5K+
P: 9,197
Are you using Windows??

If so the calls are:
BYTE GetRValue( DWORD rgb );
BYTE GetGValue( DWORD rgb );
BYTE GetBValue( DWORD rgb );
May 28 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.