字节数组 整型 字符串 十六进制 字符 之间的转换

最近在项目开发中,用到了多种类型间的转换.记录一下 

    
// 字节数组转换字符数组
      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;
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值