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
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 ]
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.
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.
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
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 ]
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