杰瑞科技汇

Java中如何将ASCII转为BCD码?

Java ASCII转BCD实现

BCD (Binary-Coded Decimal) 是一种用二进制编码表示十进制数字的方法,在Java中,将ASCII字符串转换为BCD格式有几种常见方法。

Java中如何将ASCII转为BCD码?-图1
(图片来源网络,侵删)

直接转换(每个ASCII字符转换为4位BCD)

public class AsciiToBcd {
    public static byte[] asciiToBcd(String ascii) {
        if (ascii == null || ascii.length() % 2 != 0) {
            throw new IllegalArgumentException("Input string must have even length");
        }
        byte[] bcd = new byte[ascii.length() / 2];
        for (int i = 0; i < ascii.length(); i += 2) {
            char high = ascii.charAt(i);
            char low = ascii.charAt(i + 1);
            // 验证是否为数字
            if (!Character.isDigit(high) || !Character.isDigit(low)) {
                throw new IllegalArgumentException("Input string must contain only digits");
            }
            // 转换为BCD
            bcd[i / 2] = (byte) (((high - '0') << 4) | (low - '0'));
        }
        return bcd;
    }
    public static void main(String[] args) {
        String ascii = "12345678";
        byte[] bcd = asciiToBcd(ascii);
        // 打印BCD值
        for (byte b : bcd) {
            System.out.printf("%02X ", b);
        }
        // 输出: 12 34 56 78
    }
}

压缩BCD(每个字节表示两个十进制数字)

public class AsciiToBcd {
    public static byte[] asciiToPackedBcd(String ascii) {
        if (ascii == null || ascii.length() % 2 != 0) {
            throw new IllegalArgumentException("Input string must have even length");
        }
        byte[] packedBcd = new byte[ascii.length() / 2];
        for (int i = 0; i < ascii.length(); i += 2) {
            char high = ascii.charAt(i);
            char low = ascii.charAt(i + 1);
            // 验证是否为数字
            if (!Character.isDigit(high) || !Character.isDigit(low)) {
                throw new IllegalArgumentException("Input string must contain only digits");
            }
            // 转换为压缩BCD
            packedBcd[i / 2] = (byte) (((high - '0') << 4) | (low - '0'));
        }
        return packedBcd;
    }
    public static void main(String[] args) {
        String ascii = "12345678";
        byte[] packedBcd = asciiToPackedBcd(ascii);
        // 打印压缩BCD值
        for (byte b : packedBcd) {
            System.out.printf("%02X ", b);
        }
        // 输出: 12 34 56 78
    }
}

BCD转ASCII(反向操作)

public class BcdToAscii {
    public static String bcdToAscii(byte[] bcd) {
        StringBuilder ascii = new StringBuilder(bcd.length * 2);
        for (byte b : bcd) {
            // 高4位
            char high = (char) (((b & 0xF0) >> 4) + '0');
            // 低4位
            char low = (char) ((b & 0x0F) + '0');
            ascii.append(high).append(low);
        }
        return ascii.toString();
    }
    public static void main(String[] args) {
        byte[] bcd = {0x12, 0x34, 0x56, 0x78};
        String ascii = bcdToAscii(bcd);
        System.out.println(ascii); // 输出: 12345678
    }
}

注意事项

  1. 输入字符串必须是偶数长度,因为每个BCD字节表示两个十进制数字
  2. 输入字符串只能包含数字字符('0'-'9')
  3. BCD表示有两种形式:
    • 非压缩BCD:每个字节只表示一个十进制数字(高4位为0)
    • 压缩BCD:每个字节表示两个十进制数字(高4位和低4位各一个数字)

代码实现了压缩BCD的转换,这是更常用的形式,如果需要非压缩BCD,可以调整代码使每个字节只表示一个数字。

Java中如何将ASCII转为BCD码?-图2
(图片来源网络,侵删)
分享:
扫描分享到社交APP
上一篇
下一篇