十六进制到 Base64 转换
Hex to Base64 Conversion
我尝试按照网络上的几个示例将 Hex 值转换为 Base64,但失败了。
http://tomeko.net/online_tools/hex_to_base64.php?lang=en
我需要比较 AWS 对象的 eTag 值和 Google Cloud Storage MD5 值。
base64(eTag)=GCS_MD5
。
eTag:6a95b4dd5419f2ffb9f655309c931cb0
和 MD5:apW03VQZ8v+59lUwnJMcsA==
如何将 Hex 转换为 Base64?
我尝试了 stack-overflow 中的各种示例,但仍然无法做到。
public static void main(String[] args) throws IOException {
String hexadecimal = "6a95b4dd5419f2ffb9f655309c931cb0";
System.out.println("hexadecimal: " + hexadecimal);
String binaryNum = hexToBin(hexadecimal);
System.out.println("" + binaryNum + ", length:" + binaryNum.length());
byte[] encoded = Base64.encodeBase64(binaryNum.getBytes());
byte[] decoded = Base64.decodeBase64(binaryNum.getBytes());
System.out.println("encoded: " + Base64.isBase64(encoded));
System.out.println("decoded: " + Base64.isBase64(decoded));
System.out.println(Arrays.toString(encoded));
String encodedString = new String(encoded);
System.out.println(binaryNum + " = " + encodedString);
String decodedString = new String(decoded);
System.out.println(binaryNum + " = " + decodedString);
System.out.println("ByteEncoding::" + base64Encode(binaryNum.getBytes()));
System.out.println("ByteDecoding::" + base64Decode(binaryNum));
}
使用您似乎正在使用的相同库(假定 Apache Commons Base64,因为标准 Base64 class 有其他方法),这里是一些将十六进制字符串转换为(最终) base64 字符串,并检查它是否是 base64 编码的。它转换为您提供的相同 base64 值,并且链接的转换器输出:
import java.math.BigInteger;
import org.apache.commons.codec.binary.Base64;
public class Main {
public static void main(String... args) {
String hexadecimal = "6a95b4dd5419f2ffb9f655309c931cb0";
System.out.println("hexadecimal: " + hexadecimal);
BigInteger bigint = new BigInteger(hexadecimal, 16);
StringBuilder sb = new StringBuilder();
byte[] ba = Base64.encodeInteger(bigint);
for (byte b : ba) {
sb.append((char)b);
}
String s = sb.toString();
System.out.println("base64: " + s);
System.out.println("encoded: " + Base64.isBase64(s));
}
}
//Output:
//hexadecimal: 6a95b4dd5419f2ffb9f655309c931cb0
//base64: apW03VQZ8v+59lUwnJMcsA==
//encoded: true
如果您没有导入 Apache Base64,这可能会导致问题。它是一个外部库,因此您必须从 here 下载它,将它添加到您的 IDE 中的项目,然后按照上面的代码导入。
byte[] decodedHex = Hex.decodeHex(hex);
byte[] encodedHexB64 = Base64.codeBase64(decodedHex);
来自 here
我尝试按照网络上的几个示例将 Hex 值转换为 Base64,但失败了。
http://tomeko.net/online_tools/hex_to_base64.php?lang=en
我需要比较 AWS 对象的 eTag 值和 Google Cloud Storage MD5 值。
base64(eTag)=GCS_MD5
。
eTag:6a95b4dd5419f2ffb9f655309c931cb0
和 MD5:apW03VQZ8v+59lUwnJMcsA==
如何将 Hex 转换为 Base64?
我尝试了 stack-overflow 中的各种示例,但仍然无法做到。
public static void main(String[] args) throws IOException {
String hexadecimal = "6a95b4dd5419f2ffb9f655309c931cb0";
System.out.println("hexadecimal: " + hexadecimal);
String binaryNum = hexToBin(hexadecimal);
System.out.println("" + binaryNum + ", length:" + binaryNum.length());
byte[] encoded = Base64.encodeBase64(binaryNum.getBytes());
byte[] decoded = Base64.decodeBase64(binaryNum.getBytes());
System.out.println("encoded: " + Base64.isBase64(encoded));
System.out.println("decoded: " + Base64.isBase64(decoded));
System.out.println(Arrays.toString(encoded));
String encodedString = new String(encoded);
System.out.println(binaryNum + " = " + encodedString);
String decodedString = new String(decoded);
System.out.println(binaryNum + " = " + decodedString);
System.out.println("ByteEncoding::" + base64Encode(binaryNum.getBytes()));
System.out.println("ByteDecoding::" + base64Decode(binaryNum));
}
使用您似乎正在使用的相同库(假定 Apache Commons Base64,因为标准 Base64 class 有其他方法),这里是一些将十六进制字符串转换为(最终) base64 字符串,并检查它是否是 base64 编码的。它转换为您提供的相同 base64 值,并且链接的转换器输出:
import java.math.BigInteger;
import org.apache.commons.codec.binary.Base64;
public class Main {
public static void main(String... args) {
String hexadecimal = "6a95b4dd5419f2ffb9f655309c931cb0";
System.out.println("hexadecimal: " + hexadecimal);
BigInteger bigint = new BigInteger(hexadecimal, 16);
StringBuilder sb = new StringBuilder();
byte[] ba = Base64.encodeInteger(bigint);
for (byte b : ba) {
sb.append((char)b);
}
String s = sb.toString();
System.out.println("base64: " + s);
System.out.println("encoded: " + Base64.isBase64(s));
}
}
//Output:
//hexadecimal: 6a95b4dd5419f2ffb9f655309c931cb0
//base64: apW03VQZ8v+59lUwnJMcsA==
//encoded: true
如果您没有导入 Apache Base64,这可能会导致问题。它是一个外部库,因此您必须从 here 下载它,将它添加到您的 IDE 中的项目,然后按照上面的代码导入。
byte[] decodedHex = Hex.decodeHex(hex);
byte[] encodedHexB64 = Base64.codeBase64(decodedHex);
来自 here