BCD编码及转码

简单介绍

摘取一段百度百科介绍:
BCD码(Binary-Coded Decimal‎),用4位二进制数来表示1位十进制数中的0~9这10个数码,是一种二进制的数字编码形式,用二进制编码的十进制代码。

常见的BCD码又分8421码、5421码、2421码、余3码、余3循环码。

个人理解BCD码:将十进制数压缩存储。

拿8421(四位,每位为1代表十进制数。1110等于8+4+2+0 = 14)BCD码举例:
十进制数40
4转成8421格式二进制:0100
0转成8421格式二进制:0000

众所周知:
4个二进制 = 1个十六进制
2个十六进制 = 1个字节(byte)

即:
十进制数40 = 0100 0000 = 0x40 = @ (0x40 = @ 参考ascii对照规则)
看结果,原来十进制数40占用两个字节的存储空间,现在@字符只用占用一个。

转码使用

经过介绍,知道BCD是可以将原来存储空间缩减大约一半(分奇偶长度考虑,偶数一半,奇数差一点)。

对于奇数长度的十进制压缩成BCD码,就存在一个问题,到底是左对齐还是右对齐。

推荐一个前辈写的bcd转码方法

/*
 * j8583 A Java implementation of the ISO8583 protocol
 * Copyright (C) 2007 Enrique Zamudio Lopez
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 */
package com.swiftplus.posservice.prepose.iso8583.util;

import java.math.BigInteger;
import java.text.ParseException;

/**
 * Routines for Binary Coded Digits.
 *
 * @author Enrique Zamudio
 *         Date: 23/04/13 11:24
 */
public final class Bcd {
   

    private Bcd(){
   }

    /** Decodes a BCD-encoded number as a long.
     * @param buf The byte buffer containing the BCD data.
     * @param pos The starting position in the buffer.
     * @param length The number of DIGITS (not bytes) to read. */
    public static long decodeToLong(byte[] buf, int pos, int length)
            throws IndexOutOfBoundsException {
   
        if (length > 18) {
   
            throw new IndexOutOfBoundsException("Buffer too big to decode as long");
        }
        long l = 0;
        long power = 1L;
        for (int i = pos + (length / 2) + (length % 2) - 1; i >= pos; i--) {
   
            l += (buf[i] & 0x0f) * power;
            power *= 10L;
            l += ((buf[i] & 0xf0) >> 4) * power;
            power *= 10L;
        }
        return l;
    }

    public static long decodeRightPaddedToLong(byte[] buf, int pos, int length)
            throws IndexOutOfBoundsException {
   
        if (length > 18) {
   
            throw new IndexOutOfBoundsException("Buffer too big to decode as long");
        }
        long l = 0;
        long power = 1L;
        int end = pos + (length / 2) + (length % 2) - 1;
        if (
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值