1. 简介:

Hex编码是一种用16个字符表示任意二进制数据的方法。它是一种编码,而非加密
码表为 "0123456789ABCDEF" 这16个字符。可以自行修改为改编版本。
字符编码
URL编码

2. Hex编码的代码实现和码表

  • java 中:

    public static void main(String[] args) {
      String name = "小肩膀";
      byte[] bytes = name.getBytes(StandardCharsets.UTF_8);
      System.out.println(bytes.length);
      System.out.println(Arrays.toString(bytes));
      String encode = HexBin.encode(bytes);
      String encode1= encode(bytes);
      System.out.println(encode);
      System.out.println(encode1);
      }
  • Android 中:

    • 配置: app-> build.gradle--> dependencies 中增加如下

      api "com.squareup.okhttp3:okhttp:3.10.0"
    • 代码

      String name = "小肩膀";
      byte[] bytes = name.getBytes(StandardCharsets.UTF_8);
      ByteString byteString = ByteString.of(bytes);
      System.out.println(byteString.hex());

    Hex编码特点

    a) 用0-9 a-f 16个字符表示。
    b) 每个十六进制字符代表4bit, 也就是2个十六进制字符代表一个字节。
    c) 在实际应用中,比如密钥初始化,一定要分清楚传进去的密钥是哪种编码的,采用对应 方式解析,才能得到正确的结果
    d) 编程中很多问题,需要从字节甚至二进制位的角度去考虑,才能明白


永乐
23 声望7 粉丝

目前文章属于笔记类型,暂未整理。 后期会系统化整理。