2017-11-25 150 views
-2

我已经使用了下面的代码。我似乎无法从字节数组中得到x的值。 这里是我的代码:在bytearrayoutputstream中添加两种数据类型并打印它

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.请帮

+0

“我似乎无法从字节数组中得到x的值“。是的,你做了......你拿走了X并从字节数组中取回了数值。这是48.为什么产量应该为零? – IMustBeSomeone

+0

简单地说,x = b [0] b [0] = 48.无论你的字节数组是什么,都是问题所在。 – IMustBeSomeone

+0

它也可以encodi ---谢谢mustabel。 – IMustBeSomeone

回答

0

480

的ASCII码
int x=b[0]-48;