汉字字符串转换成十六进制byte数组,一个汉字存到两个byte里面,大整数存到两个byte里面
package com.zdz8207.test;
/**
* 汉字字符串转换成十六进制byte数组,一个汉字存到两个byte里面,大整数存到两个byte里面
* @author zhengdunzhuang
*
*/
public class ChineseCharacterStringToByteArray {
public static void main(String[] args) {
byte[] data = new byte[42];
data[0] = (byte) 0x04;
data[1] = (byte) (42);
printHexString(data);
System.out.println("");
String msg = "你已成功添加李先生为好友";
data = toHexByteByStrings(data,3,msg);
printHexString(data);
/*
* 用工具转换需要的结果:
你已成功添加李先生为好友
{0x4f60,0x5df2,0x6210,0x529f,0x6dfb,0x52a0,0x674e,0x5148,0x751f,0x4e3a,0x597d,0x5 3cb,0x0};
打印输出结果:
042A00000000000000000000000000000000000000000000000000000000000000000000000000000000
042A4F605DF26210529F6DFB52A0674E5148751F4E3A597D53CB00000000000000000000000000000000
4F60
*/
int i = 20320;//0x4f60
byte[] b = intToByteArray(i);
System.out.println("");
printHexString(b);
}
//整数转换成byte数组
public static byte[] intToByteArray(int i) {
byte[] result = new byte[2];
result[0] = (byte) ((i & 0xFF00) >> 8 );
result[1] = (byte) ( i & 0xFF);
return result;
}
/**
int j = start;
for (int i = 0; i < s.length(); i++) {
int ch = (int) s.charAt(i);
data[j-1] = (byte) ((ch & 0xFF00) >> 8 );
data[j] = (byte) ( ch & 0xFF);
j += 2;
}
return data;
}
// 转化字符串为十六进制编码字符串
public static String toHexByStrings(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;// 0x表示十六进制
}
//打印十六进制字符串
public static void printHexString(byte[] b) {
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());
}
}
}
/**
* 汉字字符串转换成十六进制byte数组,一个汉字存到两个byte里面,大整数存到两个byte里面
* @author zhengdunzhuang
*
*/
public class ChineseCharacterStringToByteArray {
public static void main(String[] args) {
byte[] data = new byte[42];
data[0] = (byte) 0x04;
data[1] = (byte) (42);
printHexString(data);
System.out.println("");
String msg = "你已成功添加李先生为好友";
data = toHexByteByStrings(data,3,msg);
printHexString(data);
/*
* 用工具转换需要的结果:
你已成功添加李先生为好友
{0x4f60,0x5df2,0x6210,0x529f,0x6dfb,0x52a0,0x674e,0x5148,0x751f,0x4e3a,0x597d,0x5 3cb,0x0};
打印输出结果:
042A00000000000000000000000000000000000000000000000000000000000000000000000000000000
042A4F605DF26210529F6DFB52A0674E5148751F4E3A597D53CB00000000000000000000000000000000
4F60
*/
int i = 20320;//0x4f60
byte[] b = intToByteArray(i);
System.out.println("");
printHexString(b);
}
//整数转换成byte数组
public static byte[] intToByteArray(int i) {
byte[] result = new byte[2];
result[0] = (byte) ((i & 0xFF00) >> 8 );
result[1] = (byte) ( i & 0xFF);
return result;
}
/**
* 转化汉字字符串为十六进制编码并放到byte数组里面,一个字符两个byte字节
* @param data byte数组
* @param start 要追加的开始个数,从1算起,如从byte[3]开始 则start=4
* @param s 要转换的字符串
* @return
*/
public static byte[] toHexByteByStrings(byte[] data,int start,String s) {int j = start;
for (int i = 0; i < s.length(); i++) {
int ch = (int) s.charAt(i);
data[j-1] = (byte) ((ch & 0xFF00) >> 8 );
data[j] = (byte) ( ch & 0xFF);
j += 2;
}
return data;
}
// 转化字符串为十六进制编码字符串
public static String toHexByStrings(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;// 0x表示十六进制
}
//打印十六进制字符串
public static void printHexString(byte[] b) {
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());
}
}
}
大自然,飘然的风,QQ群: python技术交流群:453879716,人工智能深度学习群:251088643
golang技术交流群:316397059,vuejs技术交流群:458915921 囤币一族:621258209,有兴趣的可以加入
微信公众号: 心禅道(xinchandao)投资论道
golang技术交流群:316397059,vuejs技术交流群:458915921 囤币一族:621258209,有兴趣的可以加入
微信公众号: 心禅道(xinchandao)投资论道