Convert to COMP-3 using VB


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 11 of 11

Thread: Convert to COMP-3 using VB

  1. #1
    Join Date
    Jan 2006
    Posts
    4

    Question Convert to COMP-3 using VB

    Hi,

    I am trying to Convert a "HH:MM:SS" value to PIC S9(07) COMP-3
    what steps i need to follow.

    as a step-1 , i could able to convert "12:04:03" to 0120403C
    what should be the next step as finally that data in VSAM looks like :- 

    so my question is
    is this a ASCII char? and how do i convert 0120403C to 

    Any help would be much appreciated.

    Thanks

  2. #2
    Join Date
    Dec 2004
    Location
    Atlanta, Ga.
    Posts
    25
    Step 1: Convert the time value into a double
    Step 2: Convert the Double into a Packed Decimal String (COMP-3)
    Step 3: Convert the string into a byte array.

    For Example:
    '-------------------------------------
    Dim tDouble As Double
    Dim tStr As String
    Dim tByteArray() as Byte
    Dim tTimeStr As String
    Dim i As Long

    tTimeStr = "12:04:03"
    tDouble = 120403
    tStr = DoubleToPackedString(tDouble, 7, 0) 'function defined below...

    'allocate the bytes needed for the resulting string
    ReDim tByteArray(0 To Len(tStr) - 1)

    For i = 0 To Len(tStr) - 1
    tByteArray(i) = Asc(Mid(tStr, i + 1, 1))
    Next i
    '-------------------------------------

    Public Function DoubleToPackedString(pDouble As Double, pLength As Long, pFraction As Long) As String
    Dim tDouble As Double
    Dim tDecimalStr As String

    Dim tLen As Long
    Dim tChar As String
    Dim tHiBits As Byte
    Dim tLoBits As Byte
    Dim tPChar As String
    Dim i As Long
    Dim tPackStr As String
    Dim tSignChar As Byte

    Dim tFormatStr As String

    tPackStr = ""

    tFormatStr = String(pLength - pFraction, "0") & "." & String(pFraction, "0")

    'format it to the size desired
    tDecimalStr = Format(pDouble, tFormatStr)

    'if negative remove the leading sign
    If pDouble < 0 Then
    tDecimalStr = Mid(tDecimalStr, 2)
    End If

    'remove the decimal place
    tDecimalStr = Left(tDecimalStr, InStr(tDecimalStr, ".") - 1) & Mid(tDecimalStr, InStr(tDecimalStr, ".") + 1)

    'make sure we only convert the correct overall length expected
    'example problem: pDouble=1.0, pLength=7, pFraction=7 (i.e. PIC V9(7) ) resulting formated string = 1.0000000
    tDecimalStr = Right(tDecimalStr, pLength)

    'if its an even length, we need to add a leading zero to even out
    'the result when adding the sign character
    ' If (pLength - pFraction) Mod 2 = 0 Then
    If Len(tDecimalStr) Mod 2 = 0 Then
    'add a leading 0
    tDecimalStr = "0" & tDecimalStr
    End If

    tLen = Len(tDecimalStr)

    For i = 1 To tLen - 1
    tChar = Mid(tDecimalStr, i, 1)
    'even number are the "LoBits", odd are the "HiBits"
    If i Mod 2 <> 0 Then
    'get the value and shift it 4 bits
    tHiBits = Val(tChar) * 16
    Else
    'get the value
    tLoBits = Val(tChar)

    'add them together and get the resulting character
    tPChar = Chr(tHiBits + tLoBits)
    tPackStr = tPackStr & tPChar
    tHiBits = 0
    tLoBits = 0
    End If
    Next i

    'add the sign character
    tChar = Mid(tDecimalStr, i, 1)
    'get the value of the last character and shift it 4 bits
    tHiBits = Val(tChar) * 16

    'add the sign character
    If pDouble >= 0 Then
    tLoBits = 12
    Else
    tLoBits = 13
    End If

    'add them together and get the resulting character
    tPChar = Chr(tHiBits + tLoBits)
    tPackStr = tPackStr & tPChar

    DoubleToPackedString = tPackStr

    End Function

  3. #3
    Join Date
    Apr 2007
    Posts
    6
    How did you determine what to input as length to the function? I need to write something to convert a file to ftp up to the mainframe and it contains comp-3 fields so I would like to understand exactly how this would work.

    Thanks!!!

  4. #4
    Join Date
    Dec 2004
    Location
    Atlanta, Ga.
    Posts
    25
    Quote Originally Posted by dgears
    How did you determine what to input as length to the function? I need to write something to convert a file to ftp up to the mainframe and it contains comp-3 fields so I would like to understand exactly how this would work.

    Thanks!!!
    Do you have the copy book that defines the file layout ? The comp-3 definitions define the length, for example,

    05 MyCompField Pic S9(10)V9(2) Comp-3.

    Where:

    V = decimal place
    9 = a numeric digit
    10 = Length (number digits to the left of the decimal place)
    2 = Fraction (number of digits to the right of the decimal place)

    So each comp-3 field will be a different length depending on its definition.

  5. #5
    Join Date
    Apr 2007
    Posts
    6
    so given your example, would the length be 12? I do have to layout that I need for the file and it contains all the cobol definitions for the fields. Do implied decimals count for the length parameter or do I just sum the digits to the left and right of the decimal?

  6. #6
    Join Date
    Dec 2004
    Location
    Atlanta, Ga.
    Posts
    25
    Quote Originally Posted by dgears
    so given your example, would the length be 12? I do have to layout that I need for the file and it contains all the cobol definitions for the fields. Do implied decimals count for the length parameter or do I just sum the digits to the left and right of the decimal?
    The function calculates the length of the field as follows:

    FieldLength = (Length + Fraction)

    If FieldLength Mod 2 = 0 Then
    'add 1 to the field length
    FieldLength = FieldLength + 1
    End If

    So that in the given the example above, the length of the data is:

    If (10 + 2) Mod 2 = 0 Then
    FieldLength = 10 + 2 + 1
    Else
    FieldLength = 10 + 2
    End If

    Do you know how packed decimal (i.e. comp-3) fields are stored internally ?

  7. #7
    Join Date
    Apr 2007
    Posts
    6
    Well, I have an explanation that I printed off the web...I would not say I fully understand it though...

  8. #8
    Join Date
    Apr 2007
    Posts
    6
    I am taking a text file and converting it to EBCDIC to use on the mainframe. Many of the fields that I need to populate contain leading and trailing 0's which seem to be truncated during the function. Any suggestions on how to modify so it will not lose the zeros. I am feeding a COBOL program so I need to have them for placeholders.
    thanks,

  9. #9
    Join Date
    Dec 2004
    Location
    Atlanta, Ga.
    Posts
    25
    Quote Originally Posted by dgears
    I am taking a text file and converting it to EBCDIC to use on the mainframe. Many of the fields that I need to populate contain leading and trailing 0's which seem to be truncated during the function. Any suggestions on how to modify so it will not lose the zeros. I am feeding a COBOL program so I need to have them for placeholders.
    thanks,
    Sounds like you're using a string conversion methoid and the underlying funciton is intepreting the 0's as control charactes (line feeds for example) and ignoring them and the resulting string is missing those characters.

    The best way is to read the data into a byte array, a line at a time if it's to large, and convert the individual bytes using a ASCII to EBCDIC mapping.

    Attached is a class that handles the conversion (extension is renamed to .txt, just change it to .cls) ... hope it helps..
    Attached Files Attached Files

  10. #10
    Join Date
    Apr 2007
    Posts
    6
    Thanks I will give this a try. I have a combination of fields to read that are converted to comp-3 or just picx and I write out three different layouts for each line that read in.

  11. #11
    Join Date
    Apr 2007
    Posts
    6
    I have a feed of data that I am reading in and I am only using about 5 fields in that. I use that data for each of the 3 different layouts that I write for each record, but in each layout is a combination of just picX fields and comp-3 fields. I am confused as how to use the class you provided. The translate function calls in a string so am I to do that first and then add it into the byte array...sorry if I am being dense about this.

Similar Threads

  1. VB as a career choice?
    By steve in forum Careers
    Replies: 8
    Last Post: 08-08-2008, 10:46 AM
  2. Replies: 1
    Last Post: 09-17-2002, 11:57 PM
  3. .NET vs. Enterprise Java: Who's Got Better Security?
    By Glen Kunene in forum Talk to the Editors
    Replies: 17
    Last Post: 03-23-2002, 01:43 AM
  4. How to convert a C file into VB DLL
    By Ravi Sankar in forum VB Classic
    Replies: 0
    Last Post: 02-18-2002, 01:43 AM
  5. VB?? Visually BAD??
    By pnichols in forum Java
    Replies: 5
    Last Post: 12-27-2000, 01:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center