【问题标题】:Adding two data types in bytearrayoutputstream and printing it在 bytearrayoutputstream 中添加两种数据类型并打印
【发布时间】: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


【解决方案1】:

480的ASCII码

 int x=b[0]-48;

【讨论】:

    猜你喜欢
    • 2018-10-19
    • 1970-01-01
    • 2018-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-28
    • 2022-10-16
    • 1970-01-01
    相关资源
    最近更新 更多