How to convert strct to byte array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Anitha Naidu

    How to convert strct to byte array

    I have structure as below in C# which is equal to Cobol Copy book structue
    Code:
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
        public class InputStruct
        {
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 7)]
            public string CustomerNumber;
    
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 1)]
            public string CustomerType;
    
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 2)]
            public string EndUseCode;
    
            [MarshalAs(UnmanagedType.I4, SizeConst = 5)]
            public string Quantity;
        }
    Cobol copy book structure:
    01 SOS-INPUT-PC50181.
    05 CUST-NO-PC50181 PIC X(07).
    05 CUST-TYPE-PC50181 PIC X(01).
    88 CASH-CUST-PC50181 VALUE "C".
    05 END-USE-CODE-PC50181 PIC X(02).
    05 EQUIP-MFR-MODEL-PC50181 PIC X(10).
    05 ORD-QTY-PC50181 PIC S9(05).

    i am converting Csharp struct above byte of arry to pass it to Cobol program

    using below code:

    Code:
     public static byte[] StructureToByteArray(object obj)
            {
                
                int Length = Marshal.SizeOf(obj);
                byte[] bytearray = new byte[Length];
                IntPtr ptr = Marshal.AllocHGlobal(Length);            
                Marshal.StructureToPtr(obj, ptr, true);
                Marshal.Copy(ptr, bytearray, 0, Length);
                Marshal.FreeHGlobal(ptr);
                return bytearray;
            }
    after converting above struct to byte array i passing it to cobol program, in cobol program input values are not placeing for each feild ie. customer number has only 6 chars and other values are missing and quantity field is displaying as "illegal value in field".

    Please help to map the values to cobol understanding values.
    Last edited by MMcCarthy; Oct 17 '10, 09:09 PM. Reason: added code tags
  • Anitha naidu

    #2
    Hi

    I changed string to char[] and UnmanagedType to UnmanagedType.B yValArray for string values its working fine now but still has problem in Integer values i am passing 5 digit numric values to ORD-QTY of PIC S9(5) but it says <illegal value in numeric field>.

    Please help me if you have solutions.

    THanks
    Akv

    Comment

    Working...