• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

converting byte[] to int

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I have a byte[] array read from a binary file.

I came up with a very easy solution which is to shift the byte depending on their position and OR it with the next byte to create my int.
example

int test = (data[7] >> 8) | data[8];
data[7] contains 02 in hex
data[8] contains B8 in hex

the int value theoratically should be 696, becuse that is what i wrote in my file and that is what the file contains in hex 02B8. Except when i print the result i get -72!!!

i dont understand why, since 0x02 is 0010 0000 0000
and 0xB8 is 0000 1011 1000
ORing them would give the value of 696 0010 1011 1000

What do you think is the problem? i am using SDk 1.4.2_05 to generate the binary file, and using MIDP2 to read the file and convert from byte[] to int.

Thanks,
Mike
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Mike Mass:

int test = (data[7] >> 8) | data[8];
data[7] contains 02 in hex
data[8] contains B8 in hex

the int value theoratically should be 696, becuse that is what i wrote in my file and that is what the file contains in hex 02B8. Except when i print the result i get -72!!!

i dont understand why, since 0x02 is 0010 0000 0000
and 0xB8 is 0000 1011 1000
ORing them would give the value of 696 0010 1011 1000

What do you think is the problem? i am using SDk 1.4.2_05 to generate the binary file, and using MIDP2 to read the file and convert from byte[] to int.

Thanks,
Mike




Mike, the byte values are promoted to int types with sign extention, so whats really going on is:

data[7] contains 02 in hex = 0x00000200 = 0000 ... 0010 0000 0000
data[8] contains B8 in hex = 0xFFFFFFB8 = 1111 ... 1111 1011 1000
or = 0xFFFFFFB8 = 1111 ... 1111 1011 1000 = -72

Hope this helps.
-John
 
Mike Mass
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your reply John, makes perfect sense. For some reason i thought the promotion to int would fill the bits with 0's not 1's in the case of B8.
Do you have any suggestion on how to get around this problem?!

Thanks again for your answer,
Mike
 
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And your bytes with 0xFF to strip off the extended sign bits.
(byte[i] && 0xFF) + ...
 
Mike Mass
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks alot Norm! Problem solved
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic