Skip to main content
gamedev.net
🔒 Locked

[java] Converting byte[] to integer (SOLVED)

Started by D3DXVECTOR3 Sep 28, 2005 at 2:58 AM 7 replies 1.6k views
Original Post
D3DXVECTOR3
D3DXVECTOR3
Im reading a binary file and im having some problems. 41 00 BF 02 hex 65 0 191 2 dec 65 0 should give me int 65 but i get 16640 191 2 should give me int 7xx but i get -16638 int x (b[0] << 8 | b[1]) im getting really strange numbers and java byte is from -128 to 127 to the value 191 becomes -65. [Edited by - D3DXVECTOR3 on September 28, 2005 4:11:08 AM]
gregs
gregs
You're not taking into account endianness. Reverse the order of the the bytes, and you'll get what you expect.
Peregrin
Peregrin
See this page for an explanation of endianess and how to handle binary files in Java.
Jooleem. Get addicted.
D3DXVECTOR3
D3DXVECTOR3
its works, thx guys :D

int x = (b[1] & 0xFF) << 8 | (b[0] & 0xFF))
Kevinator
Kevinator
Well C does, not sure about Java.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.