BCD编码及转码

2024-03-19 21:58
文章标签 编码 转码 bcd

本文主要是介绍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 ((buf[end] & 0xf) == 0xf) {l += (buf[end] & 0xf0) >> 4;power *= 10L;end--;}for (int i = end; i >= pos; i--) {l += (buf[i] & 0x0f) * power;power *= 10L;l += ((buf[i] & 0xf0) >> 4) * power;power *= 10L;}return l;}/** Encode the value as BCD and put it in the buffer. The buffer must be big enough* to store the digits in the original value (half the length of the string). */public static void encode(String value, byte[] buf) {int charpos = 0; //char where we startint bufpos = 0;if (value.length() % 2 == 1) {//for odd lengths we encode just the first digit in the first bytebuf[0] = (byte)(value.charAt(0) - 48);charpos = 1;bufpos = 1;}//encode the rest of the stringwhile (charpos < value.length()) {buf[bufpos] = (byte)(((value.charAt(charpos) - 48) << 4)| (value.charAt(charpos + 1) - 48));charpos += 2;bufpos++;}}/** Encode the value as BCD and put it in the buffer. The buffer must be big enough* to store the digits in the original value (half the length of the string).* If the value contains an odd number of digits, the last one is stored in* its own byte at the end, padded with an F nibble. */public static void encodeRightPadded(String value, byte[] buf) {int bufpos = 0;int charpos = 0;int limit = value.length();if (limit % 2 == 1) {limit--;}//encode the rest of the stringwhile (charpos < limit) {buf[bufpos] = (byte)(((value.charAt(charpos) - 48) << 4)| (value.charAt(charpos + 1) - 48));charpos += 2;bufpos++;}if (value.length() % 2 == 1) {buf[bufpos] = (byte)(((value.charAt(limit) - 48) << 4)| 0xf);}}/** Decodes a BCD-encoded number as a BigInteger.* @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 BigInteger decodeToBigInteger(byte[] buf, int pos, int length)throws IndexOutOfBoundsException {char[] digits = new char[length];int start = 0;int i = pos;if (length % 2 != 0) {digits[start++] = (char)((buf[i] & 0x0f) + 48);i++;}for (;i < pos + (length / 2) + (length % 2); i++) {digits[start++] = (char)(((buf[i] & 0xf0) >> 4) + 48);digits[start++] = (char)((buf[i] & 0x0f) + 48);}return new BigInteger(new String(digits));}/** Decodes a right-padded BCD-encoded number as a BigInteger.* @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 BigInteger decodeRightPaddedToBigInteger(byte[] buf, int pos, int length)throws IndexOutOfBoundsException {char[] digits = new char[length];int start = 0;int i = pos;int limit = pos + (length / 2) + (length % 2);for (;i < limit; i++) {digits[start++] = (char)(((buf[i] & 0xf0) >> 4) + 48);int r = buf[i] & 0xf;digits[start++] = r == 15 ? ' ' : (char)(r + 48);}return new BigInteger(new String(digits, 0, start).trim());}/** Convert two bytes of BCD length to an int,* e.g. 0x4521 into 4521, starting at the specified offset. */public static int parseBcdLength(byte b) {return (((b & 0xf0) >> 4) * 10) + (b & 0xf);}/** Convert two bytes of BCD length to an int,* e.g. 0x4521 into 4521, starting at the specified offset. */public static int parseBcdLength2bytes(byte[] b, int offset) {return (((b[offset] & 0xf0) >> 4) * 1000) + ((b[offset] & 0xf) * 100) +(((b[offset + 1] & 0xf0) >> 4) * 10) + (b[offset + 1] & 0xf);}
}

decodeToLong 解码BCD成long型(左补,右靠)
decodeRightPaddedToLong 解码BCD成long型 (右补,左靠)
decodeToBigInteger 解码BCD成BigInteger型(左补,右靠)
decodeRightPaddedToBigInteger 解码BCD成BigInteger型(右补,左靠)
encode 压缩成BCD码(左补,右靠)
encodeRightPadded 压缩成BCD码(右补,左靠)

动手测试下,偶数:

public static void main(String[] args) throws Exception {byte[] buf = new byte[1];Bcd.encode("40",buf);System.out.println(new String(buf));System.out.println(Bcd.decodeToLong(buf, 0, 1));System.out.println(Bcd.decodeRightPaddedToLong(buf, 0, 1));Bcd.encodeRightPadded("40",buf);System.out.println(new String(buf));System.out.println(Bcd.decodeToLong(buf, 0, 1));System.out.println(Bcd.decodeRightPaddedToLong(buf, 0, 1));
}

结果:
在这里插入图片描述

奇数:

public static void main(String[] args) throws Exception {byte[] buf = new byte[2];Bcd.encode("120",buf);System.out.println(new String(buf));System.out.println(Bcd.decodeToLong(buf, 0, 3));System.out.println(Bcd.decodeRightPaddedToLong(buf, 0, 3));Bcd.encodeRightPadded("123",buf);System.out.println(new String(buf));System.out.println(Bcd.decodeToLong(buf, 0, 3));System.out.println(Bcd.decodeRightPaddedToLong(buf, 0, 3));
}

结果:
在这里插入图片描述

这玩意奇数还是的考虑清楚压缩对齐方式,以上。

这篇关于BCD编码及转码的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/827351

相关文章

Python动态处理文件编码的完整指南

《Python动态处理文件编码的完整指南》在Python文件处理的高级应用中,我们经常会遇到需要动态处理文件编码的场景,本文将深入探讨Python中动态处理文件编码的技术,有需要的小伙伴可以了解下... 目录引言一、理解python的文件编码体系1.1 Python的IO层次结构1.2 编码问题的常见场景二

Java中字符编码问题的解决方法详解

《Java中字符编码问题的解决方法详解》在日常Java开发中,字符编码问题是一个非常常见却又特别容易踩坑的地方,这篇文章就带你一步一步看清楚字符编码的来龙去脉,并结合可运行的代码,看看如何在Java项... 目录前言背景:为什么会出现编码问题常见场景分析控制台输出乱码文件读写乱码数据库存取乱码解决方案统一使

Java实现字节字符转bcd编码

《Java实现字节字符转bcd编码》BCD是一种将十进制数字编码为二进制的表示方式,常用于数字显示和存储,本文将介绍如何在Java中实现字节字符转BCD码的过程,需要的小伙伴可以了解下... 目录前言BCD码是什么Java实现字节转bcd编码方法补充总结前言BCD码(Binary-Coded Decima

Java 中编码与解码的具体实现方法

《Java中编码与解码的具体实现方法》在Java中,字符编码与解码是处理数据的重要组成部分,正确的编码和解码可以确保字符数据在存储、传输、读取时不会出现乱码,本文将详细介绍Java中字符编码与解码的... 目录Java 中编码与解码的实现详解1. 什么是字符编码与解码?1.1 字符编码(Encoding)1

基于Go语言实现Base62编码的三种方式以及对比分析

《基于Go语言实现Base62编码的三种方式以及对比分析》Base62编码是一种在字符编码中使用62个字符的编码方式,在计算机科学中,,Go语言是一种静态类型、编译型语言,它由Google开发并开源,... 目录一、标准库现状与解决方案1. 标准库对比表2. 解决方案完整实现代码(含边界处理)二、关键实现细

解决IDEA报错:编码GBK的不可映射字符问题

《解决IDEA报错:编码GBK的不可映射字符问题》:本文主要介绍解决IDEA报错:编码GBK的不可映射字符问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录IDEA报错:编码GBK的不可映射字符终端软件问题描述原因分析解决方案方法1:将命令改为方法2:右下jav

Python使用自带的base64库进行base64编码和解码

《Python使用自带的base64库进行base64编码和解码》在Python中,处理数据的编码和解码是数据传输和存储中非常普遍的需求,其中,Base64是一种常用的编码方案,本文我将详细介绍如何使... 目录引言使用python的base64库进行编码和解码编码函数解码函数Base64编码的应用场景注意

VSCode中C/C++编码乱码问题的两种解决方法

《VSCode中C/C++编码乱码问题的两种解决方法》在中国地区,Windows系统中的cmd和PowerShell默认编码是GBK,但VSCode默认使用UTF-8编码,这种编码不一致会导致在VSC... 目录问题方法一:通过 Code Runner 插件调整编码配置步骤方法二:在 PowerShell

Python如何实现读取csv文件时忽略文件的编码格式

《Python如何实现读取csv文件时忽略文件的编码格式》我们再日常读取csv文件的时候经常会发现csv文件的格式有多种,所以这篇文章为大家介绍了Python如何实现读取csv文件时忽略文件的编码格式... 目录1、背景介绍2、库的安装3、核心代码4、完整代码1、背景介绍我们再日常读取csv文件的时候经常

C++ | Leetcode C++题解之第393题UTF-8编码验证

题目: 题解: class Solution {public:static const int MASK1 = 1 << 7;static const int MASK2 = (1 << 7) + (1 << 6);bool isValid(int num) {return (num & MASK2) == MASK1;}int getBytes(int num) {if ((num &