最近在项目开发中,用到了多种类型间的转换.记录一下
// 字节数组转换字符数组
public static String bytetoString( byte [] by) {
char c[]=new char[550];
for(int i=0;i<by.length;i++){
c[i]=(char)by[i];
System.out.println("char的字节是"+c[i]);
}
String str1= new String(c);
return str1.trim();
}
// 字节数组转换为十六进制
public static String byteToHexString( byte [] by)
{
int len=by.length;
String ret="0x ";
for(int i=0;i<len;i++)
{
int a=(int)by[i];
if(a<0) a+=256;
String s=Integer.toHexString(a);
if(s.length()==1)
{
s="0"+s;
}
ret+=s+" ";
}
return ret;
}
// 一个字节数组转换为另一个字节数组
public static byte [] changeByte( byte [] byte1, int i, int a, int j, int b, byte [] byte2) {
// byte[] byExp={-35,00,-128,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,-49};
// byte2=new byte[128];
for(;i>=a;){
for(;j<=b;j++){
byte2[j]=byte1[i];
i--;
// System.err.println(byExp[j]);
}
}
return byte2;
}
// 整型转换字节数组
public static byte [] int2bytes( int num)
{
byte[] b=new byte[4];
int mask=0xff;
for(int i=0;i<4;i++){
b[i]=(byte)(num>>>(24-i*8));
}
return b;
}
// 字节数组转换整型
public static int bytes2int( byte [] b)
{
int mask=0xff;
int temp=0;
int res=0;
for(int i=0;i<4;i++){
res<<=8;
temp=b[i]&mask;
res|=temp;
}
return res;
}
// 十六进制转换字节数组
public static byte [] hexToByte(String hexStr) {
byte[] baKeyword = new byte[hexStr.length()/2];
for(int i = 0; i < baKeyword.length; i++)
{
try
{
baKeyword[i]=(byte)(Integer.parseInt(hexStr.substring(i*2,i*2+2),16));
}
catch(Exception e)
{
e.printStackTrace();
}
}
return baKeyword;
}
// 字节数组转换字符数组
public static String bytetoString( byte [] by) {
char c[]=new char[550];
for(int i=0;i<by.length;i++){
c[i]=(char)by[i];
System.out.println("char的字节是"+c[i]);
}
String str1= new String(c);
return str1.trim();
}
// 字节数组转换为十六进制
public static String byteToHexString( byte [] by)
{
int len=by.length;
String ret="0x ";
for(int i=0;i<len;i++)
{
int a=(int)by[i];
if(a<0) a+=256;
String s=Integer.toHexString(a);
if(s.length()==1)
{
s="0"+s;
}
ret+=s+" ";
}
return ret;
}
// 一个字节数组转换为另一个字节数组
public static byte [] changeByte( byte [] byte1, int i, int a, int j, int b, byte [] byte2) {
// byte[] byExp={-35,00,-128,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,-49};
// byte2=new byte[128];
for(;i>=a;){
for(;j<=b;j++){
byte2[j]=byte1[i];
i--;
// System.err.println(byExp[j]);
}
}
return byte2;
}
// 整型转换字节数组
public static byte [] int2bytes( int num)
{
byte[] b=new byte[4];
int mask=0xff;
for(int i=0;i<4;i++){
b[i]=(byte)(num>>>(24-i*8));
}
return b;
}
// 字节数组转换整型
public static int bytes2int( byte [] b)
{
int mask=0xff;
int temp=0;
int res=0;
for(int i=0;i<4;i++){
res<<=8;
temp=b[i]&mask;
res|=temp;
}
return res;
}
// 十六进制转换字节数组
public static byte [] hexToByte(String hexStr) {
byte[] baKeyword = new byte[hexStr.length()/2];
for(int i = 0; i < baKeyword.length; i++)
{
try
{
baKeyword[i]=(byte)(Integer.parseInt(hexStr.substring(i*2,i*2+2),16));
}
catch(Exception e)
{
e.printStackTrace();
}
}
return baKeyword;
}