Friday, March 12, 2010

C/C++ packing signed char into int

Programmer Question

hello



I have need to pack four signed bytes into 32-bit integral type.
this is what I came up to:



int32_t byte(int8_t c) { return (unsigned char)c; }

int pack(char c0, char c1, ...) {
return byte(c0) | byte(c1) << 8 | ...;
}


is this a good solution? Is it portable (not in communication sense)?
is there a ready-made solution, perhaps boost?



issue I am mostly concerned about is bit order when converting of negative bits from char to int. I do not know what the correct behavior should be.



Thanks



Find the answer here

No comments:

Post a Comment

LinkWithin

Related Posts with Thumbnails