aspose file tools*
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 The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "BCD to decimal and viceversa" Watch "BCD to decimal and viceversa" New topic
Author

BCD to decimal and viceversa

Prashanth Babu
Greenhorn

Joined: Nov 08, 2004
Posts: 6
Hi,
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.

TIA,
Prashanth Babu.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18743
    
  40

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.

Henry


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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: BCD to decimal and viceversa