Wednesday, 15 July 2015

How to extract 2 bytes from a word, and how to make a word from 2 bytes (in C)? -



How to extract 2 bytes from a word, and how to make a word from 2 bytes (in C)? -

i trying extract 2 bytes 16-bit word, , create 16-bit word 2 bytes. have tried (byte = unsigned char, word = unsigned short):

split grpix word 2 bytes:

word grpix; // assume value has been initialized byte grpixl = grpix & 0x00ff; byte grpixh = grpix & 0xff00;

make grpix word 2 bytes

byte grpixh; // assume value has been initialized byte grpixl; // assume value has been initialized word grpix = grpixh; grpix <<= 8; grpix += grpixl;

for reason, code doesn't work expected, , i'm not sure if "splitting" of word wrong, if "making" of word wrong, or both... give me advice?

you're not shifting when split word. if grpix 0x1234, grpixl gets expected 0x34 grpixh ends 0x1200. should say

byte grpixh = grpix >> 8;

of course, you're ignoring endianness concerns may present. should convert word known endian (with htons()) before attempting split (and reverse conversion when joining).

c byte word

No comments:

Post a Comment