Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. to byte[] conversion?

to byte[] conversion?

Scheduled Pinned Locked Moved C#
helpcsharptutorialquestionc++
6 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    stonee74
    wrote on last edited by
    #1

    Hello there, I'm getting crazy. Maybe somebody of you guys have some good ideas how to solve this. Imagine, There is an existing API in C++. The main idea of this interface is to exchange byte arrays with a server. In the example below you see the principle. I create e.g. a object myCommand from the 'Command' struct which again consists of enumns and structs. The idea is to to send these binary data of the enumns and structs down to the server. In c++ i create this object and send it like that: Socket.send(&myCommand, sizeof(blabla)); As you see i just need to give a pointer to my data. I decided to translate this interface to C#, since writing a wrapper would be about the same work(?!). Anyway, the biggest problem i have is: How can i create a byte[] from myCommand object, containg all the numerical enum and struct data to be able to send it to the server??? Sockets in C# are only accepting byte[] as parameter, but how to convert 'myCommand Object' to byte? There are dozens of examples how to decode strings to byte arrays but I did not found any material abot converting my stuff! One thing i thought about is to use serializing, but since the future app should run on a PocketPC on the Compact Framwork, this is not possible since the .NET CF is not supporting serializing...... Any help, ideas tips and tricks would be greatly appreciated! regards, stonee //example, principle of API enum DataType { Command = 0, Error = 1, Result = 2, }; enum Command { Exit = 0, GetValueA = 1, GetValueB = 2, GetValueC = 3, GetValueD = 4, }; struct Header { public long lPacketSize; public DataType type; } struct Command { public Header packetHeader; public Command command; } Command myCommand = new Command(); Socket.send(&myCommand, sizeof(blabla));

    E 1 Reply Last reply
    0
    • S stonee74

      Hello there, I'm getting crazy. Maybe somebody of you guys have some good ideas how to solve this. Imagine, There is an existing API in C++. The main idea of this interface is to exchange byte arrays with a server. In the example below you see the principle. I create e.g. a object myCommand from the 'Command' struct which again consists of enumns and structs. The idea is to to send these binary data of the enumns and structs down to the server. In c++ i create this object and send it like that: Socket.send(&myCommand, sizeof(blabla)); As you see i just need to give a pointer to my data. I decided to translate this interface to C#, since writing a wrapper would be about the same work(?!). Anyway, the biggest problem i have is: How can i create a byte[] from myCommand object, containg all the numerical enum and struct data to be able to send it to the server??? Sockets in C# are only accepting byte[] as parameter, but how to convert 'myCommand Object' to byte? There are dozens of examples how to decode strings to byte arrays but I did not found any material abot converting my stuff! One thing i thought about is to use serializing, but since the future app should run on a PocketPC on the Compact Framwork, this is not possible since the .NET CF is not supporting serializing...... Any help, ideas tips and tricks would be greatly appreciated! regards, stonee //example, principle of API enum DataType { Command = 0, Error = 1, Result = 2, }; enum Command { Exit = 0, GetValueA = 1, GetValueB = 2, GetValueC = 3, GetValueD = 4, }; struct Header { public long lPacketSize; public DataType type; } struct Command { public Header packetHeader; public Command command; } Command myCommand = new Command(); Socket.send(&myCommand, sizeof(blabla));

      E Offline
      E Offline
      Eric Gunnerson msft
      wrote on last edited by
      #2

      If you can't use serialization, your best option is to use unsafe. You can do: byte[] buffer = new byte[sizeof(Command)]; fixed (Byte* pBuffer = buffer) { *((Command*) pBuffer) = command; } socket.Send(buffer, buffer.Lenth);

      S 1 Reply Last reply
      0
      • E Eric Gunnerson msft

        If you can't use serialization, your best option is to use unsafe. You can do: byte[] buffer = new byte[sizeof(Command)]; fixed (Byte* pBuffer = buffer) { *((Command*) pBuffer) = command; } socket.Send(buffer, buffer.Lenth);

        S Offline
        S Offline
        stonee74
        wrote on last edited by
        #3

        thanks a lot for this idea, should look like below about? The problem is the compiler states: The type or namespace name 'Command' could not be found (are you missing a using directive or an assembly reference?) Sorry, can you bring me in the right direction please? And what means fixed? thanks again, stonee public unsafe override bool Send(void* Command, long lSize) { System.Console.WriteLine("Send() called!"); try { byte[] buffer = new byte[lSize]; fixed (Byte* pBuffer = buffer) { *((Command*) pBuffer) = Command; } m_socket.Send(buffer, buffer.Length,SocketFlags.None); return true; } catch(SocketException se) { System.Console.WriteLine(se.Message); return false; }

        E 1 Reply Last reply
        0
        • S stonee74

          thanks a lot for this idea, should look like below about? The problem is the compiler states: The type or namespace name 'Command' could not be found (are you missing a using directive or an assembly reference?) Sorry, can you bring me in the right direction please? And what means fixed? thanks again, stonee public unsafe override bool Send(void* Command, long lSize) { System.Console.WriteLine("Send() called!"); try { byte[] buffer = new byte[lSize]; fixed (Byte* pBuffer = buffer) { *((Command*) pBuffer) = Command; } m_socket.Send(buffer, buffer.Length,SocketFlags.None); return true; } catch(SocketException se) { System.Console.WriteLine(se.Message); return false; }

          E Offline
          E Offline
          Eric Gunnerson msft
          wrote on last edited by
          #4

          Two ways to do this: 1) Pass in the real type: Send(MyStruct s) { ... // inside fixed... *((MyStruct) pBufer) = s; 2) pass in a byte* pointer, and pass things across directly. If you do this, you'll need to have the fixed statement in the calling routine, so that you can get the pointer.

          S 1 Reply Last reply
          0
          • E Eric Gunnerson msft

            Two ways to do this: 1) Pass in the real type: Send(MyStruct s) { ... // inside fixed... *((MyStruct) pBufer) = s; 2) pass in a byte* pointer, and pass things across directly. If you do this, you'll need to have the fixed statement in the calling routine, so that you can get the pointer.

            S Offline
            S Offline
            stonee74
            wrote on last edited by
            #5

            Sorry, I guess I act as stupid but I still havent' understood 100% how to go......... 1) Can I pass void* PacketStart, long PacketSize or Do I need to go the managed way? 2) Are those two method declarations equivalent? 3) If i pass void*, what would be the substitute for MyStruct? 4) *((object) pBuffer) = PacketStart; ( results in Cannot convert type 'byte*' to 'object') 5) Could you give an example how to go, to receive the respective byte[] in this specific case? your help is greatly appreciated!! regards, newbie stonee :confused: public unsafe override bool SendPacket(void* PacketStart, long PacketSize) //public unsafe override bool SendPacket(ref object PacketStart, long PacketSize) { System.Console.WriteLine("Virtual SendPacket() called!"); try { byte[] buffer = new byte[PacketSize]; fixed (Byte* pBuffer = buffer) { *((MyStruct) pBuffer) = PacketStart; } m_socket.Send(buffer, buffer.Length,SocketFlags.None); return true; } catch(SocketException se) { System.Console.WriteLine(se.Message); return false; } }

            E 1 Reply Last reply
            0
            • S stonee74

              Sorry, I guess I act as stupid but I still havent' understood 100% how to go......... 1) Can I pass void* PacketStart, long PacketSize or Do I need to go the managed way? 2) Are those two method declarations equivalent? 3) If i pass void*, what would be the substitute for MyStruct? 4) *((object) pBuffer) = PacketStart; ( results in Cannot convert type 'byte*' to 'object') 5) Could you give an example how to go, to receive the respective byte[] in this specific case? your help is greatly appreciated!! regards, newbie stonee :confused: public unsafe override bool SendPacket(void* PacketStart, long PacketSize) //public unsafe override bool SendPacket(ref object PacketStart, long PacketSize) { System.Console.WriteLine("Virtual SendPacket() called!"); try { byte[] buffer = new byte[PacketSize]; fixed (Byte* pBuffer = buffer) { *((MyStruct) pBuffer) = PacketStart; } m_socket.Send(buffer, buffer.Length,SocketFlags.None); return true; } catch(SocketException se) { System.Console.WriteLine(se.Message); return false; } }

              E Offline
              E Offline
              Eric Gunnerson msft
              wrote on last edited by
              #6

              I'll try to be clearer: 1) Can I pass void* PacketStart, long PacketSize or Do I need to go the managed way? You need to get from the the managed world to the pointer world somehow to use the pointer operations. If you pass in a MyStruct (for example), you would do the fixed statement in your routine. If you want to pass in a pointer, you'd have to do the fixed statement in the caller of the routine. While you'll have to write a separate routine for each struct, I think that writing the managed version and putting all the ugliness in your routine is the rigth 2) Are those two method declarations equivalent? No, though they are similar. I'm not sure the object version is feasible, though I've never tried it myself. 3) If i pass void*, what would be the substitute for MyStruct? If you pass a byte* (on reflection, I don't think void* will work), you would need to copy the bytes over one by one to the byte buffer, based on the length you'd pass in. 4) *((object) pBuffer) = PacketStart; ( results in Cannot convert type 'byte*' to 'object') Object won't work here, because the compiler does't know how big the actual object is. 5) Could you give an example how to go, to receive the respective byte[] in this specific case? Here's some code I wrote up. It compiles, but I haven't actually tested it: using System; using System.IO; using System.Net.Sockets; namespace ConsoleApplication7 { struct MyStruct { int i; float j; } /// /// Summary description for Class1. /// class Class1 { /// /// The main entry point for the application. /// [STAThread] static void Main(string[] args) { // // TODO: Add code to start application here // } public unsafe void SendMyStruct(MyStruct myStruct, Socket socket) { byte[] buffer = new byte[sizeof(MyStruct)]; fixed (byte* pBuffer = buffer) { *((MyStruct*)pBuffer) = myStruct; } socket.Send(buffer); } public unsafe MyStruct ReadMyStruct(Socket socket) { byte[] buffer = new byte[sizeof(MyStruct)]; socket.Receive(buffer); MyStruct myStruct; fixed (byte* pBuffer = buffer) { myStruct = *((MyStruct*)pBuffer); } return myStruct; } } }

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups