We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › how to split HEX
Page Index Toggle Pages: 1
how to split HEX (Read 848 times)
how to split HEX
Dec 14th, 2009, 11:43am
 
Hi at all,

i get in a program a hex result like this.
0000074F

For the communication with my serial it is necessary to send first the last two bytes 4F and then the two high bytes 07.

For example:
.
.
.
myPort.write(0x4F);
myPort.write(0x07);

How i can split this hex 0000074F in two parts?? Please be detailed  Cry
Thanks for your help  Cheesy
Re: how to split HEX
Reply #1 - Dec 14th, 2009, 12:00pm
 
What you call "the last two bytes 4F" is actually only 1 byte.

Code:

int h = 0x0000074F;
byte b1 = (byte)(h&0xFF);
byte b2 = (byte)( (h>>8)&0xFF );
Re: how to split HEX
Reply #2 - Dec 14th, 2009, 12:03pm
 
Need to use binary arithmetic this should work (n, n1 and n2 are integers)

Code:

n = 0x0000074F

n1 = (n & 0xff);
n2 = (n & 0xff00) >> 8;

myPort.write(n1);
myPort.write(n2);


JR beat me too it and remembered Smiley to cast to Bytes
Re: how to split HEX
Reply #3 - Dec 14th, 2009, 12:34pm
 
Wow  Shocked
thats real kindly and fast answering. Thanks

When i wrote it like this ...:
.
.
.
   int servograu = ceil( map (grau, -16777216, -1, 732, 2524));   //servograu means servo grey
   
   byte b1 = (byte)(servograu&0xFF);
   byte b2 = (byte)( (servograu>>8)&0xFF );
   
   println (grau + "   "+ hex(servograu)+"  "+hex(b1)+"  "+hex(b2));
.
.

..then i get in the printed line my split data. Also i cant understand how it work ( i am a bloody beginner).

The problem is, when i send it to the serial device like this :

.
.
   myPort.write(0xAA);
   myPort.write(0xA0);
   myPort.write(0x55);
   myPort.write(0x01);
   myPort.write(0x01);
   myPort.write(0x02);
   myPort.write(hex(b1));
   myPort.write(hex(b2));
.
.

there comes not the right reaction made by my  servo  Cry
Is it possible that there are missing characters 0x at start of the hex data to declare it as an hex for the controller card???
Embarrassed damn thats distressing.
Re: how to split HEX
Reply #4 - Dec 14th, 2009, 1:05pm
 
Quark wrote on Dec 14th, 2009, 12:03pm:
Code:
n2 = (n & 0xff00) >> 8; 


WARNING!
Masking using bitwise and ("&") before shifting (">>") isn't especially safe.
Using the ">>" could lead to unexpected results, because it uses sign extension (the highest bit is copied into as many bits as shifted).

Use ">>>" instead (zeros copied into highest bits), and I suggest masking afterward, so you need only ever mask the low order bits (& 0xff). It appears that casting using (byte) already does this, but relying on the casting doing that is probably frowned upon (could possibly be changed in future or possibly throw an exception).

In this "two-byte" example the result is okay, but if you try this with a "four-byte" integer value you will run into problems.

Code:
(0xFFFFFFFF  >> 24) == 0xFFFFFFFF // -1 -> -1; no change!
(0xFFFFFFFF >>> 24) == 0x000000FF // -1 -> 255



-spxl
Re: how to split HEX
Reply #5 - Dec 14th, 2009, 3:18pm
 
Zoodoo - look up 'bitwise operators' Wink
Page Index Toggle Pages: 1