/** * Copyright 2010 Firat Salgur * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ public class BCD { public static byte[] DecimalToBCD(long num) { int digits = 0; long temp = num; while (temp != 0) { digits++; temp /= 10; } int byteLen = digits % 2 == 0 ? digits / 2 : (digits + 1) / 2; byte bcd[] = new byte[byteLen]; for (int i = 0; i < digits; i++) { byte tmp = (byte) (num % 10); if (i % 2 == 0) { bcd[i / 2] = tmp; } else { bcd[i / 2] |= (byte) (tmp << 4); } num /= 10; } for (int i = 0; i < byteLen / 2; i++) { byte tmp = bcd[i]; bcd[i] = bcd[byteLen - i - 1]; bcd[byteLen - i - 1] = tmp; } return bcd; } public static long BCDToDecimal(byte[] bcd) { return Long.valueOf(BCD.BCDtoString(bcd)); } public static String BCDtoString(byte bcd) { StringBuffer sb = new StringBuffer(); byte high = (byte) (bcd & 0xf0); high >>>= (byte) 4; high = (byte) (high & 0x0f); byte low = (byte) (bcd & 0x0f); sb.append(high); sb.append(low); return sb.toString(); } public static String BCDtoString(byte[] bcd) { StringBuffer sb = new StringBuffer(); for (int i = 0; i < bcd.length; i++) { sb.append(BCDtoString(bcd[i])); } return sb.toString(); } public static void main(String[] args) { System.out.println("Testing DecimalToBCD:"); BCD.testForValue(1L, "00000001"); BCD.testForValue(11L, "00010001"); BCD.testForValue(111L, "0000000100010001"); BCD.testForValue(1111L, "0001000100010001"); BCD.testForValue(11111L, "000000010001000100010001"); BCD.testForValue(42, "01000010"); BCD.testForValue(112233L, "000100010010001000110011"); BCD.testForValue(12345L, "000000010010001101000101"); System.out.println("\nTesting two way conversion using DecimalToBCD and back using BCDToDecimal:"); BCD.testForValue(1L); BCD.testForValue(11L); BCD.testForValue(111L); BCD.testForValue(1111L); BCD.testForValue(11111L); BCD.testForValue(12983283L); BCD.testForValue(9832098349L); } private static void testForValue(long val, String expected) { String binaryString = BCD.byteArrayToBinaryString(BCD.DecimalToBCD(val)); System.out.print(String.format("Testing: %10d -> %30s %4s\n", val, binaryString, binaryString.equals(expected) ? "[OK]" : "[FAIL]")); } private static void testForValue(long val) { long newVal = BCD.BCDToDecimal(BCD.DecimalToBCD(val)); System.out.print(String.format("Testing: %10d -> %30d %4s\n", val, newVal, newVal == val ? "[OK]" : "[FAIL]")); } private static String byteArrayToBinaryString(byte[] bytes) { StringBuffer sb = new StringBuffer(); for (byte i : bytes) { String byteInBinary = String.format("%8s", Integer.toBinaryString(i)).replace(' ', '0'); sb.append(byteInBinary); } return sb.toString(); } }
BCD変換 (Binary-coded decimal, 二進化十進数, byte[])
初めて、BCD変換をJAVAで対応したので、ついでにメモしとく
参考:
1)https://gist.github.com/neuro-sys/953548#file-test_output-txt
3)パック10進数
4)IccUtils
5)Algorithm to convert a String of decimal digits to BCD
相关推荐
Java Language Conversion Assistant is a tool that automatically converts existing Java-language code into Visual C#® Quick Info File Name:VS7.1-KB819018-X86.exe Download Size:10092 KB Date Published:...
二进制码转换BCD子程序,16位二进制转4位BCD码
Demo1Conversion.java
##GroupDocs.Conversion for Java ####Maven 示例 1.1.0如何安装/运行#####Windows/Linux/MacOS 下载 Java 库的 GroupDocs.Conversion 将 Java 库的 GroupDocs.Conversion 安装到本地 maven 存储库 将示例源下载到...
格子玻尔兹曼方法中的单位换算方法,ppt讲义
Java Language Conversion Assistant 是一种工具,它为那些想把现有的应用程序转到 .NET Framework 上的开发者实现从 Java 语言代码到 Visual C#® 的自动转换。 支持的操作系统: Windows 2000, Windows Server ...
世界风java源码地理坐标转换-Java 来自 NASA World Wind 项目的不同地理空间坐标系(Lat/Lon、UTM、UPS 和 MGRS)之间的转换功能(他们从用 C 编写的 GDAL 库翻译),减去对 AWT、图形库的所有依赖,他们的自定义...
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility) Java AC版本
各种进制间的转化函数,二进制和10进制,16进制,bcd码等的转化
转换库 只是一个包含不同方案的辅助类的小型库。 此时它只包含一个 Base64 编码类。 Base64 编码 二进制到文本编码以 ASCII 字符串格式呈现二进制数据。 测试基于以下 wiki 页面中的示例: :
Pic Project Binary to BCD conversion
Java Spring Boot application for currency conversion
C#编程 WinForm窗体开发 Conversion(源码)C#编程 WinForm窗体开发 Conversion(源码)C#编程 WinForm窗体开发 Conversion(源码)C#编程 WinForm窗体开发 Conversion(源码)C#编程 WinForm窗体开发 Conversion(源码)C#...
C-Java自动程序转换系统原型的设计和实现
Folder conversion: Entire folders of C# code files are converted to Java code files.(文件夹转换) 2. Code snippet and file conversion: Our snippet conversion accuracy is outstanding and does not ...
Conversion Optimization 这本书仔细的分析如何提高网站的转换率
JConvert 是一个免费的单位转换程序,具有友好的用户界面,也可以与外部应用程序一起使用。 可以轻松修改和/或添加转换。 支持多种语言。 下载并在几秒钟内运行! 现在就试试。
The sigma delta conversion technique has been in existence for many years, but recent technological advances now make the devices practical and their use is becoming widespread. The converters have ...
第二个类文件是Conversion.java,实现对文本文件转为二进制文件,再将其转为十六进制文件,从而实现对文件的加密解密操作。 第三个类文件是Base64.java,实现对文件的base64加密解密操作。 第四个类文件是Encode....
In this work we study all-optical multi-channel return-to-zero (RZ)–on-off keying (OOK) to nonreturn-to-zero (NRZ)–OOK format conversion in single uniform fiber Bragg grating (FBG) for mixed line-...