hi,
how can I covert a int to two char...
I have some idea using shift operators..
thanks...
hi,
how can I covert a int to two char...
I have some idea using shift operators..
thanks...
Your idea is a good one - see what you can come up with
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
my idea:
using big-endian address..
Code:char is 1 byte, int is 2 byte char a, b; int someValue, result; a = someValue; b = someValue << 8; result = getValue(a,b) int getValue(char a, char b) { int value1, value2, total ; value2 = value2 >> 8; total = value2 + value1; return total; }
Yes, that's the basic approach.
Now just put it all into a working program and see how you get on.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Endianness has nothing to do with addressing. It's about internal representation.Originally Posted by redsar
Emmanuel Delahaye
"C is a sharp tool"
I have some idea using a union. That's the incredibly lazy way to do it.
Quzah.
Hope is the first step on the road to disappointment.
Endianness has everything to do with how an int is stored in memory, and thus how you would cast a char to it.Originally Posted by Emmanuel Delaha
On a little endian machine, somechar would equal 0x67. On a big endian machine, somechar would equal 0x01.Code:int someint = 0x01234567; char somechar = (char)someint;
Of course, you meantOriginally Posted by bithub
char somechar = (char)(someint & 0xFF);
because a char can have more than 8 bits. The actual value is defined in CHAR_BIT from <limits.h>. On the DSP I work with (TMS320C54) the value is 16.
Emmanuel Delahaye
"C is a sharp tool"
What machine are you running this on that an int is only 2 chars?
Also, endianess does not affect shifting operations(*), so your code will yield the same result on both little and big endian machines. Casting is something different though.
(*) meaning sall, sarl, shrl or c's representations produce the same decimal results on both big and little endian
Last edited by Nyda; 09-05-2004 at 04:06 PM.
Many embedded systems are 16 bit, so he might be working on a DSP or a microcontroller. He might also be working on an old DOS or Windows 3.11 machineOriginally Posted by Nyda
You can first AND the number with 0xFFh into one variable and then again AND the original number with 0xFF0h into another var.