string - Java: Unsigned byte to int conversion works perfectly except for 129 (0x81) -


i have weird problem unsigned byte int conversion in java. hex value 0x81 (129 in decimal) gets interpreted wrongly 63. every other value works normally. maybe because bytes stored in string before conversion.

//some string string text=new string(bytearray); //create iterator go through bytes byte[] bytes=text.getbytes(); list<byte> list=new arraylist<byte>(); for(byte i:bytes){ list.add(i); } iterator<byte> it=list.iterator(); while(it.hasnext()){ //bitwise , 0xff remove 2's complement int a=it.next()&0xff; } 

is there way fix this?

solution

//some string, time use char[] fill it. string text=new string(chararray); //create iterator go through bytes char[] chars=text.tochararray(); list<byte> list=new arraylist<byte>(); for(char i:chars){ list.add((byte)i); } iterator<byte> it=list.iterator(); while(it.hasnext()){ //bitwise , 0xff remove 2's complement int a=it.next()&0xff; } 

the byte data type 8-bit signed two's complement integer. has minimum value of -128 , maximum value of 127 (inclusive).


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -