我正在尝试确定内部方法是否能够在给定不同编码的情况下正确解码字节数组。以下代码是我如何生成要编码的数据。
public class Encoding {
static byte[] VALUES = {(byte) 0x00, ..... (byte) 0xFF};
static String[] ENCODING = {"Windows-1252","ISO-8859-1"};
public static void main(String[] args) throws UnsupportedEncodingException {
for(String encode : ENCODING) {
for(byte value : VALUES) {
byte[] inputByte = new byte[]{value};
String input = new String(inputByte, encode);
String houseInput = houseMethod(input.getBytes());
}
}
}
}
我的问题是调用 house 方法时,它会向该方法发送什么编码?据我了解,当 Java 存储字符串时,会将其转换为 UTF-16。那么当我发送 Input.getBytes() 时,它发送的是 UTF-16 编码字节还是我在创建新字符串时设置的编码方案?我猜它是 UTF-16,但我不确定。房子的方法应该是???
houseMethod(input.getBytes(encode))
最佳答案
参见 String.getBytes():
Encodes this String into a sequence of bytes using the platform's default charset, storing the result into a new byte array.
建议您改用 String.getBytes(Charset) 方法并显式指定所需的编码。
关于java - 了解 Java 编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32505256/