Problem solve Get help with specific problems with your technologies, process and projects.

Converting a decimal to BCD in Java

Using Java, I need to convert a decimal to a packed BCD and back (packed BCD to Decimal), if possible, signed or unsigned. Can you help? I am hoping that there might be some open source software out here that I could work from. Would you know of any available open source that does this?
My God, someone asking about BCD. Next it'll be how to solve polynomials with VAX microcode. It looks like there's no free code anywhere, as far as I can tell, except for a small example in C called Jon's BCD Clock. It does unsigned-to-BCD only.

Scratching my head, it sounds pretty simple though. A DECIMAL type is probably implemented (for you) as a BigDecimal....

You can toString() that type. Once you've got a string, you're most of the way to BCD. Take each character, subtract "0" and stuff the result in a byte of a byte array. For signed numbers, that's a trickier proposition and depends on the packed format; I don't recall any signed BCD formats offhand.

This was last published in January 2005

Dig Deeper on Linux servers



Find more PRO+ content and other member only offers, here.

Have a question for an expert?

Please add a title for your question

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

Start the conversation

Send me notifications when other members comment.

By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Please create a username to comment.