【发布时间】:2017-11-25 20:02:16
【问题描述】:
我使用了以下代码。我似乎无法从字节数组中取回 x 的值。 这是我的代码:
int seqNo = 0;
ByteArrayOutputStream bout = new ByteArrayOutputStream();
DataOutputStream out = new DataOutputStream(bout);
try {
out.writeInt(seqNo);
String i = Integer.toString(seqNo) + "hello";
byte[] b = i.getBytes();
System.out.println(Arrays.toString(b));
int x = b[0];
System.out.println(x);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
这是输出:
[48, 104, 101, 108, 108, 111]
48
输出应该包含 0 而不是 48。请帮助
【问题讨论】:
-
您想要
char[]而不是byte[]? -
我想要一个 0。我收到 48
-
String.getBytes()使用用户的默认字符编码,这不是您想要的,除非它正是您想要的。使用其他String.getBytes(xxxx)之一,例如StandardCharsets.UTF-8。 -
你能确定这个数字只有一位小数吗?每一步倒过来有什么坏处吗?
标签: java arrays bytearrayoutputstream