java二进制,字节数组,字符,十六进制,BCD编码转换 ,GPS,GIS

ExpandedBlockStart.gif ContractedBlock.gif java二进制,字节数组,字符,十六进制,BCD编码转换2007 - 06 - 07   00 : 17 /** */ /**
InBlock.gif    * 把16进制字符串转换成字节数组
InBlock.gif    * 
@param hex
InBlock.gif    * 
@return
ExpandedBlockEnd.gif    
*/

ExpandedBlockStart.gifContractedBlock.gif
public   static   byte [] hexStringToByte(String hex)  dot.gif {
InBlock.gif    
int len = (hex.length() / 2);
InBlock.gif    
byte[] result = new byte[len];
InBlock.gif    
char[] achar = hex.toCharArray();
ExpandedSubBlockStart.gifContractedSubBlock.gif    
for (int i = 0; i < len; i++dot.gif{
InBlock.gif     
int pos = i * 2;
InBlock.gif     result[i] 
= (byte) (toByte(achar[pos]) << 4 | toByte(achar[pos + 1]));
ExpandedSubBlockEnd.gif    }

InBlock.gif    
return result;
ExpandedBlockEnd.gif}

None.gif
ExpandedBlockStart.gifContractedBlock.gif
private   static   byte  toByte( char  c)  dot.gif {
InBlock.gif    
byte b = (byte"0123456789ABCDEF".indexOf(c);
InBlock.gif    
return b;
ExpandedBlockEnd.gif}

None.gif
ExpandedBlockStart.gifContractedBlock.gif
/** */ /**
InBlock.gif    * 把字节数组转换成16进制字符串
InBlock.gif    * 
@param bArray
InBlock.gif    * 
@return
ExpandedBlockEnd.gif    
*/

ExpandedBlockStart.gifContractedBlock.gif
public   static   final  String bytesToHexString( byte [] bArray)  dot.gif {
InBlock.gif    StringBuffer sb 
= new StringBuffer(bArray.length);
InBlock.gif    String sTemp;
ExpandedSubBlockStart.gifContractedSubBlock.gif    
for (int i = 0; i < bArray.length; i++dot.gif{
InBlock.gif     sTemp 
= Integer.toHexString(0xFF & bArray[i]);
InBlock.gif     
if (sTemp.length() < 2)
InBlock.gif      sb.append(
0);
InBlock.gif     sb.append(sTemp.toUpperCase());
ExpandedSubBlockEnd.gif    }

InBlock.gif    
return sb.toString();
ExpandedBlockEnd.gif}

None.gif
ExpandedBlockStart.gifContractedBlock.gif
/** */ /**
InBlock.gif    * 把字节数组转换为对象
InBlock.gif    * 
@param bytes
InBlock.gif    * 
@return
InBlock.gif    * 
@throws IOException
InBlock.gif    * 
@throws ClassNotFoundException
ExpandedBlockEnd.gif    
*/

ExpandedBlockStart.gifContractedBlock.gif
public   static   final  Object bytesToObject( byte [] bytes)  throws  IOException, ClassNotFoundException  dot.gif {
InBlock.gif    ByteArrayInputStream in 
= new ByteArrayInputStream(bytes);
InBlock.gif    ObjectInputStream oi 
= new ObjectInputStream(in);
InBlock.gif    Object o 
= oi.readObject();
InBlock.gif    oi.close();
InBlock.gif    
return o;
ExpandedBlockEnd.gif}

None.gif
ExpandedBlockStart.gifContractedBlock.gif
/** */ /**
InBlock.gif    * 把可序列化对象转换成字节数组
InBlock.gif    * 
@param s
InBlock.gif    * 
@return
InBlock.gif    * 
@throws IOException
ExpandedBlockEnd.gif    
*/

ExpandedBlockStart.gifContractedBlock.gif
public   static   final   byte [] objectToBytes(Serializable s)  throws  IOException  dot.gif {
InBlock.gif    ByteArrayOutputStream out 
= new ByteArrayOutputStream();
InBlock.gif    ObjectOutputStream ot 
= new ObjectOutputStream(out);
InBlock.gif    ot.writeObject(s);
InBlock.gif    ot.flush();
InBlock.gif    ot.close();
InBlock.gif    
return out.toByteArray();
ExpandedBlockEnd.gif}

None.gif
ExpandedBlockStart.gifContractedBlock.gif
public   static   final  String objectToHexString(Serializable s)  throws  IOException dot.gif {
InBlock.gif    
return bytesToHexString(objectToBytes(s));
ExpandedBlockEnd.gif}

None.gif
ExpandedBlockStart.gifContractedBlock.gif
public   static   final  Object hexStringToObject(String hex)  throws  IOException, ClassNotFoundException dot.gif {
InBlock.gif    
return bytesToObject(hexStringToByte(hex));
ExpandedBlockEnd.gif}

None.gif
ExpandedBlockStart.gifContractedBlock.gif
/** */ /**
InBlock.gif    * @函数功能: BCD码转为10进制串(阿拉伯数据)
InBlock.gif    * @输入参数: BCD码
InBlock.gif    * @输出结果: 10进制串
ExpandedBlockEnd.gif    
*/

ExpandedBlockStart.gifContractedBlock.gif
public   static  String bcd2Str( byte [] bytes) dot.gif {
InBlock.gif    StringBuffer temp
=new StringBuffer(bytes.length*2);
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
for(int i=0;i<bytes.length;i++)dot.gif{
InBlock.gif     temp.append((
byte)((bytes[i]& 0xf0)>>>4));
InBlock.gif     temp.append((
byte)(bytes[i]& 0x0f));
ExpandedSubBlockEnd.gif    }

InBlock.gif    
return temp.toString().substring(0,1).equalsIgnoreCase("0")?temp.toString().substring(1):temp.toString();
ExpandedBlockEnd.gif}

None.gif
ExpandedBlockStart.gifContractedBlock.gif
/** */ /**
InBlock.gif    * @函数功能: 10进制串转为BCD码
InBlock.gif    * @输入参数: 10进制串
InBlock.gif    * @输出结果: BCD码
ExpandedBlockEnd.gif    
*/

ExpandedBlockStart.gifContractedBlock.gif
public   static   byte [] str2Bcd(String asc)  dot.gif {
InBlock.gif    
int len = asc.length();
InBlock.gif    
int mod = len % 2;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
if (mod != 0dot.gif{
InBlock.gif     asc 
= "0" + asc;
InBlock.gif     len 
= asc.length();
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
byte abt[] = new byte[len];
ExpandedSubBlockStart.gifContractedSubBlock.gif    
if (len >= 2dot.gif{
InBlock.gif     len 
= len / 2;
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
byte bbt[] = new byte[len];
InBlock.gif    abt 
= asc.getBytes();
InBlock.gif    
int j, k;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
for (int p = 0; p < asc.length()/2; p++dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif     
if ( (abt[2 * p] >= '0'&& (abt[2 * p] <= '9')) dot.gif{
InBlock.gif      j 
= abt[2 * p] - '0';
ExpandedSubBlockStart.gifContractedSubBlock.gif     }
 else if ( (abt[2 * p] >= 'a'&& (abt[2 * p] <= 'z')) dot.gif{
InBlock.gif      j 
= abt[2 * p] - 'a' + 0x0a;
ExpandedSubBlockStart.gifContractedSubBlock.gif     }
 else dot.gif{
InBlock.gif      j 
= abt[2 * p] - 'A' + 0x0a;
ExpandedSubBlockEnd.gif     }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif     
if ( (abt[2 * p + 1>= '0'&& (abt[2 * p + 1<= '9')) dot.gif{
InBlock.gif      k 
= abt[2 * p + 1- '0';
ExpandedSubBlockStart.gifContractedSubBlock.gif     }
 else if ( (abt[2 * p + 1>= 'a'&& (abt[2 * p + 1<= 'z')) dot.gif{
InBlock.gif      k 
= abt[2 * p + 1- 'a' + 0x0a;
ExpandedSubBlockStart.gifContractedSubBlock.gif     }
else dot.gif{
InBlock.gif      k 
= abt[2 * p + 1- 'A' + 0x0a;
ExpandedSubBlockEnd.gif     }

InBlock.gif
InBlock.gif     
int a = (j << 4+ k;
InBlock.gif     
byte b = (byte) a;
InBlock.gif     bbt[p] 
= b;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
return bbt;
ExpandedBlockEnd.gif}

ExpandedBlockStart.gifContractedBlock.gif
/** */ /**
InBlock.gif    * @函数功能: BCD码转ASC码
InBlock.gif    * @输入参数: BCD串
InBlock.gif    * @输出结果: ASC码
ExpandedBlockEnd.gif    
*/

ExpandedBlockStart.gifContractedBlock.gif
public   static  String BCD2ASC( byte [] bytes)  dot.gif {
InBlock.gif    StringBuffer temp 
= new StringBuffer(bytes.length * 2);
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
for (int i = 0; i < bytes.length; i++dot.gif{
InBlock.gif     
int h = ((bytes[i] & 0xf0>>> 4);
InBlock.gif     
int l = (bytes[i] & 0x0f);   
InBlock.gif     temp.append(BToA[h]).append( BToA[l]);
ExpandedSubBlockEnd.gif    }

InBlock.gif    
return temp.toString() ;
ExpandedBlockEnd.gif}

None.gif
ExpandedBlockStart.gifContractedBlock.gif
/** */ /**
InBlock.gif    * MD5加密字符串,返回加密后的16进制字符串
InBlock.gif    * 
@param origin
InBlock.gif    * 
@return
ExpandedBlockEnd.gif    
*/

ExpandedBlockStart.gifContractedBlock.gif
public   static  String MD5EncodeToHex(String origin)  dot.gif
InBlock.gif       
return bytesToHexString(MD5Encode(origin));
ExpandedBlockEnd.gif     }

None.gif
ExpandedBlockStart.gifContractedBlock.gif
/** */ /**
InBlock.gif    * MD5加密字符串,返回加密后的字节数组
InBlock.gif    * 
@param origin
InBlock.gif    * 
@return
ExpandedBlockEnd.gif    
*/

ExpandedBlockStart.gifContractedBlock.gif
public   static   byte [] MD5Encode(String origin) dot.gif {
InBlock.gif    
return MD5Encode(origin.getBytes());
ExpandedBlockEnd.gif}

None.gif
ExpandedBlockStart.gifContractedBlock.gif
/** */ /**
InBlock.gif    * MD5加密字节数组,返回加密后的字节数组
InBlock.gif    * 
@param bytes
InBlock.gif    * 
@return
ExpandedBlockEnd.gif    
*/

ExpandedBlockStart.gifContractedBlock.gif
public   static   byte [] MD5Encode( byte [] bytes) dot.gif {
InBlock.gif    MessageDigest md
=null;
ExpandedSubBlockStart.gifContractedSubBlock.gif    
try dot.gif{
InBlock.gif     md 
= MessageDigest.getInstance("MD5");
InBlock.gif     
return md.digest(bytes);
ExpandedSubBlockStart.gifContractedSubBlock.gif    }
 catch (NoSuchAlgorithmException e) dot.gif{
InBlock.gif     e.printStackTrace();
InBlock.gif     
return new byte[0];
ExpandedSubBlockEnd.gif    }

InBlock.gif  
ExpandedBlockEnd.gif}

None.gif
// 关于byte:    signed byte 把 0x00 ~ 0xff 映射成范围 0~127和 -128~-1    两段,比较简单的办法用 (b+256)%256的办法令其值回到0~255,或者用&0xff并赋给一个int。参考http: // www.jsfsoft.com:8080/beyond-pebble/pinxue/2006/08/23/1156309692525.html 
None.gif

转载于:https://www.cnblogs.com/kaixin110/archive/2007/12/11/990387.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值