Page 1 of 1

[NXC] Separate signed int into bytes

Posted: 25 Apr 2011, 06:42
by muntoo
First NXC Question on StackOverflow

Can someone tell me how to separate an int into an array of bytes? [I can't figure it out for negative numbers...]

For example, I have:

Code: Select all

int val = 1025;
// val = 0000 0100 0000 0001
And I want this:

Code: Select all

byte MM_mem[2] = {0x04, 0x01};
How would I do this in NXC?

Thanks! :D

-----

And another question: Is a signed integer stored using one's complement or what?

Re: [NXC] Separate signed int into bytes

Posted: 25 Apr 2011, 06:52
by mightor
I would normally split them up like this:

Code: Select all

int foo = 0x4589
byte msb = (foo >> 8) & 0xFF;
byte lsb = foo & 0xFF;
- Xander

Re: [NXC] Separate signed int into bytes

Posted: 25 Apr 2011, 07:29
by muntoo
mightor wrote:I would normally split them up like this:

Code: Select all

int foo = 0x4589
byte msb = (foo >> 8) & 0xFF;
byte lsb = foo & 0xFF;
I don't think think that'll work with NXC, as it uses arithmetic shifts. My problem, though, was that my USB wasn't connected*. (Can you believe it? :lol:)

The solution:

Code: Select all

int foo = 0x4589;
byte msb = (foo & 0xFF00) >> 8;
byte lsb = foo & 0xFF;
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 ... :evil: :evil: :x :x :oops:

Re: [NXC] Separate signed int into bytes

Posted: 25 Apr 2011, 08:44
by mightor
muntoo wrote:The solution:

Code: Select all

int foo = 0x4589;
byte msb = (foo & 0xFF) >> 8;
byte lsb = foo & 0xFF;
The first one makes no sense:
0x4589 & 0xFF = 0x0089
0x0089 >> 8 = 0x00

- Xander

Re: [NXC] Separate signed int into bytes

Posted: 25 Apr 2011, 12:13
by m-goldberg
muntoo wrote:First NXC Question on StackOverflow
And another question: Is a signed integer stored using one's complement or what?
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.

Code: Select all

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
Sorry, I meant (foo & 0xFF00) >> 8.