# Convert Packed decimal to decimal in ruby or shell

Hi Friends,

I am trying to convert packed decimal to decimal by using ruby or shell
but i dont have proper idea.
Input:ƒáÅiŒ
Output:123456789012

suggest me how to convert or if any library or functions or code
snippets.

Arun P. wrote in post #1025789:

I am trying to convert packed decimal to decimal by using ruby or shell
but i dont have proper idea.
Input:ƒáÅiŒ
Output:123456789012

That’s odd looking input, because it’s of undefined character set. Can
you provide some clean, unambiguous input? (e.g. in hex)

suggest me how to convert or if any library or functions or code
snippets.

When you say “packed decimal” do you mean Binary Coded Decimal, where
one byte contains two hex digits?

irb(main):005:0> [123456789012.to_s].pack(“H*”)
=> “\0224Vx\220\022”

Notice that’s the same as “\x12\x34\x56\x78\x90\x12”. Warning: if the
string has an odd number of characters then you need to add a leading
“0” to it to get the right answer. I haven’t done that here because the
string happened to be an even number of characters.

Then to reverse it:

irb(main):009:0> str = “\x12\x34\x56\x78\x90\x12”
=> “\0224Vx\220\022”
irb(main):010:0> str.unpack(“H*”).first.to_i
=> 123456789012

That’s 6 characters, which doesn’t look anything like the ones you
provided.

Or do you just mean a direct conversion from decimal to binary?

irb(main):011:0> 123456789012.to_s(16)
=> “1cbe991a14”
irb(main):012:0> [123456789012.to_s(16)].pack(“H*”)
=> “\034\276\231\032\024”
irb(main):013:0> str = “\x1c\xbe\x99\x1a\x14”
=> “\034\276\231\032\024”
irb(main):014:0> str.unpack(“H*”).first.to_i(16)
=> 123456789012

Again, at the first step you should ensure that your string has an even
number of hex digits before packing. Fortunately in this case it does.

That gives 5 characters, although they look nothing like the 5
characters you showed.