0

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

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?

Cherry
  • 31,309
  • 66
  • 224
  • 364

4 Answers4

0

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. E.g CA is easily seen as 12, 10 or 11001010 in binary). 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).

WJS
  • 36,363
  • 4
  • 24
  • 39
0

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. E.g CA is easily seen as 12, 10 or 11001010 in binary). 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).

WJS
  • 36,363
  • 4
  • 24
  • 39
0
  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 
0

In Java, byte is a signed, and have range -128 to +127. So there is no such thing as "positive bytes" in Java.

However, if you format a (signed) byte in hexadecimal using a x format specifier (e.g. String.format("%02x", someByte)), it will display it as a value in the range 00 to FF.

See https://stackoverflow.com/a/2817883/139985 for information on formatting bytes in hexadecimal.


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.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216