java
/**
* @author: zhoumo
* @data: 2024/7/4 14:56
* @descriptions:
*/
public class MsgUtil {
public static byte[] msgContentToByte(String msgcontent, int msgformat) {
String charset = null;
if (msgformat==15){
charset = "GBK";
} else if (msgformat==8){
charset = "iso-10646-ucs-2";
}
byte[] bytes= new byte[]{};
if(charset!=null){
try {
bytes= msgcontent.getBytes(charset);
} catch (UnsupportedEncodingException e) {
throw new IllegalArgumentException("Message can not Format");
}
} else {
bytes= msgcontent.getBytes();
}
return bytes;
}
public static String msgContentByteToChinese( byte[] ucs2Numbers , int msgformat) {
for (byte b : ucs2Numbers) {
//System.out.println("--- "+String.valueOf(b));
System.out.print(String.valueOf(b)+",");
// System.out.printf("0x%02X ", b);
}
System.out.println();
// 根据编码格式将字节数组重新转换回中文字符串
String decodedString ="";
try {
String charset = (msgformat == 15) ? "GBK" : "iso-10646-ucs-2";
decodedString= new String(ucs2Numbers, charset);
System.out.println("Decoded String: " + decodedString);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return decodedString;
}
public static String msgContentStrToByte( String msg , int msgformat) {
// 将字符串分割为字符串数组
String[] intStringArray = msg.split(",");
// 将字符串数组转换为整数数组
int[] intArray = new int[intStringArray.length];
for (int i = 0; i < intStringArray.length; i++) {
intArray[i] = Integer.parseInt(intStringArray[i].trim());
}
// 将整数数组转换为字节数组
byte[] byteArray = new byte[intArray.length];
for (int i = 0; i < intArray.length; i++) {
byteArray[i] = (byte) intArray[i];
}
// 打印字节数组
System.out.println("Byte Array:");
for (byte b : byteArray) {
System.out.printf("0x%02X ", b);
}
System.out.println();
return msgContentByteToChinese(byteArray,msgformat);
}
public static void main(String[] args) {
// 输入的UCS2编码的数字数组
String msgcontent="人民万岁";
int msgformat=8;
/* byte[] ucs2Numbers = msgContentToByte(msgcontent, msgformat);
// 打印字节数组
// 打印字节数组
System.out.println((msgformat == 15 ? "GBK" : "UCS2") + " Bytes:");
msgContentByteToChinese(ucs2Numbers,msgformat);*/
String msg="78,-70,108,17,78,7,92,-127";
msgContentStrToByte(msg,msgformat);
}
}
第二版
java
/**
* 内容转移为GBK 或者ucs2
* @param msgcontent 内容
* @param msgformat 转码方式
* @return
*/
public static byte[] msgContentToByte(String msgcontent, int msgformat) {
String charset = null;
if (msgformat==15){
charset = "GBK";
} else if (msgformat==8){
charset = "iso-10646-ucs-2";
}
byte[] bytes= new byte[]{};
if(charset!=null){
try {
bytes= msgcontent.getBytes(charset);
} catch (UnsupportedEncodingException e) {
throw new IllegalArgumentException("Message can not Format");
}
} else {
bytes= msgcontent.getBytes();
}
return bytes;
}
/**
* 根据传入中文转为16进制字符串
* @param msgcontent
* @param msgformat
* @return
*/
public static String msgContentToByteStr(String msgcontent, int msgformat) {
String charset = null;
if (msgformat==15){
charset = "GBK";
} else if (msgformat==8){
charset = "iso-10646-ucs-2";
}
byte[] bytes= new byte[]{};
if(charset!=null){
try {
bytes= msgcontent.getBytes(charset);
} catch (UnsupportedEncodingException e) {
throw new IllegalArgumentException("Message can not Format");
}
} else {
bytes= msgcontent.getBytes();
}
StringBuffer sb=new StringBuffer(8);
for (byte b : bytes) {
sb.append(String.format("%02X", b));
}
return sb.toString();
}
/**
*
* @param ucs2Numbers
* @param msgformat 15 = GBK ; 8 = iso-10646-ucs-2
* @return
*/
public static String msgContentByteToChinese( byte[] ucs2Numbers , int msgformat) {
// 根据编码格式将字节数组重新转换回中文字符串
String decodedString ="";
try {
String charset = (msgformat == 15) ? "GBK" : "iso-10646-ucs-2";
decodedString= new String(ucs2Numbers, charset).trim();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return decodedString;
}
/**
* 字段转中文
* @param msg 如 String msg="78,-70,108,17,78,7,92,-127";
* @param msgformat 15 = GBK ; 8 = iso-10646-ucs-2
* @return
*/
public static String msgContentStrToChinese( String msg , int msgformat) {
// 将字符串分割为字符串数组
String[] intStringArray = msg.split(",");
int[] intArray = new int[intStringArray.length];
for (int i = 0; i < intStringArray.length; i++) {
intArray[i] = Integer.parseInt(intStringArray[i].trim());
}
byte[] byteArray = new byte[intArray.length];
for (int i = 0; i < intArray.length; i++) {
byteArray[i] = (byte) intArray[i];
}
System.out.println("Byte Array:");
for (byte b : byteArray) {
System.out.printf("%02X", b);
// System.out.printf(String.valueOf(b));
}
System.out.println();
return msgContentByteToChinese(byteArray,msgformat);
}
/**
* 长短信拆分 byte数组传入,返回byte 数组
* @param content 传入的conteng byte数组
* @return
*/
private static Vector<byte[]> splitContentByteToByte(byte[] content) {
ByteArrayInputStream buf = new ByteArrayInputStream(content);
Vector<byte[]> tmpv = new Vector<byte[]>();
int msgCount = (int) (content.length / (140 - 6) + 1);
int LeftLen = content.length;
for (int i = 0; i < msgCount; i++) {
byte[] tmp = new byte[140 - 6];
if (LeftLen < (140 - 6)) {
tmp = new byte[LeftLen];
}
try {
buf.read(tmp);
tmpv.add(tmp);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
LeftLen = LeftLen - tmp.length;
}
return tmpv;
}
/**
* 长短信拆分
* @param msg 内容
* @param msgformat
* @return
*/
private static Vector<byte[]> splitContentStringToByte(String msg , int msgformat) {
byte[] content = msgContentToByte(msg, msgformat);
ByteArrayInputStream buf = new ByteArrayInputStream(content);
Vector<byte[]> tmpv = new Vector<byte[]>();
int msgCount = (int) (content.length / (140 - 6) + 1);
int LeftLen = content.length;
for (int i = 0; i < msgCount; i++) {
byte[] tmp = new byte[140 - 6];
if (LeftLen < (140 - 6)) {
tmp = new byte[LeftLen];
}
try {
buf.read(tmp);
tmpv.add(tmp);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
LeftLen = LeftLen - tmp.length;
}
return tmpv;
}
/**
* 16进制字符串转化为Byte数组
* @param hex
* @return
*/
public static byte[] hexStringToByte(String hex) {
if (hex == null || hex.length() == 0) {
return null;
}
// 如果 hex 中的字符不是偶数个, 则忽略最后一个
char[] hexChars = hex.toCharArray();
byte[] bytes = new byte[hexChars.length / 2];
for (int i = 0; i < bytes.length; i++) {
bytes[i] = (byte) Integer.parseInt("" + hexChars[i * 2] + hexChars[i * 2 + 1], 16);
}
return bytes;
}
/**
* 字符串转换为16进制字符串
* @param strPart
* @return
*/
public static String stringToHexString(String strPart) {
StringBuffer hexString = new StringBuffer();
for (int i = 0; i < strPart.length(); i++) {
int ch = (int) strPart.charAt(i);
String strHex = Integer.toHexString(ch);
hexString.append(strHex);
}
return hexString.toString();
}
/**
* 16进制字符串转化为字符串
* @param hex
* @return
*/
public static String hexStringToString(String hex){
StringBuilder sb = new StringBuilder();
for( int i=0; i < hex.length()-1; i+=2 ){
String output = hex.substring(i, (i + 2));
int decimal = Integer.parseInt(output, 16);
sb.append((char)decimal);
}
return sb.toString();
}
public static void main(String[] args) throws UnsupportedEncodingException {
// 输入的UCS2编码的数字数组
String msgcontent="人民万岁,abc。";
int msgformat=15;
System.out.println(" 根据传入中文打印--- "+msgContentToByteStr(msgcontent,msgformat));
byte[] ucs2Numbers = msgContentToByte(msgcontent, msgformat);
// 打印字节数组
// 打印字节数组
System.out.println((msgformat == 15 ? "GBK" : "UCS2") + " Bytes:");
msgContentByteToChinese(ucs2Numbers,msgformat);
String six="C8CBC3F1CDF2CBEA2C616263A1A3";
String gbk = new String(hexStringToByte(six), "GBK");
System.out.println("根据16进制反转中文-- "+gbk);
String msg="78,-70,108,17,78,7,92,-127";
// String msgEnd = msgContentStrToChinese(msg, msgformat);
// 6个字节
/* String msgEnd ="【测试】返回地球的速度是地球质量决定的,对任何进入外太空后返回的载具都是一样。阿波罗11号再入速度是每秒10.67千米,阿波罗10号为了省时间更快到每秒11.08千米。这俩甚至不是人造物体里最快的,最快的是星尘号彗星探测器,样品返回舱再入速度是每秒12.9千米,或者说达到了49马赫数。";
byte[] ucs2Numbers = msgContentToByte(msgEnd, msgformat);
*//* Vector<byte[]> bytes = splitContentByteToByte(ucs2Numbers);
for (int i = 0; i < bytes.size(); i++) {
System.out.println(Arrays.toString(bytes.get(i)) +",");
}*//*
Vector<byte[]> bytes = splitContentStringToByte(msgEnd, msgformat);
for (int i = 0; i < bytes.size(); i++) {
System.out.println(Arrays.toString(bytes.get(i)) +",");
}*/
}