如何将十六进制转换为base64

新手上路,请多包涵

如何将十六进制字符串转换为 base64?我发现这个页面 http://home2.paulschou.net/tools/xlate/ 但我需要一些 java 函数: String base64 = ...decoder(String hex); 我在互联网上找到了一些东西,但它们很难使用字节数组。我正在寻找更简单的东西,非常感谢

原文由 hudi 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.6k
2 个回答

看看 Commons 编解码器

  import org.apache.commons.codec.binary.Base64;
 import org.apache.commons.codec.binary.Hex;

byte[] decodedHex = Hex.decodeHex(hex);
byte[] encodedHexB64 = Base64.encodeBase64(decodedHex);

原文由 Victor Martinez 发布,翻译遵循 CC BY-SA 4.0 许可协议

首先你必须导入 Apache Commons Codec 库! https://commons.apache.org/proper/commons-codec/archives/1.9/index.html

这是 1.9 版本的 API http://commons.apache.org/proper/commons-codec/archives/1.9/apidocs/index.html

那么你必须按照这3个步骤

       //convert String to char array (1st step)
      char[] charArray = myhexString.toCharArray();

      // decode the char array to byte[] (2nd step)
      byte[] decodedHex = Hex.decodeHex(charArray);

    // The String decoded to Base64 (3rd step)
    String result= Base64.encodeBase64String(decodedHex);

原文由 gtopal 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题