java byte 转 c_Java byte转换工具类

这是一个Java工具类,提供了将数字、16进制字符串与字节之间相互转换的方法,包括数字转16进制字符串、16进制字符串转数字、16进制字符串转10进制、10进制转16进制,以及字节数组到int值的转换。同时,类中还包含了计算CRC16校验码、字节合并、加密解密和异或操作的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

packagecn.hawk.bluetoothlib.utils;importandroid.util.Log;importcom.clj.fastble.utils.HexUtil;importjava.math.BigInteger;importjavax.crypto.Cipher;importjavax.crypto.spec.SecretKeySpec;/*** Created by kehaowei on 2017/10/27.*/

public classHexHelper {/*** 数字转16进制字符串

*@paraminput

*@return

*/

public staticString number2HexStr(String input) {

String output= "";if (input.length() % 2 != 0)

input+= "0";int count = input.length() / 2;for (int i = 0; i < count; i++) {

String sub= input.substring(i * 2, i * 2 + 2);int num =getIntSafely(sub);

output+=int2Hex(num);

}returnoutput;

}/*** 16进制字符串转数字

*@paraminput

*@return

*/

public staticString hexStr2number(String input) {

String output= "";if (input.length() % 2 != 0)

input+= "0";int count = input.length() / 2;for (int i = 0; i < count; i++) {

String sub= input.substring(i * 2, i * 2 + 2);int temp = hexChar2int(sub.substring(0, 1)) * 16 + hexChar2int(sub.substring(1, 2));if (temp < 10) {

output+= "0" +temp;

}else{

output+= temp + "";

}

}returnoutput;

}/*** 16进制字符串转10进制

*@paraminput

*@return

*/

public static inthexStr2Int(String input) {int output = 0;int count =input.length();for (int i = 0; i < count; i++) {

String sub= input.substring(i, i + 1);int temp = (int) (hexChar2int(sub) * Math.pow(16, count - 1 -i));

output+=temp;

}returnoutput;

}/*** 16进制字符串转10进制

*@paraminput

*@return

*/

public static longhexStr2Long(String input) {long output = 0;int count =input.length();for (int i = 0; i < count; i++) {

String sub= input.substring(i, i + 1);long temp = (long) (hexChar2int(sub) * Math.pow(16, count - 1 -i));

output+=temp;

}returnoutput;

}/*** 安全的获取整数

*@paramstr

*@return

*/

public static intgetIntSafely(String str) {int result = 0;try{

result=Integer.parseInt(str);

}catch(Exception e) {

e.printStackTrace();

}returnresult;

}/*** 10进制转16进制

*@paraminput

*@return

*/

public static String int2Hex(intinput) {

String result= "";if (input < 16) {

result= "0" +intChar2Hex(input);

}else{

result= intChar2Hex(input / 16) + intChar2Hex(input % 16);

}returnresult;

}/*** 整数转16进制

*@paraminput

*@return

*/

public static String intChar2Hex(intinput) {

String result= "0";if (input < 10)

result= "" +input;else{switch(input) {case 10:

result= "A";break;case 11:

result= "B";break;case 12:

result= "C";break;case 13:

result= "D";break;case 14:

result= "E";break;case 15:

result= "F";break;

}

}returnresult;

}public static final String bytesToHexString(byte[] bArray) {

StringBuffer sb= newStringBuffer(bArray.length);

String sTemp;for (int i = 0; i < bArray.length; i++) {

sTemp= Integer.toHexString(0xFF &bArray[i]);if (sTemp.length() < 2)

sb.append(0);

sb.append(sTemp.toUpperCase());

}returnsb.toString();

}/*** 16进制字符转整数

*@paraminput

*@return

*/

public static inthexChar2int(String input) {int result = 0;try{

result=Integer.parseInt(input);

}catch(Exception e) {if (input.equalsIgnoreCase("A"))

result= 10;else if (input.equalsIgnoreCase("B"))

result= 11;else if (input.equalsIgnoreCase("C"))

result= 12;else if (input.equalsIgnoreCase("D"))

result= 13;else if (input.equalsIgnoreCase("E"))

result= 14;else if (input.equalsIgnoreCase("F"))

result= 15;

}returnresult;

}//使用1字节就可以表示b

public static String numToHex8(intb) {return String.format("%02x", b);//2表示需要两个16进行数

}//需要使用2字节表示b

public static String numToHex16(intb) {return String.format("%04x", b);

}//需要使用4字节表示b

public static String numToHex32(intb) {return String.format("%08x", b);

}/*** 获取高四位

*

*@paramdata

*@return

*/

public static int getHeight4(bytedata) {intheight;

height= ((data & 0xf0) >> 4);returnheight;

}/*** 获取低四位

*

*@paramdata

*@return

*/

public static int getLow4(bytedata) {intlow;

low= (data & 0x0f);returnlow;

}/*** c-byte数组转换成java-int值
高位在前
[24 - 16 - 08 - 00]

*

*@parambuffer byte数组

*@paramindex 转换开始位置

*@paramlen 转换的长度*/

public static int cbyte2intHigh(byte[] buffer, int index, intlen) {if (buffer == null || index + len >buffer.length) {return 0;

}int value = 0;for (int i = 0, j = len - 1; i < len; i++, j--) {

value+= (cbyte2Int(buffer[i + index]) << (j * 8));

}returnvalue;

}/*** c无符号的值,转换成java-int值*/

public static int cbyte2Int(bytebyteNum) {return byteNum & 0xff;

}/*** 计算CRC16校验码

*

*@parambytes

*@return

*/

public static String getCRC(byte[] bytes) {int CRC =CRC16Util.CRC16(bytes);

System.out.println("getCRC CRC = "+CRC);returnnumToHex16(CRC);//return CRC16Util.getCRC(bytes);

}public static byte[] byteMerger(byte[] bt1, byte[] bt2){byte[] bt3 = new byte[bt1.length+bt2.length];

System.arraycopy(bt1,0, bt3, 0, bt1.length);

System.arraycopy(bt2,0, bt3, bt1.length, bt2.length);returnbt3;

}/*** 十六进制转换字符串

*@returnString 对应的字符串*/

public staticString hexStr2Str(String hexStr)

{

String str= "0123456789ABCDEF";char[] hexs =hexStr.toCharArray();byte[] bytes = new byte[hexStr.length() / 2];intn;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 newString(bytes);

}/*** 字节数组转版本号*/

public staticString hexStr2Version(String input){int[] ints = new int[4];

String output= "";if (input.length() % 2 != 0)

input+= "0";int count = input.length() / 2;for (int i = 0; i < count; i++) {

String sub= input.substring(i * 2, i * 2 + 2);int temp = hexChar2int(sub.substring(0, 1)) * 16 + hexChar2int(sub.substring(1, 2));

ints[i]=temp;

}return String.format("%d.%d.%d.%d",ints[0],ints[1],ints[2],ints[3]);

}public staticString hexXOR(String hex1, String hex2){

BigInteger i1= new BigInteger(hex1, 16);

BigInteger i2= new BigInteger(hex2, 16);

BigInteger res=i1.xor(i2);return res.toString(16).toUpperCase();

}//加密

public static byte[] Encrypt(String sSrc, String sKey) throwsException {if (sKey == null) {

System.out.print("Key为空null");return null;

}//判断Key是否为16位

if (sKey.length() != 16) {

System.out.print("Key长度不是16位");return null;

}byte[] raw = sKey.getBytes("utf-8");

SecretKeySpec skeySpec= new SecretKeySpec(raw, "AES");

Cipher cipher= Cipher.getInstance("AES/ECB/PKCS5Padding");//"算法/模式/补码方式"

cipher.init(Cipher.ENCRYPT_MODE, skeySpec);byte[] encrypted = cipher.doFinal(sSrc.getBytes("utf-8"));returnencrypted;

}//加密

public static byte[] Encrypt(byte[] sSrc, byte[] aesEncrypt) throwsException {if (aesEncrypt == null) {

System.out.print("Key为空null");return null;

}//判断Key是否为16位

/*if (sKey.length() != 16) {

System.out.print("Key长度不是16位");

return null;

}*/

byte[] raw =aesEncrypt;

SecretKeySpec skeySpec= new SecretKeySpec(raw, "AES");

Cipher cipher= Cipher.getInstance("AES/ECB/PKCS5Padding");//"算法/模式/补码方式"

cipher.init(Cipher.ENCRYPT_MODE, skeySpec);byte[] encrypted =cipher.doFinal(sSrc);returnencrypted;

}/*** 基于Sn的异或加密*/

public staticString getXOR_DATA(String ly2Package,String deviceSn){int ly2packLength=ly2Package.length();//得到sn前四位

String getStart4 = deviceSn.substring(0, 4);//得到sn后16位

String getLast16=deviceSn.substring(4,deviceSn.length());

String last16Hex=HexUtil.encodeHexStr(getLast16.getBytes());

String result1=XOR(ly2Package, ly2packLength, last16Hex);if (result1 != null) returnresult1;return "";

}public static String XOR(String content, intcotentLength, String keyHex) {int keyHexLength=keyHex.length();

String result=keyHex;if(keyHexLength>cotentLength){

result= keyHex.substring(0,cotentLength);

}else{int add = cotentLength -keyHexLength;int count = add /keyHexLength;int remainder = add % keyHexLength;//余数

for (int i = 0; i

result= result+keyHex;

}if (remainder!=0){

result= result + keyHex.substring(0, remainder);

}

}if (result.length()==cotentLength){//进行异或

Log.e("进行异或result:",result);

Log.e("进行异或content:",content);

String hexXOR=HexHelper.hexXOR(result, content);if (hexXOR.length()!=cotentLength){int absCount = Math.abs(cotentLength-hexXOR.length());for (int i = 0; i < absCount; i++) {

hexXOR="0"+hexXOR;

}returnhexXOR;

}returnhexXOR;

}return null;

}public staticString timeStr2Hex(String time){

String result="";int length = time.length() / 2;int j=0;for (int i = 0; i < length; i++) {

String oneStr= numToHex8(Integer.parseInt(time.substring(j, j+2)));

j+=2;if (oneStr.length()==1){

oneStr="0"+oneStr;

}

result=result+oneStr;

}returnresult;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值