title: linux writing string value as hex to serial
body: <p>here's my problem<br>
  i have a string with hex value such as  </p>
  
  <pre><code>std::string str ="8C000002008E"  
  </code></pre>
  
  <p>and i want to write this as hex to serial out by using the  </p>
  
  <pre><code>write()
  </code></pre>
  
  <p>I have a sony Display which i want to control with.<br>
  Passing a</p>
  
  <pre><code>unsigned char test[6] = {0x8c, 0x00, 0x00, 0x02, 0x00, 08E};
  </code></pre>
  
  <p>to the write method works.
  But i dont know how to convert the string to such a char array especially that the size of the char array has to be calculated on runtime.</p>
  
  <p>Thanks for your help.</p>
  
  <p>Here is my full code</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;iostream&gt;
  #include &lt;cstring&gt;
  #include &lt;fcntl.h&gt;
  #include &lt;termios.h&gt;
  
  #include &lt;sstream&gt;
  
  using namespace std;
  #define TERM_DEVICE "/dev/ttyUSB0"
  #define TERM_SPEED "B9600"
  
  
  int main() {
  
  std::string teststr = "8C000002008E";
  
  int fd, old_flags;
  
  ssize_t length;
  
  char buffer[16];
  
  struct termios term_attr;
  
  fd_set input_fdset;
  
  if ((fd = open(TERM_DEVICE, O_RDWR)) == -1)
     {
      perror("terminal: Can't open device " TERM_DEVICE);
      return(1);
     }
              /* RS232 konfigurieren */
    if (tcgetattr(fd, &amp;term_attr) != 0)
     {
      perror("terminal: tcgetattr() failed");
      return(1);
     }
  
    cfsetispeed(&amp;term_attr, B9600);
    cfsetospeed(&amp;term_attr, B9600);
  
    term_attr.c_cflag &amp;= ~PARENB;
  
    term_attr.c_cflag &amp;= CS8;
    term_attr.c_cflag &amp;= CSIZE;
    term_attr.c_cflag &amp;= CSTOPB;
    term_attr.c_iflag = 0;
    term_attr.c_oflag = OPOST | ONLCR;
    term_attr.c_lflag = 0;
    if (tcsetattr(fd, TCSAFLUSH, &amp;term_attr) != 0)
      perror("terminal: tcsetattr() failed");
  
    if (tcgetattr(STDIN_FILENO, &amp;term_attr) != 0)
     {
      perror("terminal: tcgetattr() failed");
      return(1);
     }
  
    old_flags = term_attr.c_lflag;
    term_attr.c_lflag &amp;= ~(ICANON | ECHO);
    if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &amp;term_attr) != 0)
      perror("terminal: tcsetattr() failed");
  
    while (1)
     {
      FD_ZERO(&amp;input_fdset);
      FD_SET(STDIN_FILENO, &amp;input_fdset);
      FD_SET(fd, &amp;input_fdset);
  
      if (select(fd+1, &amp;input_fdset, NULL, NULL, NULL) == -1)
        perror("terminal: select() failed");
  
  
      unsigned char test[6] = {0x8c, 0x00, 0x00, 0x02, 0x00, 0x8E};
  
      if (FD_ISSET(STDIN_FILENO, &amp;input_fdset)){
            if ((length = read(STDIN_FILENO, buffer, 16)) == -1)
              perror("terminal: read() failed");
            else
              if (buffer[0] == '\33')
                  break;
          else{
                write(fd, test , sizeof(test));
          }
       }
  
  
      if (FD_ISSET(fd, &amp;input_fdset))
       {
        if ((length = read(fd, buffer, 16)) == -1)
          perror("terminal: read() failed");
        else
            cout &lt;&lt; std::hex &lt;&lt; buffer&lt;&lt; endl;
  
       }
     }
  
    term_attr.c_lflag = old_flags;
    if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &amp;term_attr) != 0)
      perror("terminal: tcsetattr() failed");
  
    printf("Aborted.\n");
    close(fd);
  
  return 0;
  </code></pre>
  
  <p>}</p>