Forums Register Login

How can I convert Decimal value to Packed Format

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
could you explain a bit more as to what packed format means???
+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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.

 
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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:

+Pie Number of slices to send: Send
Not a very helpful answer to Stan, I am afraid. He died in 2007. Beware of old posts; please look at this FAQ.
+Pie Number of slices to send: Send
And welcome to JavaRanch
I'm a lumberjack and I'm okay, I sleep all night and work all day. Lumberjack ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 8849 times.
Similar Threads
How can I convert Decimal value to Packed format
conversion
Reg. Integer literals
ASCII TO EBCDIC conversion preserving COMP-3
Packed decimal to normal Ascii integer conversion
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 09:20:00.