OSS Nokalva 中的 Tbcd 支持

发布于 2025-01-01 22:41:52 字数 155 浏览 1 评论 0原文

正如我所看到的,OSS Nokalva Java api 支持 BCD 编码(ByteTool 类有 toBCD 和 parseBCD 方法),但不支持 TBCD。有人知道如何处理吗? 或者有谁知道如何在 Java 中实现从 String 到 TBCD 编码字节数组的转换,反之亦然? 提前致谢。

As I can see, OSS Nokalva Java api have support for BCD coding (ByteTool class have methods toBCD and parseBCD), but doesn't have for TBCD. Does anyone know how to deal with it?
Or does anyone know how to implement in Java conversion from String into TBCD encoded byte array and vice versa?
Thank in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

む无字情书 2025-01-08 22:41:52

OSS Nokalva 在其知识库中提供了有关 TBCD 的问答。请查看 http://www.oss.com/asn1/ knowledge-center/asn1-java/209.html 是一个可以下载用于处理 TBCD 的示例。

请注意,此处之前的答案包含不正确的代码,该代码依赖于现有的 ByteTool BCD 方法,而这些方法在一般情况下不适用于 TBCD。该代码将 TBCD 处理为交换半字节的 BCD。当 TBCD 不是有效的 BCD 时,ByteTool BCD 代码将针对有效的 TBCD 抛出 NumberFormatException。请参阅上面的 URL,了解正确处理 TBCD 的代码。

保罗

OSS Nokalva has a question and answer in their Knowledge Base regarding TBCD. Please look at http://www.oss.com/asn1/knowledge-center/asn1-java/209.html for an example you can download for handling TBCD.

Please note that the earlier answer here has incorrect code which relies on existing ByteTool BCD methods that do not work with TBCD in the general case. That code handles TBCD as BCD with nibbles swapped. The ByteTool BCD code will throw NumberFormatException for valid TBCD when it is not valid BCD. See the URL above for code that properly handles TBCD.

Paul

成熟的代价 2025-01-08 22:41:52

我会回答我自己。为了实现以字符串格式给出的十进制数的TBCD编码和解码,我做了以下方法:

该方法反转字节的高位和低位4位。

private byte reverseByte(byte bInput){
   return (byte) ((byte)((bInput & (byte)0x0f)<<4) + (byte)((bInput &(byte)0xf0)>>>4)&0x0f));
}

接受输入十进制字符串并返回 TBCD 编码字节数组的方法。该方法使用 OSS Nokalva java api 的 ByteTool 类中的 parseBCD 方法。

public static byte[] parseTBCD(String input){
byte[] tbcd = ByteTool.parseBCD(input);
for(int i=0; i<tbcd.length; i++) tbcd[i] = reverseByte(tbcd[i]);
return tbcd;
}

以及接受 TBCD 字节数组并将其转换为字符串十进制的方法。

public static String toTBCD(byte[] tbcd){
for(int i=0; i<tbcd.length; i++) tbcd[i] = reverseByte(tbcd[i]);
return ByteTool.toBCD(tbcd);
}

I will answer to myself. In order to implement TBCD encoding and decoding of decimal number given in String format I made the following methods:

This method reverse upper and lower 4 bits of byte.

private byte reverseByte(byte bInput){
   return (byte) ((byte)((bInput & (byte)0x0f)<<4) + (byte)((bInput &(byte)0xf0)>>>4)&0x0f));
}

Method that accept input decimal String and return TBCD encoded byte array. This method use a method parseBCD from ByteTool class of OSS Nokalva java api.

public static byte[] parseTBCD(String input){
byte[] tbcd = ByteTool.parseBCD(input);
for(int i=0; i<tbcd.length; i++) tbcd[i] = reverseByte(tbcd[i]);
return tbcd;
}

And the method that accept TBCD byte array and covert it to String decimal.

public static String toTBCD(byte[] tbcd){
for(int i=0; i<tbcd.length; i++) tbcd[i] = reverseByte(tbcd[i]);
return ByteTool.toBCD(tbcd);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文