On 2/9/06, The Fool <[EMAIL PROTECTED]> wrote:
>Ok so this is what I've been doing:
I'll cut down to the meat of it:
> *((int *) &Mem[PutAt * __Int_Size__]) = PutMe;
This looks reasonable. I presume that Mem[] is a char or byte array of some
kind... Are you only writing ints into Mem[]? If you're going to mix data
types in the same buffer, you'll need to consider data packing or padding,
and possibly be aware of data alignment issues.
>But you are telling me I need to create a union / structure fill it
>with the value, manually flip the bytes around and then store it?
I'm not sure what byte manipulations you're trying to do, but I don't think
you need to manually flip anything as long as you know what data type you're
dealing with at a given address (ie: long, short, char) in your buffer . If
you read the data out as an int just like you wrote it in, you won't ever
see the endian swap:
ie:
int GotMe = *((int *) &Mem[GetFrom *__Int_Size__]);
so if GetFrom == PutAt, GotMe == PutMe.
If you want to access the individual bytes, just keep the endian-ness in
mind:
// BYTE0_OFFSET == 0 // LSB byte
// BYTE3_OFFSET == 3 // MSB byte
unsigned char GotMeB0 = *((unsigned char *) &Mem[(GetFrom *__Int_Size__) +
BYTE0_OFFSET]); // get LSByte
unsigned char GotMeB3 = *((unsigned char *) &Mem[(GetFrom *__Int_Size__) +
BYTE3_OFFSET]); // get MSByte
Of course, using something like a union might make life easier. ie:
union
{
int intdata;
unsigned char bdata[4]; // bdata[0] is LSByte, bdata[3] is MSByte on a PC
} INTUNION;
INTUNION iu;
iu.intdata = *((int *) &Mem[GetFrom *__Int_Size__]); // pull data out as
integer
iu.bdata[0] = (iu.bdata[0] & BIT_MASK) | bit_val; // twiddle some bit in LS
Byte of integer
*((int *) &Mem[GetFrom * __Int_Size__]) = iu.intdata; // write modified
integer back to Mem buffer
(caveat: I haven't compiled/tested the above to ensure correctness, but you
get the gist.)
It's possible to get a lot fancier, of course. My boss wrote a really slick
set of platform-independent templates to handle this type stuff along
with non-aligned data packing. On the other hand, at a previous employer,
we usually simply used a union of pointers of all types and just manipulated
the data with pointer offsets.
-Bryon
_______________________________________________
http://www.mccmedia.com/mailman/listinfo/brin-l