/**
* @author benjaminwan
*
*/
public class DataCnv {
//-------------------------------------------------------
//
static public int isOdd(int num)
{
return num & 0x1;
}
//-------------------------------------------------------
static public int HexToInt(String inHex)
{
return Integer.parseInt(inHex, 16);
}
//-------------------------------------------------------
static public byte HexToByte(String inHex)
{
return (byte) Integer.parseInt(inHex,16);
}
//-------------------------------------------------------
// static public String Byte2Hex(Byte inByte)
// {
// return String.format("%02x", inByte).toUpperCase();
// }
static public String Byte2Hex(Byte inByte)
{
String strHex = Integer.toHexString(inByte & 0xFF);
strHex = (strHex.length() == 1) ? "0" + strHex : strHex; // 每个字节由两个字符表示,位数不够,高位补0
return strHex;
}
//-------------------------------------------------------
static public String ByteArrToHex(byte[] inBytArr)
{
StringBuilder strBuilder=new StringBuilder();
int j=inBytArr.length;
for (int i = 0; i < j; i++)
{
strBuilder.append(Byte2Hex(inBytArr[i]));
if (i != j - 1){
strBuilder.append(" ");
}
}
return strBuilder.toString();
}
//-------------------------------------------------------
static public String ByteArrToHex(byte[] inBytArr, int len)
{
StringBuilder strBuilder=new StringBuilder();
int j=len<=inBytArr.length?len:inBytArr.length;
for (int i = 0; i < j; i++)
{
strBuilder.append(Byte2Hex(inBytArr[i]));
strBuilder.append(" ");
}
return strBuilder.toString();
}
//-------------------------------------------------------
static public String ByteArrToHex(byte[] inBytArr, int offset, int byteCount)
{
StringBuilder strBuilder=new StringBuilder();
int j=byteCount;
for (int i = offset; i < j; i++)
{
strBuilder.append(Byte2Hex(inBytArr[i]));
if(i!=j-1)
strBuilder.append(":");
}
return strBuilder.toString();
}
/**
* byte数组转换成十六进制字符串
* @param src
* @return
*/
public static String bytesToHexString(byte[] src) {
StringBuilder stringBuilder = new StringBuilder("");
if (src == null || src.length <= 0) {
return null;
}
for (int i = 0; i < src.length; i++) {
int v = src[i] & 0xFF;
String hv = Integer.toHexString(v);
if (hv.length() < 2) {
stringBuilder.append(0);
}
stringBuilder.append(hv);
}
return stringBuilder.toString();
}
//-------------------------------------------------------
//
static public byte[] HexToByteArr(String inHex)
{
int hexlen = inHex.length();
byte[] result;
if (isOdd(hexlen)==1)
{
hexlen++;
result = new byte[(hexlen/2)];
inHex="0"+inHex;
}else {
result = new byte[(hexlen/2)];
}
int j=0;
for (int i = 0; i < hexlen; i+=2)
{
result[j]=HexToByte(inHex.substring(i,i+2));
j++;
}
return result;
}
//-------------------------------------------------------
static public int Byte2int(byte[] b, int len, int offset) {
int res = 0;
int bLen = len;
for (int i = 0; i < bLen; i++) {
res = res << (8 * i) | (b[i+offset] & 0xFF);
}
return res;
}
//-------------------------------------------------------
static public int _2ByteASCII2int(byte[] b, int offset) {
int res = 0;
res = ((b[offset]-0x30)&0xFF)*10 + ((b[offset+1]-0x30)&0xFF);
return res;
}
}
06-30
2033
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
03-28
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交