A little rant:
*Someone unplugged it in order to stick in a USB (none of the other ports would do, of course, even though they were all there, just begging to be used).
Then, I tried figuring it out for about an hour (and researching a bit). I decided I'd put a NumOut() and Wait() in my function, but it wouldn't display. I changed some of the variables, but the program would remain the same... almost as if it wasn't downloading.
My bloodshot eyes wandered over to the USB ports, and ...
Negative signed integers are represented by two's complement values.
Re: [NXC] Separate signed int into bytes
Posted: 25 Apr 2011, 15:47
by afanofosc
Copied from my response to this question on StackOverflow:
The best way to do this in NXC with the opcodes available in the underlying VM is to use FlattenVar to convert any type into a string (aka byte array with a null added at the end). It results in a single VM opcode operation where any of the above options which use shifts and logical ANDs and array operations will require dozens of lines of assembly language.
task main()
{
int x = Random(); // 16 bit random number - could be negative
string data;
data = FlattenVar(x); // convert type to byte array with trailing null
NumOut(0, LCD_LINE1, x);
for (int i=0; i < ArrayLen(data)-1; i++)
{
#ifdef __ENHANCED_FIRMWARE
TextOut(0, LCD_LINE2-8*i, FormatNum("0x%2.2x", data[i]));
#else
NumOut(0, LCD_LINE2-8*i, data[i]);
#endif
}
Wait(SEC_4);
}
The best way to get help with LEGO MINDSTORMS and the NXT and Not eXactly C is via the mindboards forums at http://forums.mindboards.net/
John Hansen
Re: [NXC] Separate signed int into bytes
Posted: 25 Apr 2011, 20:14
by muntoo
mightor wrote:The first one makes no sense:
0x4589 & 0xFF = 0x0089
0x0089 >> 8 = 0x00