aspose file tools*
The moose likes Java in General and the fly likes How can I convert Decimal value to Packed Format Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How can I convert Decimal value to Packed Format " Watch "How can I convert Decimal value to Packed Format " New topic
Author

How can I convert Decimal value to Packed Format

Ravi Pydi
Greenhorn

Joined: Sep 01, 2002
Posts: 25
Hello,
Could some one help me, how to convert Decimal value to Packed format and vice-versa
for example
Decimal value Packed Format
+123 12 3C or 12 3F
I appreciate if you send sample java code
Thanks in Advance
Ravi
Ram Mohan
Ranch Hand

Joined: Jan 19, 2004
Posts: 44
could you explain a bit more as to what packed format means???


If quitters never win, and winners never cheat, who's the idiot that said: Quit while you're ahead?
Maarten Vergouwen
Ranch Hand

Joined: Jan 12, 2004
Posts: 60
Ravi,
you cannot convert a value to a format; I assume your question is actually how to convert one format into another format, in your case going from a string representation of a number to a packed format of that number.
The numeric formats like Int, Long etcetera are all stored packed (write them to a file and see). Strings are stored in a 'readable' way.
(If it helps, in mainframe (cobol) terminology, Strings are stored like CHARACTER type, and ints etc are stored like PACKED DECIMAL types.)
You could do this by using the .valueOf and .toString methods of the String and Integer objects.
Alternatively if you want to use primitives, you could chop up a string into chars I suppose.
Hope this helps, if not and you meant something else you'd have to be more specific in your question.
[ February 02, 2004: Message edited by: Maarten Vergouwen ]
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I meant to play with this over the weekend but took a nap instead.
You need to set the left & right "nibbles" of a packed byte independently. The "or" operator will let you do this. So to pack 321 ...

That gives us two bytes '321F'.
Now we have to figure out some kind of loop that will step through the digits of the input and step through an array of output bytes. We can get the digits from an int like this:

This is a bit yucky. We can also get them with something like this:

This takes advantage of knowing that the character "1" is x'31' in ASCII and we can subtract the 30. Coming from mainframe EBCEDIC background, it took me years to be comfortable with relying on ASCII.
Some times we have mix our digit into the LEFT nibble of the output byte. We can shift an int to do that.

Whew. Are you seeing any light at the end of the tunnel? This explored some manipulation but didn't yet suggest what the loop will look like. Lemme know if this was something you can use to get to the next step.


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
Ravi Pydi
Greenhorn

Joined: Sep 01, 2002
Posts: 25
Originally posted by Stan James:
I meant to play with this over the weekend but took a nap instead.
You need to set the left & right "nibbles" of a packed byte independently. The "or" operator will let you do this. So to pack 321 ...

That gives us two bytes '321F'.
Now we have to figure out some kind of loop that will step through the digits of the input and step through an array of output bytes. We can get the digits from an int like this:

This is a bit yucky. We can also get them with something like this:

This takes advantage of knowing that the character "1" is x'31' in ASCII and we can subtract the 30. Coming from mainframe EBCEDIC background, it took me years to be comfortable with relying on ASCII.
Some times we have mix our digit into the LEFT nibble of the output byte. We can shift an int to do that.

Whew. Are you seeing any light at the end of the tunnel? This explored some manipulation but didn't yet suggest what the loop will look like. Lemme know if this was something you can use to get to the next step.
Ravi Pydi
Greenhorn

Joined: Sep 01, 2002
Posts: 25
Hi Stan James
Thank you so much for the reply,but I was not getting properly what you explained in there , could you explain it to me in detail, and is the language you were using was java ??? please reply back with more info ,even if its not in java I could try using your logic into java
thank you once again
ravi
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I sent this in a private note, but here it is just in case anybody else cared. I really wrote every bit of this test first, so here's the test first:

And the PackedDecimal class ...

Couple notes ... are 0F and 0C right for the signs? It's been a while and I might have remembered them wrong. The int to byte loop could terminate any time the int == 0. I just didn't put the "break" tests in. The philosophy is: prove that it's slow before you muck it up.
[ February 04, 2004: Message edited by: Stan James ]
Thomas Autera
Greenhorn

Joined: Nov 10, 2003
Posts: 14
F is unsigned, neither negative/positive; x'123F' = 123 (dec)
C is a positive sign, x'123C' = +123 (dec)
D is a negative sign, x'123D' = -123 (dec)
-Thomas
Douglas Kelly
Greenhorn

Joined: Sep 29, 2009
Posts: 1
Hey Stan: your test needs to include the digits 9 and 8...the code bytes to int routine doesn't work for these digits since the high order bit is on.

I changed the internal number to long instead of int so I could handle larger numbers, so mInt is long also. Here's my update to the byte constructor:

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
Not a very helpful answer to Stan, I am afraid. He died in 2007. Beware of old posts; please look at this FAQ.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
And welcome to JavaRanch
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How can I convert Decimal value to Packed Format