Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
how I can convert a structure to a byte array with c # and how I can make the function reciproce another project in c + + but without losing data
Posted 12-Feb-13 5:32am
Edited 12-Feb-13 5:51am
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

There is no standard method of doing this, as it depends on what types you have in your struct, and what restrictions you have.
 
For instance, if you're not concerned with optimal speed and space, you could stream the struct to/from XML (this is relatively trivial in C#, but more complicated in C++) and send the XML string as a byte array.
 
Keep in mind that even simple types are not necessarily the same size in C# and C++, and floating point numbers in particular will have different memory layout. That's why they are often transferred as text, or broken up into exponent and mantissa.
  Permalink  
Comments
AbassiOmar at 12-Feb-13 11:17am
   
Here is my structure
struct A
{
int a;
int b;
byte [] T;
}
I want this vonvertir structure byte [] and I need to recover the other party in c + + and I have to convert this byte [] in this struct
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

>> how I can convert a structure to a byte tbleau array
struct A
{ int i; };
 

A a;
 
char* ptr_to_array = (char*)&a;
 

You don't need to convert the structure, it is after all just an array of bytes already ...
 
As for roundtripping through .Net (c#) - you should perhaps use System.IntPtr[^], or perhaps something else - as it's not entirely clear what you want.
 
Best regards
Espen Harlinn
  Permalink  
Comments
AbassiOmar at 12-Feb-13 10:56am
   
no I want to convert struct to byte [] in C # and convert byte [] to struct in c + +
it is the same data sent from c # to c + +
Espen Harlinn at 12-Feb-13 11:04am
   
Here goes: http://msdn.microsoft.com/en-us/library/bd99e6zt.aspx
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

In C#, int is a signed 32-bit value, which gives a 4-byte array, so you could just write those bytes to the array like this:
 
        void IntToArray(int val, byte[] array, int index)
        {
            // Convert int to uint to make bit handling tidier
            uint temp = unchecked((uint)val);
            for (int i = 0; i < 4; ++i)
            {
                array[index + i] = (byte)((temp >> i*8) & 0xff);
            }
        }
 
        byte[] StructAToArray(A a)
        {
            byte[] array = new byte[8 + a.T.Length];
            // First int
            IntToArray(a.a, array, 0);
            // Second int, 4 bytes later
            IntToArray(a.b, array, 4);
            // Array, 4 bytes later again
            Array.Copy(a.T, 0, array, 8, a.T.Length);
            // Done
            return array;
        }
Then you just have to do the corresponding conversion on the C++ side. Smile | :)
  Permalink  
Comments
AbassiOmar at 12-Feb-13 12:11pm
   
thanks what i need

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 330
1 Sergey Alexandrovich Kryukov 279
2 BillWoodruff 245
3 Afzaal Ahmad Zeeshan 215
4 CPallini 205
0 OriginalGriff 5,635
1 DamithSL 4,496
2 Maciej Los 3,942
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,180


Advertise | Privacy | Mobile
Web03 | 2.8.141216.1 | Last Updated 12 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100