• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How can I convert Decimal value to Packed Format

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
could you explain a bit more as to what packed format means???
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:

 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And welcome to JavaRanch
 
reply
    Bookmark Topic Watch Topic
  • New Topic