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. How to convert Struct to array

How to convert Struct to array

Scheduled Pinned Locked Moved C#
data-structurestutorialquestion
5 Posts 5 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.
  • K Offline
    K Offline
    kobi10i10
    wrote on last edited by
    #1

    Hi , How can I convert struct like this to array byte public struct Message { public byte id; public int value1; public int value2; public byte chkSum; } Not in this way : public void converter() { Message message; message.id = 1; message.value1 = int.MaxValue ; message.value2 = int.MaxValue ; message.chkSum = (byte)(message.id + message.value1 + message.value2) ; byte [] dataToSend = new byte [10]; dataToSend[0] = message.id ; byte [] dataToInsert = BitConverter.GetBytes (message.value1 ); Array.Copy(dataToInsert, 0, dataToSend, 1, dataToInsert.Length); dataToInsert = BitConverter.GetBytes(message.value1); Array.Copy(dataToInsert, 0, dataToSend, 5, dataToInsert.Length); dataToSend[9] = message.chkSum ; } thank you

    C G L C 4 Replies Last reply
    0
    • K kobi10i10

      Hi , How can I convert struct like this to array byte public struct Message { public byte id; public int value1; public int value2; public byte chkSum; } Not in this way : public void converter() { Message message; message.id = 1; message.value1 = int.MaxValue ; message.value2 = int.MaxValue ; message.chkSum = (byte)(message.id + message.value1 + message.value2) ; byte [] dataToSend = new byte [10]; dataToSend[0] = message.id ; byte [] dataToInsert = BitConverter.GetBytes (message.value1 ); Array.Copy(dataToInsert, 0, dataToSend, 1, dataToInsert.Length); dataToInsert = BitConverter.GetBytes(message.value1); Array.Copy(dataToInsert, 0, dataToSend, 5, dataToInsert.Length); dataToSend[9] = message.chkSum ; } thank you

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      Perhaps one way is to use a pointer ? You can set up a struct so that you specify how the bytes are laid out.

      Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

      1 Reply Last reply
      0
      • K kobi10i10

        Hi , How can I convert struct like this to array byte public struct Message { public byte id; public int value1; public int value2; public byte chkSum; } Not in this way : public void converter() { Message message; message.id = 1; message.value1 = int.MaxValue ; message.value2 = int.MaxValue ; message.chkSum = (byte)(message.id + message.value1 + message.value2) ; byte [] dataToSend = new byte [10]; dataToSend[0] = message.id ; byte [] dataToInsert = BitConverter.GetBytes (message.value1 ); Array.Copy(dataToInsert, 0, dataToSend, 1, dataToInsert.Length); dataToInsert = BitConverter.GetBytes(message.value1); Array.Copy(dataToInsert, 0, dataToSend, 5, dataToInsert.Length); dataToSend[9] = message.chkSum ; } thank you

        G Offline
        G Offline
        Guffa
        wrote on last edited by
        #3

        If you don't want that, what do you want then? What should the result be?

        --- single minded; short sighted; long gone;

        1 Reply Last reply
        0
        • K kobi10i10

          Hi , How can I convert struct like this to array byte public struct Message { public byte id; public int value1; public int value2; public byte chkSum; } Not in this way : public void converter() { Message message; message.id = 1; message.value1 = int.MaxValue ; message.value2 = int.MaxValue ; message.chkSum = (byte)(message.id + message.value1 + message.value2) ; byte [] dataToSend = new byte [10]; dataToSend[0] = message.id ; byte [] dataToInsert = BitConverter.GetBytes (message.value1 ); Array.Copy(dataToInsert, 0, dataToSend, 1, dataToInsert.Length); dataToInsert = BitConverter.GetBytes(message.value1); Array.Copy(dataToInsert, 0, dataToSend, 5, dataToInsert.Length); dataToSend[9] = message.chkSum ; } thank you

          L Offline
          L Offline
          leppie
          wrote on last edited by
          #4

          using System;
          using System.Runtime.InteropServices;

          namespace ConsoleApplication9
          {
          class Program
          {
          // make pack size of smallest element
          [StructLayout(LayoutKind.Sequential, Pack=1)]
          public struct Message
          {
          public byte id;
          public int value1;
          public int value2;
          public byte chkSum;
          }

          static unsafe void Main(string\[\] args)
          {
            Message m \= new Message();
          
            int size \= sizeof(Message);
          
            byte\[\] output \= new byte\[size\];
          
            byte\* p \=
          
          1 Reply Last reply
          0
          • K kobi10i10

            Hi , How can I convert struct like this to array byte public struct Message { public byte id; public int value1; public int value2; public byte chkSum; } Not in this way : public void converter() { Message message; message.id = 1; message.value1 = int.MaxValue ; message.value2 = int.MaxValue ; message.chkSum = (byte)(message.id + message.value1 + message.value2) ; byte [] dataToSend = new byte [10]; dataToSend[0] = message.id ; byte [] dataToInsert = BitConverter.GetBytes (message.value1 ); Array.Copy(dataToInsert, 0, dataToSend, 1, dataToInsert.Length); dataToInsert = BitConverter.GetBytes(message.value1); Array.Copy(dataToInsert, 0, dataToSend, 5, dataToInsert.Length); dataToSend[9] = message.chkSum ; } thank you

            C Offline
            C Offline
            CiNN
            wrote on last edited by
            #5

            Use Serialization ? If the client is using .net

            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