更新时间:2022-06-11 05:34:42
从字符串转换为字节[]:
Convert from String to byte[]:
String s = "some text here";
byte[] b = s.getBytes(StandardCharsets.UTF_8);
从字节[]转换为字符串:
Convert from byte[] to String:
byte[] b = {(byte) 99, (byte)97, (byte)116};
String s = new String(b, StandardCharsets.US_ASCII);
当然,您应该使用正确的编码名称.我的示例使用了 US-ASCII 和 UTF-8,这两种最常见的编码.
You should, of course, use the correct encoding name. My examples used US-ASCII and UTF-8, the two most common encodings.