Java BCD数组与字符串互转

/**
     * BCD数组转字符串,比如设备id、sim卡号等
     * @param bytes
     * @return
     */
    public static String bcdArrToString(byte[] bytes) {
        Charset cset = Charset.forName("GBK");
        ByteBuffer bbuf = ByteBuffer.wrap(bytes, 0, bytes.length);
        CharBuffer cbuf = cset.decode(bbuf);
        return cbuf.toString();
    }

    /**
     * 字符串转BCD数组,比如设备id、sim卡号等
     * @param s
     * @return
     */
    public static byte[] StringToBcdArr(String s) {
        if (s == null || s.length() == 0) return null;
        byte[] bytes = new byte[s.length()];
        char[] cc = s.toCharArray();
        int i = 0;
        for (char c: cc) {
            bytes[i] = (byte) (c-0);
            i++;
        }
        return bytes;
    }

 

posted @ 2023-07-12 16:37  都是城市惹的祸  阅读(414)  评论(0)    收藏  举报