栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

JAVA 十六进制与字符串的转换

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

JAVA 十六进制与字符串的转换

笔者前几日在开服过程中需要将字符串转化成为16进制的字符串,在网上找到了一些方法尝试之后,均发现存在一个问题-->字符串转为16进制后再转回来,英文正常,中文出现乱码

经过考虑决定通过以下方式进行解决: 

  1)在将字符串转为16进制之前先进行一次转化,先将其转化成为Unicode编码(相当于把中文用英文字符代替),在转化成为16进制

  2)相反的,在十六进制转换为字符串后的得到的是Unicode编码,此时再将Unicode编码解码即可获取原始字符串

代码如下:


public static String string2Unicode(String string) {
  StringBuffer unicode = new StringBuffer();
  for (int i = 0; i < string.length(); i++) {
    // 取出每一个字符
 char c = string.charAt(i);
 // 转换为unicode
 unicode.append("\u" + Integer.toHexString(c));
  }
  return unicode.toString();
}

*字符串转为16进制


public static String strTo16(String s) {
 String str = "";
 for (int i = 0; i < s.length(); i++) {
  int ch = (int) s.charAt(i);
  String s4 = Integer.toHexString(ch);
  str = str + s4;
 }
 return str;
}

*16进制转为字符串


public static String hexStringToString(String s) {
 if (s == null || s.equals("")) {
  return null;
 }
 s = s.replace(" ", "");
 byte[] baKeyword = new byte[s.length() / 2];
 for (int i = 0; i < baKeyword.length; i++) {
  try {
   baKeyword[i] = (byte) (0xff & Integer.parseInt(s.substring(i * 2, i * 2 + 2), 16));
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
 try {
  s = new String(baKeyword, "UTF-8");
  new String();
 } catch (Exception e1) {
  e1.printStackTrace();
 }
 return s;
}

*Unicode转为字符串


public static String unicode2String(String unicode) {
 StringBuffer string = new StringBuffer();
 String[] hex = unicode.split("\\u");
 for (int i = 1; i < hex.length; i++) {
  // 转换出每一个代码点
  int data = Integer.parseInt(hex[i], 16);
  // 追加成string
  string.append((char) data);
 }
 return string.toString();
}

此方法虽然解决了转化过程中中文乱码的问题,但是过于复杂,笔者后来又发现一种新的转化方式,可直接转化,中文不乱码,

代码如下:

*字符串转16进制


public static String str2HexStr(String str) {
 char[] chars = "0123456789ABCDEF".toCharArray();
 StringBuilder sb = new StringBuilder("");
 byte[] bs = str.getBytes();
 int bit;
 for (int i = 0; i < bs.length; i++) {
  bit = (bs[i] & 0x0f0) >> 4;
  sb.append(chars[bit]);
  bit = bs[i] & 0x0f;
  sb.append(chars[bit]);
  // sb.append(' ');
 }
 return sb.toString().trim();
}

*16进制转为字符串


public static String hexStr2Str(String hexStr) {
 String str = "0123456789ABCDEF";
 char[] hexs = hexStr.toCharArray();
 byte[] bytes = new byte[hexStr.length() / 2];
 int n;
 for (int i = 0; i < bytes.length; i++) {
  n = str.indexOf(hexs[2 * i]) * 16;
  n += str.indexOf(hexs[2 * i + 1]);
  bytes[i] = (byte) (n & 0xff);
 }
 return new String(bytes);
}

下面是补充

java字符串和十六进制字符串互转

public class HexStringUtils {

 private static final char[] DIGITS_HEX = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D',
   'E', 'F' };

 protected static char[] encodeHex(byte[] data) {
  int l = data.length;
  char[] out = new char[l << 1];
  for (int i = 0, j = 0; i < l; i++) {
   out[j++] = DIGITS_HEx[(0xF0 & data[i]) >>> 4];
   out[j++] = DIGITS_HEX[0x0F & data[i]];
  }
  return out;
 }

 protected static byte[] decodeHex(char[] data) {
  int len = data.length;
  if ((len & 0x01) != 0) {
   throw new RuntimeException("字符个数应该为偶数");
  }
  byte[] out = new byte[len >> 1];
  for (int i = 0, j = 0; j < len; i++) {
   int f = toDigit(data[j], j) << 4;
   j++;
   f |= toDigit(data[j], j);
   j++;
   out[i] = (byte) (f & 0xFF);
  }
  return out;
 }

 protected 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;
 }

 public static String toHex(String str) {
  return new String(encodeHex(str.getBytes()));
 }

 public static String fromHex(String hex) {
  return new String(decodeHex(hex.toCharArray()));
 }

 public static void main(String[] args) {
  String s = "abc你好";
  String hex = toHex(s);
  String decode = fromHex(hex);
  System.out.println("原字符串:" + s);
  System.out.println("十六进制字符串:" + hex);
  System.out.println("还原:" + decode);
 }
}

toHexString

public static String toHexString(int i)以十六进制的无符号整数形式返回一个整数参数的字符串表示形式。
如果参数为负,那么无符号整数值为参数加上 232;否则等于该参数。将该值转换为十六进制(基数 16)的无前导 0 的 ASCII 数字字符串。如果无符号数的大小值为零,则用一个零字符 '0' ('u0030') 表示它;否则,无符号数大小的表示形式中的第一个字符将不是零字符。用以下字符作为十六进制数字:

0123456789abcdef

这些字符的范围是从 'u0030' 到 'u0039' 和从 'u0061' 到 'u0066'。如果希望得到大写字母,可以在结果上调用 String.toUpperCase() 方法:

Integer.toHexString(n).toUpperCase()

参数:
i - 要转换成字符串的整数。

返回:

用十六进制(基数 16)参数表示的无符号整数值的字符串表示形式。

// 转化字符串为十六进制编码 
public static String toHexString(String s) 
{ 
String str=""; 
for (int i=0;i>4)); 
sb.append(hexString.charAt((bytes[i]&0x0f)>>0)); 
} 
return sb.toString(); 
} 
 
public static String decode(String bytes) 
{ 
ByteArrayOutputStream baos=new ByteArrayOutputStream(bytes.length()/2); 
//将每2位16进制整数组装成一个字节 
for(int i=0;i

第二种方法:

将指定byte数组以16进制的形式打印到控制台

package com.nantian.iclient.atm.sdb; 

public class Util { 
public Util() { 
} 

 
public static void printHexString(String hint, byte[] b) { 
System.out.print(hint); 
for (int i = 0; i < b.length; i++) { 
String hex = Integer.toHexString(b[i] & 0xFF); 
if (hex.length() == 1) { 
hex = '0' + hex; 
} 
System.out.print(hex.toUpperCase() + " "); 
} 
System.out.println(""); 
} 

 
public static String Bytes2HexString(byte[] b) { 
String ret = ""; 
for (int i = 0; i < b.length; i++) { 
String hex = Integer.toHexString(b[i] & 0xFF); 
if (hex.length() == 1) { 
hex = '0' + hex; 
} 
ret += hex.toUpperCase(); 
} 
return ret; 
} 

 
public static byte uniteBytes(byte src0, byte src1) { 
byte _b0 = Byte.decode("0x" + new String(new byte[]{src0})).bytevalue(); 
_b0 = (byte)(_b0 << 4); 
byte _b1 = Byte.decode("0x" + new String(new byte[]{src1})).bytevalue(); 
byte ret = (byte)(_b0 ^ _b1); 
return ret; 
} 

 
public static byte[] HexString2Bytes(String src){ 
byte[] ret = new byte[8]; 
byte[] tmp = src.getBytes(); 
for(int i=0; i<8; i++){ 
ret[i] = uniteBytes(tmp[i*2], tmp[i*2+1]); 
} 
return ret; 
} 

} 

以上就是JAVA 十六进制与字符串的转换的详细内容,更多关于JAVA 十六进制的资料请关注考高分网其它相关文章!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/153530.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号