字符串为二进制,反之亦然:扩展ASCII [英] String to binary and vice versa: extended ASCII
问题描述
我想通过把它在一个字节数组( String.getBytes []
)将字符串转换为二进制,然后存储二进制字符串的每个字节(在String [] Integer.toBinaryString(字节阵列)
)。然后我想通过的Byte.parseByte转换回正常的字符串(字符串数组[我],2)
。本标准ASCII码表的伟大工程,但不能用于扩展之一。例如, A
给我 1000001
,而在 A
返回
11111111111111111111111111000011
11111111111111111111111110000100
任何想法如何管理呢?
公共类BinString {
公共静态无效的主要(字符串ARGS []){
字符串s =A;
的System.out.println(binToString(stringToBin(S))); } 公共静态的String [] stringToBin(String s)将{
的System.out.println(转换:+ S);
字节[] B = s.getBytes();
的String [] SA =新的String [s.getBytes()的长度。]
的for(int i = 0; I< b.length个;我++){
SA [I] = Integer.toBinaryString(B [1] - 安培; 0xFF的);
}
返回SA;
} 公共静态字符串binToString(字符串[] STRAR){
字节[]巴=新的字节[strar.length]
的for(int i = 0; I< strar.length;我++){
酒吧[I] =的Byte.parseByte(STRAR [I],2);
的System.out.println(的Byte.parseByte(STRAR [I],2)); }
字符串s =新的String(巴);
返回S;
}}
第一关:扩展ASCII是的用来指一吨不同编码的一个非常误导的标题
二:字节
在Java中签,而在编码字节通常为unsigned处理。由于您使用 Integer.toBinaryString()
的字节
将被转换为 INT
使用符号扩展(因为字节值> 127将重新$ p $由负值在Java中psented)。
要避免这种情况只需使用&安培; 0xFF的
来掩盖一切,但低8位是这样的:
字符串二进制= Integer.toBinaryString(的字节数组[1] - 安培; 0xFF的);
I want to convert a String to binary by putting it in a byte array (String.getBytes[]
) and then store the binary string for each byte (Integer.toBinaryString(bytearray)
) in a String[]. Then I want to convert back to normal String via Byte.parseByte(stringarray[i], 2)
. This works great for standard ASCII-Table, but not for the extended one. For example, an A
gives me 1000001
, but an Ä
returns
11111111111111111111111111000011
11111111111111111111111110000100
Any ideas how to manage this?
public class BinString {
public static void main(String args[]) {
String s = "ä";
System.out.println(binToString(stringToBin(s)));
}
public static String[] stringToBin(String s) {
System.out.println("Converting: " + s);
byte[] b = s.getBytes();
String[] sa = new String[s.getBytes().length];
for (int i = 0; i < b.length; i++) {
sa[i] = Integer.toBinaryString(b[i] & 0xFF);
}
return sa;
}
public static String binToString(String[] strar) {
byte[] bar = new byte[strar.length];
for (int i = 0; i < strar.length; i++) {
bar[i] = Byte.parseByte(strar[i], 2);
System.out.println(Byte.parseByte(strar[i], 2));
}
String s = new String(bar);
return s;
}
}
First off: "extended ASCII" is a very misleading title that's used to refer to a ton of different encodings.
Second: byte
in Java is signed, while bytes in encodings are usually handled as unsigned. Since you use Integer.toBinaryString()
the byte
will be converted to an int
using sign extension (because byte values > 127 will be represented by negative values in Java).
To avoid this simply use & 0xFF
to mask all but the lower 8 bit like this:
String binary = Integer.toBinaryString(byteArray[i] & 0xFF);
这篇关于字符串为二进制,反之亦然:扩展ASCII的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!