title: Equivalent of C unsigned char array in c# using PInvoke
body: <p>I posted yesterday about how I am converting a C program to C# as my first C# project here: <a href="https://stackoverflow.com/questions/21709528/convert-from-c-to-c-or-make-dll?noredirect=1#comment32834861_21709528">Convert from C to C#, or make DLL?</a>. I have made a lot of progress, but I am stuck on a section. I am not sure how I should go about recreating this unsigned char array</p>
  
  <p>C Code:</p>
  
  <pre><code>unsigned char state_buf[5125];
  
  //--------------------------------------------------------------------------
  // Main of iSerial64
  //
  void main(int argc, char **argv)
  {
     char refresh,buf[200];
     short flag,i,didsetup=0;
     short ROM[9];
     short PortNum,PortType;
     long SHandle;
     char serialtmp[2], serial[10][17];
     int j = -1;
  </code></pre>
  
  <p>....</p>
  
  <p>This is the function in C# that takes state_buf using PInvoke <a href="http://files.maximintegrated.com/sia_bu/licensed/docs/1-wire_sdk_win/TMEX/romm1rxq.html" rel="nofollow noreferrer">http://files.maximintegrated.com/sia_bu/licensed/docs/1-wire_sdk_win/TMEX/romm1rxq.html</a></p>
  
  <p>C# Code that I am using to import the function from the api:   </p>
  
  <pre><code> [DllImport("IBFS64.dll")]
          public static extern short TMRom(
              int session_handle,
              byte state_buf,
              [MarshalAs(UnmanagedType.LPStr)]StringBuilder ROM
          );
  </code></pre>
  
  <p>I saw that byte was the C# equivalent, but I am not fully sure how to use it.</p>
  
  <p>You guys have been so helpful thus far, and I am really pleased with the progress I am making on this.</p>
  
  <p>This is what I have gotten done in C# since yesterday: </p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Runtime.InteropServices;
  
  namespace PInvokeTest {
    class Program {
      static void Main(string[] args)
        {
            int session_handle = 0;
            int result = 0;
            int flag = 0;
            int didsetup = 0;
            int defPort = 0;
            int i = 0, j = -1;
            short type_test = 0;
            byte state_buf = 0;
            short port_num = 0, port_type = 1;
            StringBuilder ID_buf = new StringBuilder();
            StringBuilder serial = new StringBuilder();
            StringBuilder serialtmp = new StringBuilder();
            StringBuilder ROM = new StringBuilder();
  
            //result = TMSearch(session_handle, state_buffer, 1, 1, 0xEC);
  
  
            defPort = TMReadDefaultPort(out port_num,out port_type);
  
  
            if (defPort == 1)
            {
                Console.Write("Default Device: ");
  
                switch (port_type)
                {
                    case 1:
                        Console.Write("DS9097E serial adapter ");
                        break;
  
                    case 2:
                        Console.Write("Parallel adapter ");
                        break;
  
                    case 5:
                        Console.Write("DS90907U serial adapter ");
                        break;
  
                    case 6:
                        Console.Write("DS9490 USB adapter ");
                        break;
                }
                Console.Write("on port # {0}", port_num);
            }
  
            else
                Console.Write("No Device found");
  
            // get the TMEX driver version
            Get_Version(ID_buf);
  
            Console.WriteLine("\nDriver: {0}", ID_buf);
  
            type_test = TMGetTypeVersion(port_type, ID_buf);
  
            if (type_test &lt; 0)
            {
                Console.WriteLine("\nNo Hardware Driver for this type found!\n");
                Console.Write("Press any key to exit");
                Console.ReadKey();
                Environment.Exit(0);
            }
            else
                Console.WriteLine("Driver Found");
  
  
            // check for valid range of PortNum
            if ((port_num &lt; 1) || (port_num &gt; 15))
            {
                Console.WriteLine("ERROR, invalid port requested: {0}\n", port_num);
                Console.Write("Press any key to exit");
                Console.ReadKey();
                Environment.Exit(0);
            }
  
  
            do
            {
                // get a session handle to the requested port
                session_handle = TMExtendedStartSession(port_num, port_type, IntPtr.Zero);
  
                if (session_handle &gt; 0)
                {
                    // check to see if TMSetup has been done once
                    if (didsetup == 0)
                    {
                        flag = TMSetup(session_handle);
                        if (flag == 1 || flag == 2)
                        {
                            Console.Write("TMSetup complete {0}", flag);
                            didsetup = 1;
                        }
                        else
                        {
                            Console.Write("ERROR doing setup {0}", flag);
                            break;
                        }
                    }
  
                    else
                    {
                        //j was added to keep track of the serial number strings
                        j++;
  
                        //memset(serial[j], 0, strlen(serial[j]));
                        flag = TMNext(session_handle, state_buf);
                        if (flag &gt; 0)
                        {
                            //ROM[0] = 0;
                            flag = TMRom(session_handle, state_buf, ROM);
                            for (i = 7; i &gt;= 0; i--)
                            {
                                //This section was changed from the original
                                //copies raw number into string
                                //sprintf(serialtmp, "%02X", ROM[i]);
                                //strcat(serial[j], serialtmp);
  
                            }
                            Console.WriteLine("{0} \n", serial[j]);
  
                        }
                        else
                            Console.WriteLine("end of search\n");
                    }
                    // close the opened session
                    TMEndSession(session_handle);
                }
  
            }
            while (flag &gt; 0);
  
  
  
  
  
  
            Console.ReadKey();
        }
  
     [DllImport("IBFS64.dll")]
      public static extern int TMExtendedStartSession(
          short PortNum, 
          short PortType,
          IntPtr EnhancedOptions 
      );
  
      [DllImport("IBFS64.dll")]
      public static extern short TMSearch(
          int session_handle, 
          byte state_buffer, 
          short p1, 
          short p2, 
          short p3
      );
  
      [DllImport("IBFS64.dll")]
      public static extern short TMReadDefaultPort(
          out short port_num,
          out short port_type
      );
  
      [DllImport("IBFS64.dll")]
      public static extern short Get_Version(
          [MarshalAs(UnmanagedType.LPStr)]StringBuilder ID_buf 
      );
  
      [DllImport("IBFS64.dll")]
      public static extern short TMGetTypeVersion(
          short port_type,
          [MarshalAs(UnmanagedType.LPStr)]StringBuilder ID_buf
      );
  
      [DllImport("IBFS64.dll")]
      public static extern short TMSetup(
          int session_handle
      );
  
      [DllImport("IBFS64.dll")]
      public static extern short TMNext(
          int session_handle,
          byte state_buf
      );
  
      [DllImport("IBFS64.dll")]
      public static extern short TMRom(
          int session_handle,
          byte state_buf,
          [MarshalAs(UnmanagedType.LPStr)]StringBuilder ROM
      );
  
      [DllImport("IBFS64.dll")]
      public static extern short TMEndSession(
          int session_handle
      );
    }
  }
  </code></pre>
  
  <p>Let me know if there is anything I should change. I appreciate the help!</p>