java byte 工具类_java字节处理的各种工具类

importjava.util.ArrayList;importjava.util.List;/*** 字节处理的工具类

*@authorwq

**/

public classByteDisposeUtil {/*** 将字符创转化为字节数组(只适合16进制)

*@paramzhiling

*@return

*/

public static byte[] conversionStringToBytes(String zhiling){

List bytes = new ArrayList();for(int i=0;i

String hex=zhiling.substring(i,i+2);byte[] bnew=hexStringToBytes(hex);

bytes.add(bnew);

}byte[] newByte =sysCopy(bytes);returnnewByte;

}/*** 把data中从off开始的 length个字符转换成十六进制

*@paramdata

*@paramoff

*@paramlength

*@return

*/

public static final StringBuffer toHex(byte[] data, int off, intlength) {

StringBuffer buf= new StringBuffer(data.length * 2);for (int i = off; i < length; i++) {if (((int) data[i] & 0xff) < 0x10) {

buf.append("0");

}

buf.append(Long.toString((int) data[i] & 0xff, 16));

}returnbuf;

}/*** 字符转化为字节(将10进制转化为16进制后再变为字节)

*@paramc

*@return

*/

public static byte charToByte(charc) {return (byte) "0123456789ABCDEF".indexOf(c);

}/*** 十六进制转化为字节

*@paramhexString

*@return

*/

public static byte[] hexStringToBytes(String hexString) {if (hexString == null || hexString.equals("")) {return null;

}

hexString=hexString.toUpperCase();int length = hexString.length() / 2;char[] hexChars =hexString.toCharArray();byte[] d = new byte[length];for (int i = 0; i < length; i++) {int pos = i * 2;

d[i]= (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));

}returnd;

}/*** 合并数组

*@paramsrcArrays

*@return

*/

public static byte[] sysCopy(ListsrcArrays) {int len = 0;for (byte[] srcArray:srcArrays) {

len+=srcArray.length;

}byte[] destArray = new byte[len];int destLen = 0;for (byte[] srcArray:srcArrays) {

System.arraycopy(srcArray,0, destArray, destLen, srcArray.length);

destLen+=srcArray.length;

}returndestArray;

}/*** 将一个int数据转为按小端顺序排列的字节数组

*@paramdata int数据

*@return按小端顺序排列的字节数组*/

public static byte[] changeByte(intdata){byte b4 = (byte)((data)>>24);byte b3 = (byte)(((data)<<8)>>24);byte b2= (byte)(((data)<<16)>>24);byte b1 = (byte)(((data)<<24)>>24);byte[] bytes ={b1,b2,b3,b4};returnbytes;

}/*** 十进制转化为十六进制

**/

public static String dtoh(inta){

String bb="";while((a/16)!=0){int b=a%16;

String s=""+b;if(b==10){

s="A";

}if(b==11){

s="B";

}if(b==12){

s="C";

}if(b==13){

s="D";

}if(b==14){

s="E";

}if(b==15){

s="F";

}

a=a/16;

bb+=s;

}

int k=a%16;

if(k>=10){

if(k==10){

bb+="A";

}

if(k==11){

bb+="B";

}

if(k==12){

bb+="C";

}

if(k==13){

bb+="D";

}

if(k==14){

bb+="E";

}

if(k==15){

bb+="F";

}

}

else{

bb+=k;

}String result=newStringBuffer(bb).reverse().toString();int length=result.length();for(int j=0;j<4-length;j++){

result="0"+result;

}returnresult;

}/*** 16进制转10进制(8位)*/

public staticString htod(String a){

StringBuffer icid=newStringBuffer(a);

String fanzhuan=icid.reverse().toString().toUpperCase();int zhengshuka=0;for(int i=0;i

zhengshuka+=(num*Math.pow(16,i));

}

String zhengshu=""+zhengshuka;for(int j=0;j<8-(zhengshuka+"").length();j++){

zhengshu=("0"+zhengshu);

}returnzhengshu;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值