This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
The moose likes Java in General and the fly likes BCD to decimal and viceversa Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "BCD to decimal and viceversa" Watch "BCD to decimal and viceversa" New topic

BCD to decimal and viceversa

Prashanth Babu

Joined: Nov 08, 2004
Posts: 6
Am working on conversion of decimal to BCD (Binary coded decimal) and viceversa. I require BCD for calling Cobol from Java using JNI.
Can anybody help me out in giving a pointer or an algorithm for doing the same.
I Googled on the web for any info on BCD conversion, but all in vain.
Please help me out.

Prashanth Babu.
Henry Wong

Joined: Sep 28, 2004
Posts: 20165

It has been years since I did this, but I am surprised that you could not find anything with google. BCD is simply the representation of decimal digits in 4 bits each... For example...

Number 42 ---> digit 4 is "0100" and digit 2 is "0010" ---> "01000010" in BCD. Just pull each decimal digit off, convert to 4 bits, and concat it back.


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Horatio Westock
Ranch Hand

Joined: Feb 23, 2005
Posts: 221
I'm no expert, COBOL was a bit before my time, but I think the BCD used is packed in some way. It might be worth checking, then searching the web for "Comp-3". I don't think it's drastically difficult to do the conversion, and I expect that someone has faced the same problem as you before.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
We've talked about packed decimal before. Don't know if I maybe posted a running solution once. Use the ranch search on the three Java in General forums.

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
I agree. Here's the link:
subject: BCD to decimal and viceversa
It's not a secret anymore!