私はいくつかの文字列 -> バイト -> バイナリ コードをいじりましたが、自分のコードを任意の byte[] 配列で動作させたいのですが、現在は動作するのは ascii だけですか?
中国語は動作しません。
String message =" 汉语";
playingWithFire(message.getBytes());
while String wow = "WOW..."; 動作します:(すべてのutf-8形式で動作するようにしたいのですが、どうすればそれができるかについての指針はありますか?
//ありがとう
public static byte[] playingWithFire(byte[] bytes){
byte[] newbytes = null;
newbytes = new byte[bytes.length];
for(int i = 0; i < bytes.length; i++){
String tempStringByte = String.format("%8s", Integer.toBinaryString(bytes[i] & 0xFF)).replace(' ', '0');
StringBuffer newByteBrf = null;
newByteBrf = new StringBuffer();
for(int x = 0; x < tempStringByte.length(); x++){
newByteBrf.append(tempStringByte.charAt(x));
}
/*short a = Short.parseShort(newByteBrf.toString(), 2);
ByteBuffer bytesads = ByteBuffer.allocate(2).putShort(a);
newbytes[i] = bytesads.get();
cause: java.nio.BufferUnderflowException
*/
//cause: java.lang.NumberFormatException: Value out of range.
newbytes[i] = Byte.parseByte(newByteBrf.toString(), 2);
}
return newbytes;
}