0

I have the next string:

String points = "210987654321";

and I need to end up in the 6 byte array in bcd format which looks like this (same digits):

byte [] result = { 0x21, 0x09, (byte) 0x87, 0x65, 0x43, 0x21 };

How do I get there? points.getBytes(); gives not what I need.

IKo
  • 4,998
  • 8
  • 34
  • 54

1 Answers1

3

Based on the code you provided and its expected output it looks like you're trying to convert into the BCD format (BCD is a rather generic definition) normally used to represent a string in which each letter represents a HEX digit into its corresponding byte representation (from comments to this answer: "packed BCD").

There are solutions in StackOverflow, such as the following:

In the interest of "not reinventing the wheel", however, I would favour functionality available in libraries like Apache Commons or Google Guava:

For Apache Commons it boils down to roughly this code:

byte[] result = Hex.decodeHex(points)

It is to be noted however that the above code will accept as inputs also strings containing letters up to the letter "F" so if that's not acceptable to you, you will have to make sure that your string only contains numbers.


As BCD comprises a number of variants, I believe it's worth mentioning that before applying a BCD conversion you should determine which specific conversion you want to apply based on your software's purposes.

In the financial/services sector, for example, it's very common the need to convert into the EBCDIC format (http://ascii-table.com/ebcdic-table.php) to interact with IBM systems. In such case I recommend checking the following StackOverflow question and its related answers:


Finally, if you really meant converting into another BCD format, you may have write your own code based the rules governing your specific BCD format. Before you start coding, of course, I still recommend looking for libraries performing the conversion for you.

Filippo Possenti
  • 1,300
  • 8
  • 18
  • BCD format is not a "rather generic definition". BCD is well-defined, and ancient (computer-wise), see e.g. [Wikipedia](https://en.wikipedia.org/wiki/Binary-coded_decimal). The bytes in the question is actually [Packed BCD](https://en.wikipedia.org/wiki/Binary-coded_decimal#Packed_BCD), and to quote Wikipedia: *Packed BCD has been in use since at least the **1960s***. – Andreas Feb 11 '20 at 09:57
  • The article in Wikipedia starts saying that it's a "class of binary encodings"... meaning it's generic. The article then goes on listing some of the specific encodings... again meaning that saying BCD is not sufficient to identify the specific variant you want. That's why I both provided information about what the user appears to want and information about one of the most common variants (EBCDIC). I will concede however that converting a HEX string into its byte representation is a BCD conversion: I updated the answer accordingly. – Filippo Possenti Feb 11 '20 at 10:04
  • Ok, but looking at the bytes in the question, it is clearly not EBCDIC, so that part of your answer seems farfetched. – Andreas Feb 11 '20 at 10:11
  • 1
    Agreed. I still find it useful for users generically searching for BCD, as it gives insight that they might want to understand which specific variant they want and EBCDIC is unquestionably (and unfortunately, in my opinion) a rather common one. – Filippo Possenti Feb 11 '20 at 10:14