java的格式转换函数(持续更新添加)

JAVA的格式转换函数

因为所在的行业经常需要用到各种格式的报文,经常需要在各种格式之间转换,把用到过的函数都集中整理放到一起,方便以后可以直接用

//String转BCD编码字符数组,String=”1234567890ABCDEF”转换为byte[]={0x12,0x34,0x56,0x78,0x90,0xAB,0xCD,0xEF}
public static byte[] str2Bcd(String asc)
{
int len = asc.length();
int mod = len % 2;
if (mod != 0)
{
asc = “0” + asc;
len = asc.length();
}
byte abt[] = new byte[len];
if (len >= 2)
{
len = len / 2;
}
byte bbt[] = new byte[len];
abt = asc.getBytes();
int j, k;
for (int p = 0; p < asc.length() / 2; p++)
{
if ((abt[2 * p] >= ‘0’) && (abt[2 * p] <= ‘9’))
{
j = abt[2 * p] – ‘0’;
}
else if ((abt[2 * p] >= ‘a’) && (abt[2 * p] <= ‘z’))
{
j = abt[2 * p] – ‘a’ + 0x0a;
}
else
{
j = abt[2 * p] – ‘A’ + 0x0a;
}
if ((abt[2 * p + 1] >= ‘0’) && (abt[2 * p + 1] <= ‘9’))
{
k = abt[2 * p + 1] – ‘0’;
}
else if ((abt[2 * p + 1] >= ‘a’) && (abt[2 * p + 1] <= ‘z’))
{
k = abt[2 * p + 1] – ‘a’ + 0x0a;
}
else
{
k = abt[2 * p + 1] – ‘A’ + 0x0a;
}
int a = (j << 4) + k;
byte b = (byte) a;
bbt[p] = b;
System.out.format(“%02X\n”, bbt[p]);
}
return bbt;
}
//BCD编码字符数组转换为String字符串
public static String bcd2Str(byte[] bytes)
{
char temp[] = new char[bytes.length * 2], val;
for (int i = 0; i < bytes.length; i++)
{
val = (char) (((bytes[i] & 0xf0) >> 4) & 0x0f);
temp[i * 2] = (char) (val > 9 ? val + ‘A’ – 10 : val + ‘0’);
val = (char) (bytes[i] & 0x0f);
temp[i * 2 + 1] = (char) (val > 9 ? val + ‘A’ – 10 : val + ‘0’);
}
return new String(temp);
}
//ASCII字符转换BCD编码字符
public static byte asc_to_bcd(byte asc)
{
byte bcd;
if ((asc >= ‘0’) && (asc <= ‘9’))
bcd = (byte) (asc – ‘0’);
else if ((asc >= ‘A’) && (asc <= ‘F’))
bcd = (byte) (asc – ‘A’ + 10);
else if ((asc >= ‘a’) && (asc <= ‘f’))
bcd = (byte) (asc – ‘a’ + 10);
else
bcd = (byte) (asc – 48);
return bcd;
}
 //ASCII字符数组转换为BCD编码格式数组
public static byte[] ASCII_To_BCD(byte[] ascii, int asc_len)
{
byte[] bcd = new byte[asc_len / 2];
int j = 0;
for (int i = 0; i < (asc_len + 1) / 2; i++)
{
bcd[i] = asc_to_bcd(ascii[j++]);
bcd[i] = (byte) (((j >= asc_len) ? 0x00 : asc_to_bcd(ascii[j++])) + (bcd[i] << 4));
System.out.format(“%02X\n”, bcd[i]);
}
return bcd;
}

 //char数组转换为Byte数组

//char[] to byte[]
public byte[] char2byte (char[] chars)
{
    Charset cs = Charset.forName ("UTF-8");
    CharBuffer cb = CharBuffer.allocate (chars.length);
    cb.put (chars);
    cb.flip ();
    ByteBuffer bb = cs.encode (cb);
    return bb.array();
}

//byte数组转换为char数组

//byte[] to char[]
public char[] byte2char (byte[] bytes)
{
    Charset cs = Charset.forName ("UTF-8");
    ByteBuffer bb = ByteBuffer.allocate (bytes.length);
    bb.put (bytes);
    bb.flip ();
    CharBuffer cb = cs.decode (bb);
    return cb.array();
}

 //将二进制byte字符串转换为string,bin to hex

//byte[] to string
public static String bin2hex(byte[] b)
{
    // using the default system Locale
    Locale defloc = Locale.getDefault();

    String str = "";
    for (int i = 0; i < b.length; i++) {
        String hex = Integer.toHexString(b[i] & 0xFF);
        if (hex.length() == 1) {
            hex = '0' + hex;
        }

        if (i > 0)
            if ((i % 16 == 0)) {
                str += "\n";
            } else if ((i % 8 == 0)) {
                str += " - ";
            } else {
                str += " ";
            }
        str += hex.toUpperCase(defloc);
    }
    return str;
}

 //将Byte[]与hex字符串相互转换

private final static byte[] hex = "0123456789ABCDEF".getBytes();

private static int parse(char c) {
    if (c >= 'a')
        return (c - 'a' + 10) & 0x0f;
    if (c >= 'A')
        return (c - 'A' + 10) & 0x0f;
    return (c - '0') & 0x0f;
}

/**
 * 从字节数组到十六进制字符串转换
 * @param b bytes buffer
 * @return convert result
 */
public static String Bytes2HexString(byte[] b) {
    byte[] buff = new byte[2 * b.length];
    for (int i = 0; i < b.length; i++) {
        buff[2 * i] = hex[(b[i] >> 4) & 0x0f];
        buff[2 * i + 1] = hex[b[i] & 0x0f];
    }
    return new String(buff);
}

/**
 * 从十六进制字符串到字节数组转换
 * @param hexstr 16进制显示的字符串
 * @return convert result
 */
public static byte[] HexString2Bytes(String hexstr) {
    byte[] b = new byte[hexstr.length() / 2];
    int j = 0;
    for (int i = 0; i < b.length; i++) {
        char c0 = hexstr.charAt(j++);
        char c1 = hexstr.charAt(j++);
        b[i] = (byte) ((parse(c0) << 4) | parse(c1));
    }
    return b;
}

发表评论