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 › byte to int conversion problem ( 141-> -115 )
Page Index Toggle Pages: 1
byte to int conversion problem ( 141-> -115 ) (Read 1262 times)
byte to int conversion problem ( 141-> -115 )
Nov 8th, 2009, 6:32am
 
hellow
If i write int -> byte[4] (and back) conversion rutines for network communications    

Code:

void i2b(int from,byte[] to,int offs){
 //println(offs);
 //println(hex(int));
 to[offs]=  byte((from&0xFF000000)>>24);
 to[offs+1]=byte((from&0x00FF0000)>>16);
 to[offs+2]=byte((from&0x0000FF00)>>8);
 to[offs+3]=byte( from&0x000000FF);
 println(hex(from)
 +" "+ hex(to[offs])
 +" "+ hex(to[offs+1])
 +" "+ hex(to[offs+2])
 +" "+ hex(to[offs+3]));
}

int b2i (byte [] from,int offs){
 int out;
 int out0=(from[offs]   << 24);
 int out1=(from[offs+1] << 16);
 int out2=(from[offs+2] << 8);
 int out3=from[offs+3] ;
 out = out0 | out1 | out2 | out3;
 println(
  " "+hex(out0)
 +" "+hex(out1)
 +" "+hex(out2)
 +" "+hex(out3)
 +" "+hex(from[offs])
 +" "+hex(from[offs+1])
 +" "+hex(from[offs+2])
 +" "+hex(from[offs+3])
 +" "+hex(out));
 return (out);
}


it does not work well for some numbers beacuse of some automatic byte sign rutine which I can't swich off.

for example

Code:

//int to byte work well
117 141 117 141
00000075 00 00 00 75
0000008D 00 00 00 8D
00000075 00 00 00 75
0000008D 00 00 00 8D

//byte to int works bad :(
00 00 00 75 00000075
00 00 00 8D FFFFFF8D
00 00 00 75 00000075
00 00 00 8D FFFFFF8D
117 -115 117 -115



Re: byte to int conversion problem ( 141-> -115 )
Reply #1 - Nov 8th, 2009, 6:45am
 
See if using >>> 24 instead of >> 24 works better.
Or shift first and apply mask after.
Re: byte to int conversion problem ( 141-> -115 )
Reply #2 - Nov 8th, 2009, 3:00pm
 
no,
I tracked the problem to line
int out3=from[offs+3]
in backward conversion rutine (byte to int)

if byte from[offs+3] is 0000008D than the int out3 become FFFFFF8D
or 0000018D -> FFFFFE8D

I think the problem is that processing is too intelignet and tries to convert unsigned byte to signed, or something like this

Code:

int b2i (byte [] from,int offs){
 int out;
 int out0=(from[offs]   << 24);
 int out1=(from[offs+1] << 16);
 int out2=(from[offs+2] << 8);
 int out3=from[offs+3] ;


has this result
Code:

from[0..3]:  00 00 00 8D

out0: 00000000
out1: 00000000
out2: 00000000
out3: FFFFFF8D

Re: byte to int conversion problem ( 141-> -115 )
Reply #3 - Nov 9th, 2009, 2:11am
 
Processing is not too intelligent...
Java (used by Processing) knows only signed values. Which is sometime really annoying.
As I wrote, you can just mask the excess bits: int out3=from[offs+3] & 0xFF;
Re: byte to int conversion problem ( 141-> -115 )
Reply #4 - Nov 9th, 2009, 1:26pm
 
int out3=from[offs+3] & 0xFF;   ?
I probably don't understand
if I mask byte by & 0xFF it does nothing
if I mask the sign bit by & 0x8F I lose the information which I need to reconstruct the Int

Sure one way is to write function which turn negative values back to positive like
if(myByte<0){myInt=-myByte+1;}
but it is much slower than just <<, &, | operations
Re: byte to int conversion problem ( 141-> -115 )
Reply #5 - Nov 10th, 2009, 2:11am
 
Unless I misunderstood your problem, it works for me.
Tested with:
Code:
void setup()
{
byte[] from = { 0, 0, 0, (byte) 0x8D };
b2i(from, 0);
}


int b2i(byte[] from, int offs) {
int out;
int out0=(from[offs] << 24);
int out1=(from[offs+1] << 16);
int out2=(from[offs+2] << 8);
int out3=from[offs+3] & 0xFF;

out = out0 | out1 | out2 | out3;
println(
" "+hex(out0)
+" "+hex(out1)
+" "+hex(out2)
+" "+hex(out3)
+" "+hex(from[offs])
+" "+hex(from[offs+1])
+" "+hex(from[offs+2])
+" "+hex(from[offs+3])
+" "+hex(out));
return (out);
}
Re: byte to int conversion problem ( 141-> -115 )
Reply #6 - Nov 10th, 2009, 3:44am
 
The sign "interference" happens with all four byte values, not just from[3].

It may be preferable to shift before masking in your i2b() method to avoid more problems...

binary((0x81828384 & 0xff000000) >> 24) = "11111111111111111111111110000001"

which is probably not what you expect!

Right-shifting with >> duplicates the highest bit instead of shifting in zeros. I am supposing your i2b() method works due to the byte() function discarding the bits that don't fit from the integer.

Code:
void i2b(int from, byte[] to, int offs) {
 to[offs    ] = byte((from >>> 24) & 0xff);
 to[offs + 1] = byte((from >>> 16) & 0xff);
 to[offs + 2] = byte((from >>>  8) & 0xff);
 to[offs + 3] = byte( from        & 0xff);
}

int b2i(byte[] from, int offs) {
 return ((from[offs    ] & 0xff) << 24) |
        ((from[offs + 1] & 0xff) << 16) |
        ((from[offs + 2] & 0xff) <<  8) |
        ( from[offs + 3] & 0xff       );
}
Page Index Toggle Pages: 1