java转换工具类_Java数据转换工具类
前段時間有個項目是通過報文協議與硬件進行交互,在實現的過程中整理了一些通用的數據轉換方法,方便項目的使用,特此記錄一下,具體實現如下,如有需要的可以參考一下,希望會有幫助......
/**
* byte轉無符號int
* @param s 轉換值
* @return 無符號
*/
public static int toUnsigned(byte s) {
return s & 0xFF;
}
/**
* byte數組轉換為無符號short整數
*
* @param bytes byte數組
* @param changeHi 是否進行高低位轉換,true低位在前高位在后,false高位在前低位在后
* @return short整數
*/
public static long bytesToUnsignedShort(byte[] bytes,boolean changeHi) {
int t = 0;
if(changeHi) {
for (int i = bytes.length - 1; i >= 0; i--) {
if(i == bytes.length - 1)
t = (bytes[i]&0xFF)<
else
t = t|(bytes[i]&0xFF)<
}
return t;
}
else {
for(int i=0;i
if(i == 0)
t = (bytes[i]&0xFF);
else
t = t|(bytes[i]&0xFF)<
}
return t;
}
}
/**
* byte數組轉換為無符號short整數
*
* @param bytes byte數組
* @param changeHi 是否進行高低位轉換,true低位在前高位在后,false高位在前低位在后
* @return short整數
*/
public static int byte2ToUnsignedShort(byte[] bytes,boolean changeHi) {
if(changeHi)
return (bytes[1] << 8 & 0xFF00) | (bytes[0] & 0xFF);
else
{
return (((bytes[0] & 0xFF)<<8)|(bytes[1] & 0xFF));
}
}
/**
* byte數組轉換為無符號short整數
*
* @param bytes byte數組
* @return short整數
*/
public static int byte3ToUnsignedShort(byte[] bytes,boolean changeHi) {
int b0 = bytes[0] & 0xFF;
int b1 = bytes[1] & 0xFF;
int b2 = bytes[2] & 0xFF;
if(changeHi)
return (b2 << 16) | (b1 << 8) | b0;
else
return (b0 << 16)|(b1 << 8) | b2;
}
/**
* byte數組轉換為無符號short整數
*
* @param bytes byte數組
* @return short整數
*/
public static long byte4ToUnsignedShort(byte[] bytes,boolean changeHi) {
long b0 = bytes[0] & 0xFF;
long b1 = bytes[1] & 0xFF;
long b2 = bytes[2] & 0xFF;
long b3 = bytes[3] & 0xFF;
if(changeHi)
return (b3 << 24) | (b2 << 16) | (b1 << 8) | b0;
else
return (b0 << 24) | (b1 << 16) | (b2 << 8) | b3;
}
/**
* byte數組轉換為無符號short整數
*
* @param bytes byte數組
* @return short整數
*/
public static long byte5ToUnsignedShort(byte[] bytes,boolean changeHi) {
long b0 = bytes[0] & 0xFF;
long b1 = bytes[1] & 0xFF;
long b2 = bytes[2] & 0xFF;
long b3 = bytes[3] & 0xFF;
long b4 = bytes[4] & 0xFF;
if(changeHi)
return (b4 << 32) | (b3 << 24) | (b2 << 16) | (b1 << 8) | b0;
else
return (b0 << 32)|(b1<<24)|(b2<<16)|(b3<<8)|b4;
}
/**
* byte數組轉換為無符號short整數
*
* @param bytes byte數組
* @return short整數
*/
public static long byte8ToUnsignedShort(byte[] bytes,boolean changeHi) {
long b0 = bytes[0] & 0xFF;
long b1 = bytes[1] & 0xFF;
long b2 = bytes[2] & 0xFF;
long b3 = bytes[3] & 0xFF;
long b4 = bytes[4] & 0xFF;
long b5 = bytes[5] & 0xFF;
long b6 = bytes[6] & 0xFF;
long b7 = bytes[7] & 0xFF;
if(changeHi)
return (b7 << 56) | (b6 << 48) | (b5 << 40) | (b4 << 32) | (b3 << 24) | (b2 << 16) | (b1 << 8) | b0;
else
return (b0<<56)|(b1<<48)|(b2<<40)|(b3<<32)|(b4<<24)|(b5<<16)|(b6<<8)|b7;
}
/**
* Convert byte[] to hex string.這里我們可以將byte轉換成int,然后利用Integer.toHexString(int)來轉換成16進制字符串。
*
* @param src byte[] data
* @return hex string
*/
public static String bytesToHexString(byte[] src,char slicer) {
StringBuilder stringBuilder = new StringBuilder();
if (src == null || src.length <= 0) {
return null;
}
for (byte b : src) {
int v = b & 0xFF;
String hv = Integer.toHexString(v);
if (hv.length() < 2) {
stringBuilder.append(0);
}
stringBuilder.append(hv);
if('\0'!=slicer)
stringBuilder.append(slicer);
}
if('\0'!=slicer)
stringBuilder.deleteCharAt(stringBuilder.length()-1);
return stringBuilder.toString();
}
/**
* 將 10 進制轉化為二進制
*
* @param de :待轉換的十進制
* @return :轉換后的二進制(string)
*/
public static String Decimal2Binary(long de,int byteNum) {
StringBuilder numstr = new StringBuilder();
while (de > 0) {
long res = de % 2; //除2 取余數作為二進制數
numstr.insert(0, res);
de = de / 2;
}
int len = numstr.length();
if(len<=byteNum*8){
int cha = byteNum*8-len;
for(int i=0;i
numstr.insert(0,0);
}
}
return numstr.toString();
}
/**
* 將 10 進制轉化為八進制
*
* @param de :待轉換的十進制
* @return :轉換后的八進制(string)
*/
public static String Decimal2Oct(long str){
return Long.toOctalString(str);
}
public static String Decimal2Hex(long str){
return Long.toHexString(str);
}
/**
* byte數組轉成16進制的字符串
*
* @param bytes
* @return
*/
final protected static char[] hexArray = "0123456789ABCDEF".toCharArray();
public static String bytesToHexString(byte[] bytes) {
char[] hexChars = new char[bytes.length * 2];
for (int j = 0; j < bytes.length; j++) {
int v = bytes[j] & 0xFF;
hexChars[j * 2] = hexArray[v >>> 4];
hexChars[j * 2 + 1] = hexArray[v & 0x0F];
}
return new String(hexChars);
}
/**
* 將 16 進制字符串轉化為int
*
* @param de :待轉換的16進制
* @return :轉換后的int
*/
public static int string16ToInt(String hex,boolean changeHi){
String temp = hex.replaceAll("^0[x|X]", "");
int i = 0;
if(changeHi){
if(temp.length()==2){
i=Integer.parseInt(temp, 16);
}else if(temp.length()==4){
temp = temp.substring(2,4)+temp.substring(0,2);
i=Integer.parseInt(temp, 16);
}else {
i=Integer.parseInt(temp, 16);
}
}else {
i=Integer.parseInt(temp, 16);
}
return i;
}
/**
* 整形轉字節數組
*
* @param iSource 源
* @param iArrayLen 長度
* @return 數組
*/
public static byte[] intTobye(int iSource, int iArrayLen,boolean changeHi) {
if(changeHi) {
byte[] bLocalArr = new byte[iArrayLen];
for (int i = 0; (i < iArrayLen); i++) {
bLocalArr[i] = (byte) (iSource >> 8 * i & 0xFF);
}
return bLocalArr;
}else {
byte[] bLocalArr = new byte[iArrayLen];
for (int i = iArrayLen-1; i >=0; i--) {
bLocalArr[i] = (byte) ((iSource >> 8 * i) & 0xFF);
}
return bLocalArr;
}
}
/**
* 把long值轉byte[]
* @param iSource 待轉long值
* @param iArrayLen 需轉成多少byte數組長度
* @param changeHi 是否高位在前,true高位在前,false低位在前
* @return
*/
public static byte[] longTobye(long iSource, int iArrayLen,boolean changeHi) {
byte[] bLocalArr = new byte[iArrayLen];
if(changeHi) {
for (int i = 0; (i < 8) && (i < iArrayLen); i++) {
bLocalArr[i] = (byte) (iSource >> 8 * i & 0xFF);
}
}else {
for (int i = iArrayLen-1; i >=0; i--) {
bLocalArr[i] = (byte) (iSource >> 8 * i & 0xFF);
}
}
return bLocalArr;
}
/**
* byte轉無符號整型
*
* @param bytes 數組
* @return 整型
*/
public static int bytesToInt(byte[] bytes,boolean changeHi) {
if(changeHi) {
int i1 = bytes[0] & 0xff;
int i2 = (bytes[1] & 0xff) << 8;
int i3 = (bytes[2] & 0xff) << 16;
int i4 = (bytes[3] & 0xff) << 24;
return i1 | i2 | i3 | i4;
}else {
int i1 = (bytes[0] & 0xff) << 24;
int i2 = (bytes[1] & 0xff) << 16;
int i3 = (bytes[2] & 0xff) << 8;
int i4 = (bytes[3] & 0xff);
return i1 | i2 | i3 | i4;
}
}
/**
* 返回一段字節數組
*
* @param src 源
* @param begin 開始
* @param count 位數
* @return 一段字節數組
*/
public static byte[] SubBytes(byte[] src, int begin, int count) {
byte[] bs = new byte[count];
if (src.length > begin && src.length > count && src.length >= begin + count) {
System.arraycopy(src, begin, bs, 0, count);
}
return bs;
}
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的java转换工具类_Java数据转换工具类的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java 配置文件加载_Java加载配置
- 下一篇: java表达式由什么组成_必知必会之La