[NXC] Separate signed int into bytes

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
Post Reply
muntoo
Posts: 834
Joined: 01 Oct 2010, 02:54
Location: Your Worst Nightmare
Contact:

[NXC] Separate signed int into bytes

Post 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?
Image

Commit to LEGO Mindstorms Robotics Stack Exchange:
bit.ly/MindstormsSE


Commit to LEGO Stack Exchange: bit.ly/Area51LEGOcommit
mightor
Site Admin
Posts: 1079
Joined: 25 Sep 2010, 15:02
Location: Rotterdam, Netherlands
Contact:

Re: [NXC] Separate signed int into bytes

Post 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
| My Blog: I'd Rather Be Building Robots (http://botbench.com)
| RobotC 3rd Party Driver Suite: (http://rdpartyrobotcdr.sourceforge.net)
| Some people, when confronted with a problem, think, "I know, I'll use threads,"
| and then two they hav erpoblesms. (@nedbat)
muntoo
Posts: 834
Joined: 01 Oct 2010, 02:54
Location: Your Worst Nightmare
Contact:

Re: [NXC] Separate signed int into bytes

Post 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:
Last edited by muntoo on 25 Apr 2011, 20:12, edited 1 time in total.
Image

Commit to LEGO Mindstorms Robotics Stack Exchange:
bit.ly/MindstormsSE


Commit to LEGO Stack Exchange: bit.ly/Area51LEGOcommit
mightor
Site Admin
Posts: 1079
Joined: 25 Sep 2010, 15:02
Location: Rotterdam, Netherlands
Contact:

Re: [NXC] Separate signed int into bytes

Post 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
| My Blog: I'd Rather Be Building Robots (http://botbench.com)
| RobotC 3rd Party Driver Suite: (http://rdpartyrobotcdr.sourceforge.net)
| Some people, when confronted with a problem, think, "I know, I'll use threads,"
| and then two they hav erpoblesms. (@nedbat)
m-goldberg
Posts: 73
Joined: 29 Sep 2010, 12:05

Re: [NXC] Separate signed int into bytes

Post 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.
Regards, Morton
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: [NXC] Separate signed int into bytes

Post 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
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
muntoo
Posts: 834
Joined: 01 Oct 2010, 02:54
Location: Your Worst Nightmare
Contact:

Re: [NXC] Separate signed int into bytes

Post by muntoo »

mightor wrote:The first one makes no sense:
0x4589 & 0xFF = 0x0089
0x0089 >> 8 = 0x00
Sorry, I meant (foo & 0xFF00) >> 8.
Image

Commit to LEGO Mindstorms Robotics Stack Exchange:
bit.ly/MindstormsSE


Commit to LEGO Stack Exchange: bit.ly/Area51LEGOcommit
Post Reply

Who is online

Users browsing this forum: Semrush [Bot] and 4 guests