简体   繁体   English

如何将 base64 字符串解码为 java 中的正字节或十六进制?

[英]How decode base64 string to positive bytes or hexadecimal in java?

Consider just simple code:只考虑简单的代码:

String base64 = "psgEYvQBtGAjFns=";
byte[] bytes = BaseEncoding.base64().decode(base64);

Gives:给出:

[-90, -56, 4, 98, -12, 1, -76, 96, 35, 22, 123]

While the same string online ( https://cryptii.com/pipes/base64-to-hex ) decoded as而在线相同的字符串 ( https://cryptii.com/pipes/base64-to-hex ) 解码为

a6 c8 04 62 f4 01 b4 60 23 16 7b

So hexadecimal number are positive.所以十六进制数都是正数。 Why?为什么? How to decode base64 with java to positive bytes and/or hexadecimals?如何将 base64 与 java 解码为正字节和/或十六进制?

Here is how to print a byte as unsigned.以下是如何将字节打印为无符号。

byte[] bytes = new byte[]{-90, -56, 4, 98, -12, 1, -76, 96, 35, 22, 123};
for (byte b : bytes) {
    System.out.print((b&0xFF) + " ");
}

prints印刷

166 200 4 98 244 1 180 96 35 22 123 

Decimal numbers are normally printed as signed values (the high order bit is set).十进制数通常打印为带符号的值(设置高位)。 Where as hexadecimal is usually printed as unsigned to, imo, show the structure of the value.十六进制通常打印为无符号,imo,显示值的结构。 Eg CA is easily seen as 12, 10 or 11001010 in binary).例如,CA 在二进制中很容易被视为 12、10 或 11001010)。 Also, I wouldn't find it very useful to show a byte FF as -1 if I wanted it in hex (even though 1 is a valid hex digit).另外,如果我想用十六进制显示一个字节 FF 为 -1,我会发现它不是很有用(即使 1 是一个有效的十六进制数字)。

Here is how to print a byte as unsigned.以下是如何将字节打印为无符号。

byte[] bytes = new byte[]{-90, -56, 4, 98, -12, 1, -76, 96, 35, 22, 123};
for (byte b : bytes) {
    System.out.print((b&0xFF) + " ");
}

prints印刷

166 200 4 98 244 1 180 96 35 22 123 

Decimal numbers are normally printed as signed values (the high order bit is set).十进制数通常打印为带符号的值(设置高位)。 Where as hexadecimal is usually printed as unsigned to, imo, show the structure of the value.十六进制通常打印为无符号,imo,显示值的结构。 Eg CA is easily seen as 12, 10 or 11001010 in binary).例如,CA 在二进制中很容易被视为 12、10 或 11001010)。 Also, I wouldn't find it very useful to show a byte FF as -1 if I wanted it in hex (even though 1 is a valid hex digit).另外,如果我想用十六进制显示一个字节 FF 为 -1,我会发现它不是很有用(即使 1 是一个有效的十六进制数字)。

  public static void main(String[] args) {
String base64 = "psgEYvQBtGAjFns=";
byte[] bytes = BaseEncoding.base64().decode(base64);
for (byte data : bytes) {
  System.out.print(StringUtils.leftPad(
      Integer.toHexString(Byte.toUnsignedInt(data)),2,"0")+ " ");
}

} }

Result: 

   a6 c8 04 62 f4 01 b4 60 23 16 7b 

In Java, byte is a signed, and have range -128 to +127.在 Java 中, byte是有符号的,范围为 -128 到 +127。 So there is no such thing as "positive bytes" in Java.所以Java中没有所谓的“正字节”。

However, if you format a (signed) byte in hexadecimal using a x format specifier (eg String.format("%02x", someByte) ), it will display it as a value in the range 00 to FF .但是,如果您使用x格式说明符(例如String.format("%02x", someByte) )将(带符号的)字节格式化为十六进制,它将显示为00FF范围内的值。

See https://stackoverflow.com/a/2817883/139985 for information on formatting bytes in hexadecimal.有关以十六进制格式化字节的信息,请参阅https://stackoverflow.com/a/2817883/139985


So hexadecimal number are positive.所以十六进制数都是正数。 Why?为什么?

What you are really commenting on is the convention used by that particular converter site.您真正要评论的是该特定转换器站点使用的约定。 The site has chosen to use an unsigned representation... because that is what is most commonly used by programmers.该站点选择使用无符号表示...因为这是程序员最常使用的。 However it is just a convention.然而,这只是一个惯例。 They could have chosen signed hexadecimal.他们本可以选择带符号的十六进制。

In the general sense, hexadecimal is a notation not a representation.在一般意义上,十六进制是一种表示法,而不是一种表示法。 It can be either signed or unsigned, depending on the context.根据上下文,它可以是有符号的或无符号的。 (Just like decimal can be signed or unsigned, depending on the context.) (就像小数可以有符号或无符号一样,具体取决于上下文。)

By contrast byte , short , int , long are Java types which have specific value ranges, and specific binary representations for those values.相比之下, byteshortintlong是 Java 类型,它们具有特定的值范围,以及这些值的特定二进制表示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM