Wednesday, March 30, 2011

How do I convert VB's Double to COBOL's COMP-3?

Hi All,

Does anyone here know how to convert a VB Double to Cobol S9(15)V99 Comp-3 data type. Any help would be appreciated.

From stackoverflow
  • Does it need to be done in the same program? It seems to me it would be easier to find a common format that both VB and COBOL can understand.

    That would be ... TEXT ! Yes, is it an option to write the code out to a file as text "3.14159" and have the COBOL code read it in in that format and MOVE it to the comp-field?

    If that's not possible, the COMP-3 is a fairly simple BCD type. I would convert the number to a string anyway then take it two characters at a time into a byte array.

    The S9(15)V99 requires 18 nybbles (a nybble being 4 bits, or half an octet) to store:

    • one for the sign.
    • 15 for the integer bit.
    • 2 for the non-integer bit (V is an implied decimal, not a real one).

    So the number 3.14 would be represented as the bytes:

    00 00 00 00 00 00 00 31 4C
    

    The only tricky bit is that final sign nybble (C for positive and D for negative).

    Here's a bit of code I whipped up in Excel VBA (I don't have VB installed on this machine unfortunately) that shows you how to do it. The makeComp3() function should be easily transferred into a real VB program.

    The macro test program outputs the values 0, 49 and 76 which are hex 00, 31 and 4C respectively (00314C is +3.14).

    Option Explicit
    
    ' makeComp3. '
    '   inp is the double to convert. '
    '   sz is the minimum final size (with sign). '
    '   frac is the number of fractional places. '
    
    Function makeComp3(inp As Double, sz As Integer, frac As Integer) As String
        Dim inpshifted As Double
        Dim outstr As String
        Dim outbcd As String
        Dim i As Integer
        Dim outval As Integer
        Dim zero As Integer
        zero = Asc("0")
    
        ' Make implied decimal. '
        inpshifted = Abs(inp)
        While frac > 0
            inpshifted = inpshifted * 10
            frac = frac - 1
        Wend
        inpshifted = Int(inpshifted)
    
        ' Get as string and expand to correct size. '
        outstr = CStr(inpshifted)
        While Len(outstr) < sz - 1
            outstr = "0" & outstr
        Wend
        If Len(outstr) Mod 2 = 0 Then
            outstr = "0" & outstr
        End If
    

     

        ' Process each nybble pair bar the last. '
        outbcd = ""
        For i = 1 To Len(outstr) - 2 Step 2
            outval = (Asc(Mid(outstr, i)) - zero) * 16
            outval = outval + Asc(Mid(outstr, i + 1)) - zero
            outbcd = outbcd & Chr(outval)
        Next i
    
        ' Process final nybble including the sign. '    
        outval = (Asc(Right(outstr, 1)) - zero) * 16 + 12
        If inp < 0 Then
            outval = outval + 1
        End If
    
        makeComp3 = outbcd & Chr(outval)
    End Function
    
    Sub Macro1()
        Dim i As Integer
        Dim cobol As String
    
        cobol = makeComp3(3.14159, 6, 2)
        For i = 1 To Len(cobol)
            MsgBox CStr(Asc(Mid(cobol, i)))
        Next i
    End Sub
    
    Grekoz : Hi, I wanted the conversion to be done in VB but don't know how. Anywayz thanks for the reply. Well is there anyway to code the conversion in VB? what the program does is that it reads a string input and then convert the numeric digits to COMP-3 in cobol and store it to a text file.
    paxdiablo : See updated answer.
  • Hi,

    I wanted the conversion to be done in VB but don't know how. Anywayz thanks for the reply. Well is there anyway to code the conversion in VB? what the program does is that it reads a string input and then convert the numeric digits to COMP-3 in cobol and store it to a text file.

    paxdiablo : See updated answer.
  • Hi,

    Thank you so much. I think it worked, but when I try to upload it in the mainframe thru ftp and open it on a copy book the field value is INVALID. I think the problem is during the upload.

    paxdiablo : Grekoz, please put these entries as comments to the answer you're referring to. SO isn't meant to be a conversational board. And, if you're talking mainframe, I'm your man :-). Show me the copybook and the first few lines of the uploaded file captured from ISPF with HEX ON.

0 comments:

Post a Comment