一个字节处理类

来源:互联网 发布:linux启动mysql失败 编辑:程序博客网 时间:2024/05/20 18:48

这是一个很有价值的字节处理类,在我的万能表单解析程序中直接加以引用,现转载,以示对原作者的敬意。



import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;


/**
 *这个类来自《电脑编程技巧与维护》2007合订本 实例33
 * @author PC_WalkerMa
 */
public class ByteProcess {
    static private final String CharSet = "0123456789ABCDEF";
    
    public static int HexCharToNum(String ch){
        ch = ch.toUpperCase();
        return CharSet.indexOf(ch);
    }
    
    public static byte TwoHexCharsToByte(String chs){
        int i,j;
        
        chs = chs.toUpperCase();
        if (chs.length() < 2) return -1;
        i = HexCharToNum(chs.substring(0,1));
        j = HexCharToNum(chs.substring(1,2));
        
        return (byte)(i*16+j); 
    }
    
    public static byte[] HexCharsToBytes(String chs){
        int i, nLen;
        byte bTmp;
        
        nLen = (chs.length())/2;
        byte[] bResult = new byte[nLen];
        
        for (i=0; i<nLen; i++){
            bTmp = TwoHexCharsToByte(chs.substring(i*2,(i+1)*2));
            bResult[i] = bTmp;
        }
        
        return bResult;
    }
    
    public static String ByteToTwoHexChar(byte bIn) {
        int i, j;
        
        String strResult = "";
        i = ((int)bIn >> 4 & 0xf);
        strResult += CharSet.substring(i, i+1);
        
        j = (int)bIn & 0xf;
        strResult += CharSet.substring(j, j+1);
        
        return strResult;
    }
    
    public static String BytesToHexChars(byte[] bIn){
        int nLen;
        String strResult = "";
        
        nLen = bIn.length;
        
        for(int i=0; i<nLen; i++){
            strResult += ByteToTwoHexChar(bIn[i]);
        }
        
        return strResult;
    }
    
    public static String InsertSpaceToHexChars(String strIn){
        int nLen;
        String strResult = "";
        
        nLen = strIn.length();
        
        for(int i=0; i<nLen/2; i++){
            strResult += strIn.substring(i*2, (i+1)*2) + " ";
        }
        
        return strResult;
    }
    
    public static String DeleteSpaceFromHexChars(String strIn){
        int nLen;
        String strTmp;;
        String strResult = "";
        
        nLen = strIn.length();
        for(int i=0; i<nLen; i++){
            strTmp = (String)(strIn.subSequence(i,i+1));
            if(strTmp.equals(" ") == false) strResult = strResult.concat(strTmp);
        }
        
        return strResult;
    }
    
    
    public static byte[] GetBytesFromByteBuffer(ByteBuffer buf){
        int bLen;
        byte[] bResult;
        
        bLen = buf.position();
        if (bLen>0){
            bResult = new byte[bLen];
            buf.rewind();
            buf.get(bResult, 0, bLen);
            return bResult;
        }
        else return null;
    }
    
    public static String BytesToEnString(byte[] bIn){
        String strOut = "";
        
        try {
            strOut= new String(bIn, "UTF-8");
        } catch (UnsupportedEncodingException e) {}
 
        return strOut;
    }
    
    public static byte[] EnStringToBytes(String strIn){
        byte[] bOut = new byte[strIn.length()];
        
        try {
            bOut = strIn.getBytes("UTF-8");
        } catch (UnsupportedEncodingException e) {}
   
        return bOut;
    }
    
    public static byte[] StringToUnicode(String strIn){
        byte[] bOut = new byte[strIn.length()*2];
        
        try {
            bOut = strIn.getBytes("UTF-16BE");
        } catch (UnsupportedEncodingException e) {}
   
        return bOut;
     }
     
    public static String StringToUnicodeHexChars(String strIn){
        return (BytesToHexChars(StringToUnicode(strIn)));
    }
    
    public static String UnicodeToString(byte[] bIn) {
        String strOut = "";
        
        try {
            strOut = new String(bIn, "UTF-16BE");
        } catch (UnsupportedEncodingException e) {}
        
        return strOut;
    }
    
    public static String UnicodeHexCharsToString(String strIn){
        return (UnicodeToString(HexCharsToBytes(strIn))); 
    }
}