473,407 Members | 2,676 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

How to convert strct to byte array

I have structure as below in C# which is equal to Cobol Copy book structue
Expand|Select|Wrap|Line Numbers
  1. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
  2.     public class InputStruct
  3.     {
  4.         [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 7)]
  5.         public string CustomerNumber;
  6.  
  7.         [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 1)]
  8.         public string CustomerType;
  9.  
  10.         [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 2)]
  11.         public string EndUseCode;
  12.  
  13.         [MarshalAs(UnmanagedType.I4, SizeConst = 5)]
  14.         public string Quantity;
  15.     }
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:

Expand|Select|Wrap|Line Numbers
  1.  public static byte[] StructureToByteArray(object obj)
  2.         {
  3.  
  4.             int Length = Marshal.SizeOf(obj);
  5.             byte[] bytearray = new byte[Length];
  6.             IntPtr ptr = Marshal.AllocHGlobal(Length);            
  7.             Marshal.StructureToPtr(obj, ptr, true);
  8.             Marshal.Copy(ptr, bytearray, 0, Length);
  9.             Marshal.FreeHGlobal(ptr);
  10.             return bytearray;
  11.         }
  12.  
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.
Oct 17 '10 #1
1 2299
Hi

I changed string to char[] and UnmanagedType to UnmanagedType.ByValArray 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
Oct 20 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Tomas Deman | last post by:
Hi, I need a fast method for converting an int array to a byte array. At the moment, I'm using this: public static byte Int2ByteArray(int array) { byte lbytRetval = new byte; int lintIdxHi;...
2
by: Dmitri Shvetsov | last post by:
Hi All, Does somebody know what's the length's ratio in we convert byte to base64 string? Is it a constant ratio or it can be different from case to case? For example if I convert a byte array...
4
by: Prabhu | last post by:
Hi, We are having problem in converting a byte array to string, The byte array has char(174), char(175), char(240), char(242) and char(247) as delimiters for the message. when we use...
1
by: James Dean | last post by:
Could somebody tell me a quick way to write a whole byte array to a 1 bit per pixel bitmap if its possibleinstead of locking the bits and doing it like that..... *** Sent via Developersdex...
1
by: ZorpiedoMan | last post by:
There MUST be an easier way... I have a byte array I want to convert to a string. I do NOT want to have characters above hex7h converted to a question mark (that is what...
2
by: Bryan | last post by:
Apologies if this is a noob question, but I've been struggling with this for quite a while... I'm trying to convert a byte array (encrypted authorization code) into a *screen-printable* string...
4
by: movieknight | last post by:
Hi, I have an application that stores raw .wav files (and also jpgs/bitmaps) within strings, and I need to sometimes convert these strings to byte arrays, and sometimes go from byte arrays back...
1
by: fishspm | last post by:
hello everyone in my source I receive a byte array (for eg: 0xED, 0xFF, 0x01, 0x3D). is there an easy way to convert this byte array to a ushort/uint16 array (0xffed, 0x3d01)? thank you very...
0
by: Jim Rasmussen | last post by:
I need some help in converting a byte array to pdf. Could someone give me an example of how in C#? Here is what I have so far, using System.IO; string sFile = "c:\testpdf.pdf"; FileStream fs =...
3
by: =?Utf-8?B?Vmlua2k=?= | last post by:
Hello Everyone, How can I convert an array of bytes to an integer so for example, I have this a= "00001010" a="11111111" a="01110011" How can I convert this to an integer in .net
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.