溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Java實(shí)現(xiàn)的進(jìn)制轉(zhuǎn)換工具類完整示例

發(fā)布時(shí)間:2020-09-02 03:26:43 來(lái)源:腳本之家 閱讀:265 作者:黃寶康 欄目:編程語(yǔ)言

本文實(shí)例講述了Java實(shí)現(xiàn)的進(jìn)制轉(zhuǎn)換工具類。分享給大家供大家參考,具體如下:

import java.nio.charset.Charset;
/**
 * 十六進(jìn)制(簡(jiǎn)寫(xiě)為hex或下標(biāo)16)在數(shù)學(xué)中是一種逢16進(jìn)1的進(jìn)位制,一般用數(shù)字0到9和字母A到F表示(其中:A~F即10~15)。<br>
 * 例如十進(jìn)制數(shù)57,在二進(jìn)制寫(xiě)作111001,在16進(jìn)制寫(xiě)作39。<br>
 * 像java,c這樣的語(yǔ)言為了區(qū)分十六進(jìn)制和十進(jìn)制數(shù)值,會(huì)在十六進(jìn)制數(shù)的前面加上 0x,比如0x20是十進(jìn)制的32,而不是十進(jìn)制的20<br>
 *
 * 參考:https://my.oschina.net/xinxingegeya/blog/287476
 *
 * @author Looly
 *
 */
public class HexKit {
  /**
   * 用于建立十六進(jìn)制字符的輸出的小寫(xiě)字符數(shù)組
   */
  private static final char[] DIGITS_LOWER = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
  /**
   * 用于建立十六進(jìn)制字符的輸出的大寫(xiě)字符數(shù)組
   */
  private static final char[] DIGITS_UPPER = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
  //---------------------------------------------------------------------------------------------------- encode
  /**
   * 將字節(jié)數(shù)組轉(zhuǎn)換為十六進(jìn)制字符數(shù)組
   *
   * @param data byte[]
   * @return 十六進(jìn)制char[]
   */
  public static char[] encodeHex(byte[] data) {
    return encodeHex(data, true);
  }
  /**
   * 將字節(jié)數(shù)組轉(zhuǎn)換為十六進(jìn)制字符數(shù)組
   *
   * @param str 字符串
   * @param charset 編碼
   * @return 十六進(jìn)制char[]
   */
  public static char[] encodeHex(String str, Charset charset) {
    return encodeHex(StrKit.getBytes(str, charset), true);
  }
  /**
   * 將字節(jié)數(shù)組轉(zhuǎn)換為十六進(jìn)制字符數(shù)組
   *
   * @param data byte[]
   * @param toLowerCase <code>true</code> 傳換成小寫(xiě)格式 , <code>false</code> 傳換成大寫(xiě)格式
   * @return 十六進(jìn)制char[]
   */
  public static char[] encodeHex(byte[] data, boolean toLowerCase) {
    return encodeHex(data, toLowerCase ? DIGITS_LOWER : DIGITS_UPPER);
  }
  /**
   * 將字節(jié)數(shù)組轉(zhuǎn)換為十六進(jìn)制字符串
   *
   * @param data byte[]
   * @return 十六進(jìn)制String
   */
  public static String encodeHexStr(byte[] data) {
    return encodeHexStr(data, true);
  }
  /**
   * 將字節(jié)數(shù)組轉(zhuǎn)換為十六進(jìn)制字符串
   *
   * @param data byte[]
   * @param toLowerCase <code>true</code> 傳換成小寫(xiě)格式 , <code>false</code> 傳換成大寫(xiě)格式
   * @return 十六進(jìn)制String
   */
  public static String encodeHexStr(byte[] data, boolean toLowerCase) {
    return encodeHexStr(data, toLowerCase ? DIGITS_LOWER : DIGITS_UPPER);
  }
  //---------------------------------------------------------------------------------------------------- decode
  /**
   * 將十六進(jìn)制字符數(shù)組轉(zhuǎn)換為字符串
   *
   * @param hexStr 十六進(jìn)制String
   * @param charset 編碼
   * @return 字符串
   */
  public static String decodeHexStr(String hexStr, Charset charset) {
    if(StrKit.isEmpty(hexStr)){
      return hexStr;
    }
    return decodeHexStr(hexStr.toCharArray(), charset);
  }
  /**
   * 將十六進(jìn)制字符數(shù)組轉(zhuǎn)換為字符串
   *
   * @param hexData 十六進(jìn)制char[]
   * @param charset 編碼
   * @return 字符串
   */
  public static String decodeHexStr(char[] hexData, Charset charset) {
    return StrKit.str(decodeHex(hexData), charset);
  }
  /**
   * 將十六進(jìn)制字符數(shù)組轉(zhuǎn)換為字節(jié)數(shù)組
   *
   * @param hexData 十六進(jìn)制char[]
   * @return byte[]
   * @throws RuntimeException 如果源十六進(jìn)制字符數(shù)組是一個(gè)奇怪的長(zhǎng)度,將拋出運(yùn)行時(shí)異常
   */
  public static byte[] decodeHex(char[] hexData) {
    int len = hexData.length;
    if ((len & 0x01) != 0) {
      throw new RuntimeException("Odd number of characters.");
    }
    byte[] out = new byte[len >> 1];
    // two characters form the hex value.
    for (int i = 0, j = 0; j < len; i++) {
      int f = toDigit(hexData[j], j) << 4;
      j++;
      f = f | toDigit(hexData[j], j);
      j++;
      out[i] = (byte) (f & 0xFF);
    }
    return out;
  }
  //---------------------------------------------------------------------------------------- Private method start
  /**
   * 將字節(jié)數(shù)組轉(zhuǎn)換為十六進(jìn)制字符串
   *
   * @param data byte[]
   * @param toDigits 用于控制輸出的char[]
   * @return 十六進(jìn)制String
   */
  private static String encodeHexStr(byte[] data, char[] toDigits) {
    return new String(encodeHex(data, toDigits));
  }
  /**
   * 將字節(jié)數(shù)組轉(zhuǎn)換為十六進(jìn)制字符數(shù)組
   *
   * @param data byte[]
   * @param toDigits 用于控制輸出的char[]
   * @return 十六進(jìn)制char[]
   */
  private static char[] encodeHex(byte[] data, char[] toDigits) {
    int l = data.length;
    char[] out = new char[l << 1];
    // two characters form the hex value.
    for (int i = 0, j = 0; i < l; i++) {
      out[j++] = toDigits[(0xF0 & data[i]) >>> 4];
      out[j++] = toDigits[0x0F & data[i]];
    }
    return out;
  }
  /**
   * 將十六進(jìn)制字符轉(zhuǎn)換成一個(gè)整數(shù)
   *
   * @param ch 十六進(jìn)制char
   * @param index 十六進(jìn)制字符在字符數(shù)組中的位置
   * @return 一個(gè)整數(shù)
   * @throws RuntimeException 當(dāng)ch不是一個(gè)合法的十六進(jìn)制字符時(shí),拋出運(yùn)行時(shí)異常
   */
  private static int toDigit(char ch, int index) {
    int digit = Character.digit(ch, 16);
    if (digit == -1) {
      throw new RuntimeException("Illegal hexadecimal character " + ch + " at index " + index);
    }
    return digit;
  }
  //---------------------------------------------------------------------------------------- Private method end
  /**
   * 2進(jìn)制轉(zhuǎn)16進(jìn)制
   * @param bString 2進(jìn)制字符串
   * @return
   */
  public static String binary2Hex(String bString) {
    if (bString == null || bString.equals("") || bString.length() % 8 != 0)
      return null;
    StringBuffer tmp = new StringBuffer();
    int iTmp = 0;
    for (int i = 0; i < bString.length(); i += 4) {
      iTmp = 0;
      for (int j = 0; j < 4; j++) {
        iTmp += Integer.parseInt(bString.substring(i + j, i + j + 1)) << (4 - j - 1);
      }
      tmp.append(Integer.toHexString(iTmp));
    }
    return tmp.toString();
  }
  /**
   * 16進(jìn)制轉(zhuǎn)2進(jìn)制
   * @param hexString
   * @return
   */
  public static String hex2Binary(String hexString) {
    if (hexString == null || hexString.length() % 2 != 0)
      return null;
    String bString = "", tmp;
    for (int i = 0; i < hexString.length(); i++) {
      tmp = "0000" + Integer.toBinaryString(Integer.parseInt(hexString.substring(i, i + 1), 16));
      bString += tmp.substring(tmp.length() - 4);
    }
    return bString;
  }
  /**
   * 將二進(jìn)制轉(zhuǎn)換成16進(jìn)制
   * @param buf
   * @return
   */
  public static String binary2Hex(byte buf[]) {
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < buf.length; i++) {
      String hex = Integer.toHexString(buf[i] & 0xFF);
      if (hex.length() == 1) {
        hex = '0' + hex;
      }
      sb.append(hex.toUpperCase());
    }
    return sb.toString();
  }
  /**
   * 將16進(jìn)制轉(zhuǎn)換為二進(jìn)制
   * @param hexStr
   * @return
   */
  public static byte[] hex2Byte(String hexStr) {
    if (hexStr.length() < 1)
      return null;
    byte[] result = new byte[hexStr.length() / 2];
    for (int i = 0; i < hexStr.length() / 2; i++) {
      int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
      int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16);
      result[i] = (byte) (high * 16 + low);
    }
    return result;
  }
}

PS:這里再為大家推薦幾款本站的在線進(jìn)制轉(zhuǎn)換與計(jì)算工具,相信對(duì)于大家能有所幫助:

在線任意進(jìn)制轉(zhuǎn)換工具:
http://tools.jb51.net/transcoding/hexconvert

在線標(biāo)準(zhǔn)計(jì)算器:
http://tools.jb51.net/jisuanqi/jsq

在線科學(xué)計(jì)算器:
http://tools.jb51.net/jisuanqi/jsqkexue

更多關(guān)于java算法相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》

希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI