0

Java で unsigned byte から int への変換に奇妙な問題があります。16 進値 0x81 (10 進数で 129) は常に 63 として誤って解釈されます。他のすべての値は正常に機能します。おそらくこれは、変換前にバイトが文字列に格納されているためです。

//some string
String text=new String(bytearray);
//create an iterator to go through the 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 AND 0xFF to remove 2's complement
int a=it.next()&0xFF;
}

これを修正する方法はありますか?

解決

//some string, this time I use a char[] to fill it.
String text=new String(chararray);
//create an iterator to go through the 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 AND 0xFF to remove 2's complement
int a=it.next()&0xFF;
}
4

1 に答える 1