我是靠谱客的博主 幸福西装,最近开发中收集的这篇文章主要介绍字节传换,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

package cn.xxxxx.tools;

import java.io.ByteArrayOutputStream;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TranscodeUtil
{
  public static String strToUnicodeStr(String str)
  {
    StringBuffer buffer = new StringBuffer();
    for (int i = 0; i < str.length(); i++)
    {
      char ch = str.charAt(i);
      Character.UnicodeBlock ub = Character.UnicodeBlock.of(ch);
      if (ub == Character.UnicodeBlock.BASIC_LATIN) {
        buffer.append(ch);
      } else if (ch > 'ÿ') {
        buffer.append("\u" + Integer.toHexString(ch));
      } else {
        buffer.append("\" + Integer.toHexString(ch));
      }
    }
    return buffer.toString();
  }
  

  public static String strToBase64Str(String str)
  {
    return new String(encode(str.getBytes()));
  }
  
  public static String base64StrToStr(String base64Str)
  {
    char[] dataArr = new char[base64Str.length()];
    base64Str.getChars(0, base64Str.length(), dataArr, 0);
    return new String(decode(dataArr));
  }
  
  public static String byteArrayToBase64Str(byte[] byteArray)
  {
    return new String(encode(byteArray));
  }
  
  public static byte[] base64StrToByteArray(String base64Str)
  {
    char[] dataArr = new char[base64Str.length()];
    base64Str.getChars(0, base64Str.length(), dataArr, 0);
    return decode(dataArr);
  }
  
  private static char[] encode(byte[] data)
  {
    char[] out = new char[(data.length + 2) / 3 * 4];
    int i = 0;
    for (int index = 0; i < data.length; index += 4)
    {
      boolean quad = false;
      boolean trip = false;
      int val = 0xFF & data[i];
      val <<= 8;
      if (i + 1 < data.length)
      {
        val |= 0xFF & data[(i + 1)];
        trip = true;
      }
      val <<= 8;
      if (i + 2 < data.length)
      {
        val |= 0xFF & data[(i + 2)];
        quad = true;
      }
      out[(index + 3)] = alphabet[64];
      val >>= 6;
      out[(index + 2)] = alphabet[64];
      val >>= 6;
      out[(index + 1)] = alphabet[(val & 0x3F)];
      val >>= 6;
      out[(index + 0)] = alphabet[(val & 0x3F)];i += 3;
    }
    return out;
  }
  
  private static byte[] decode(char[] data)
  {
    int len = (data.length + 3) / 4 * 3;
    if ((data.length > 0) && (data[(data.length - 1)] == '=')) {
      len--;
    }
    if ((data.length > 1) && (data[(data.length - 2)] == '=')) {
      len--;
    }
    byte[] out = new byte[len];
    int shift = 0;
    int accum = 0;
    int index = 0;
    for (int ix = 0; ix < data.length; ix++)
    {
      int value = codes[(data[ix] & 0xFF)];
      if (value >= 0)
      {
        accum <<= 6;
        shift += 6;
        accum |= value;
        if (shift >= 8)
        {
          shift -= 8;
          out[(index++)] = ((byte)(accum >> shift & 0xFF));
        }
      }
    }
    if (index != out.length) {
      throw new Error("miscalculated data length!");
    }
    return out;
  }
  
  private static char[] alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="
    .toCharArray();
  private static byte[] codes = new byte[256];
  
  static
  {
    for (int i = 0; i < 256; i++) {
      codes[i] = -1;
    }
    for (int i = 65; i <= 90; i++) {
      codes[i] = ((byte)(i - 65));
    }
    for (int i = 97; i <= 122; i++) {
      codes[i] = ((byte)(26 + i - 97));
    }
    for (int i = 48; i <= 57; i++) {
      codes[i] = ((byte)(52 + i - 48));
    }
    codes[43] = 62;
    codes[47] = 63;
  }
  
  private static String hexString = "0123456789ABCDEF";
  
  public static String strToHexStr(String str)
  {
    byte[] bytes = str.getBytes();
    StringBuilder sb = new StringBuilder(bytes.length * 2);
    for (int i = 0; i < bytes.length; i++)
    {
      sb.append(hexString.charAt((bytes[i] & 0xF0) >> 4));
      sb.append(hexString.charAt((bytes[i] & 0xF) >> 0));
    }
    return sb.toString();
  }
  
  public static String hexStrToStr(String hexStr)
  {
    ByteArrayOutputStream baos = new ByteArrayOutputStream(
      hexStr.length() / 2);
    for (int i = 0; i < hexStr.length(); i += 2) {
      baos.write(hexString.indexOf(hexStr.charAt(i)) << 4 | hexString
        .indexOf(hexStr.charAt(i + 1)));
    }
    return new String(baos.toByteArray());
  }
  
  public static String byteArrayToHexStr(byte[] byteArray)
  {
    StringBuffer buffer = new StringBuffer(byteArray.length * 2);
    for (int i = 0; i < byteArray.length; i++)
    {
      if ((byteArray[i] & 0xFF) < 16) {
        buffer.append("0");
      }
      buffer.append(Long.toString(byteArray[i] & 0xFF, 16));
    }
    return buffer.toString();
  }
  
  public static byte[] hexStrToByteArray(String hexStr)
  {
    if (hexStr.length() < 1) {
      return null;
    }
    byte[] encrypted = 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);
      encrypted[i] = ((byte)(high * 16 + low));
    }
    return encrypted;
  }
  
  public static String strToBinStr(String str)
  {
    char[] chars = str.toCharArray();
    StringBuffer result = new StringBuffer();
    for (int i = 0; i < chars.length; i++)
    {
      result.append(Integer.toBinaryString(chars[i]));
      result.append(" ");
    }
    return result.toString();
  }
  
  public static String binStrToStr(String binStr)
  {
    String[] tempStr = strToStrArray(binStr);
    char[] tempChar = new char[tempStr.length];
    for (int i = 0; i < tempStr.length; i++) {
      tempChar[i] = binstrToChar(tempStr[i]);
    }
    return String.valueOf(tempChar);
  }
  
  private static char binstrToChar(String binStr)
  {
    int[] temp = binstrToIntArray(binStr);
    int sum = 0;
    for (int i = 0; i < temp.length; i++) {
      sum += (temp[(temp.length - 1 - i)] << i);
    }
    return (char)sum;
  }
  
  private static String[] strToStrArray(String str)
  {
    return str.split(" ");
  }
  
  private static int[] binstrToIntArray(String binStr)
  {
    char[] temp = binStr.toCharArray();
    int[] result = new int[temp.length];
    for (int i = 0; i < temp.length; i++) {
      temp[i] -= '0';
    }
    return result;
  }
}

 

最后

以上就是幸福西装为你收集整理的字节传换的全部内容,希望文章能够帮你解决字节传换所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(59)

评论列表共有 0 条评论

立即
投稿
返回
顶部