none
I need to convert a signed integer to a packed numeric decimal number for a Mainframe application RRS feed

  • Question

  • I am looking forward to convert a signed integer to a packed numeric decimal number after padding some zeroes (10 will be total length) for a Mainframe application.

    e.g. 72365 will become 0 0 0 0 0 0 0 7 3 5
                                         0 0 0 0 0 0 0 2 6 D

    A negative number e.g.  -72365 will become 0 0 0 0 0 0 0 7 3 5
                                                                          0 0 0 0 0 0 0 2 6 E 

    Any leads how to achieve this ?

    Thanks in advance

     

                                        

    Wednesday, May 8, 2013 4:39 PM

Answers

  • Here is a VB Function that takes a signed Int64 and returns an array of 10 bytes containing the packed decimal equivalent of the Int64.  Note that I used "C" for positive and "D" for negative as I remember that being the convention in IBM assembler. If you need "D" and "E", you can change it the third statement inside the function.

    Function PackedDecimal(num As Long) As Byte()
        Const pLen As Integer = 10 'length of byte array
        Dim packed(pLen - 1) As Byte
        Dim decString As String = Math.Abs(num).ToString & If(num < 0, "D", "C")
        decString = decString.PadLeft(pLen * 2, "0"c)
        For i As Integer = pLen - 1 To 0 Step -1
            packed(i) = Byte.Parse(decString.Substring(i * 2, 2), _
          System.Globalization.NumberStyles.HexNumber)
        Next
        Return packed
    End Function


    Wednesday, May 8, 2013 9:23 PM