这是一个python代码版本:
def decode(s):
for i in range(len(s)):
print compat_ord(s[i])
def compat_ord(c):
if type(c) is int:
return c
else:
return ord(c)
decode(base64.b64decode('NwXYSw8YI7nb2PnE8eJxVoLzuBQ81wjOXh4='.encode('ascii')))
产量
55
5
216
75
15
24
35
185
219
216
249
196
241
226
113
86
130
243
184
20
60
215
8
206
94
30
我的Java版本
byte[] s = Base64.getDecoder().decode("NwXYSw8YI7nb2PnE8eJxVoLzuBQ81wjOXh4=".getBytes("ascii"));
for(int i= 0;i
System.out.println(s[i]);
}
Ouptput
55
5
-40
75
15
24
35
-71
-37
-40
-7
-60
-15
-30
113
86
-126
-13
-72
20
60
-41
8
-50
94
30
我的问题
为什么有些值是相同的而有些则不是
解决方法:
Java中的字节是8位有符号的.所以你会得到负值.
更改
System.out.println(s[i]);
至
System.out.println(s[i]&0xff);
获得相同的价值观.
更新:我刚看到Java 8引入了Byte.toUnsignedInt().这可能更具可读性:
System.out.println(Byte.toUnsignedInt(s[i]));
标签:python,java
来源: https://codeday.me/bug/20190608/1197958.html