This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How can I convert Decimal value to Packed Format

 
Ravi Pydi
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
could you explain a bit more as to what packed format means???
 
Maarten Vergouwen
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to JavaRanch
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic