title: c++ Convert hexa to ASCII before send with the serial port
body: <p>I want to communicate with an electronic Load in C++. I use win32.h. To put the electronic load in remote control, I need to send : </p>
  
  <p>"AA 00 20 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 CB" </p>
  
  <p>But before to send, I need to convert it in ASCII code.</p>
  
  <p>My code is :</p>
  
  <pre><code> HANDLE hCom;
   DWORD dwError;
   BOOL fSuccess;
   DWORD dwEvtMask;
   int i;
   int NbOctet;
   char *Message;
   unsigned long nBytesWrite;
  LPCWSTR Port = L"COM14";
   Message = new char[200];
  std::string Test;
  /*-----------------------------------------------*/
  /* Ouverture du port de communiucation           */
  /*-----------------------------------------------*/
  
  hCom = CreateFile(Port,
     GENERIC_READ | GENERIC_WRITE,
     0,
     NULL,
     OPEN_EXISTING,
     0,
     NULL
     );
  
  Message = "AA 00 20 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 CB";
  
  NbOctet = strlen(Message);
  
  WriteFile(hCom,Message,NbOctet,&amp;nBytesWrite,NULL);
  
  
  CloseHandle(hCom);
  
  
  delete[] Message;
  </code></pre>
  
  <p>My question is : How I can convert Message in ASCII caracter before to send it?</p>
  
  <p>I have an example in python of what i want :</p>
  
  <pre><code># Construct a set to remote command
  cmd = chr(0xaa) + chr(0x00) + chr(0x20) # First three bytes
  cmd += chr(0x01) + chr(0x00)*(length_packet - 1 - 4)
  cmd += chr(CalculateChecksum(cmd))
  
  sp.write(cmd)
  </code></pre>
  
  <hr>
  
  <p>My new code is :</p>
  
  <pre><code>void main(int argc, TCHAR *argv[])
  {
   HANDLE hCom;
   DWORD dwError;
   BOOL fSuccess;
   DWORD dwEvtMask;
   int i;
   int NbOctet;
   unsigned long nBytesWrite;
   LPCWSTR Port = L"\\\\.\\COM14";
  
   /*-----------------------------------------------*/
   /* Ouverture du port de communiucation           */
   /*-----------------------------------------------*/
  
   hCom = CreateFile(Port,
     GENERIC_READ | GENERIC_WRITE,
     0,
     NULL,
     OPEN_EXISTING,
     0,
     NULL
     );
  
  char Message[] = {0xAA,0x00,0x20,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xCB};
  
  NbOctet = strlen(Message);
  qDebug() &lt;&lt; Message;
  WriteFile(hCom,Message,NbOctet,&amp;nBytesWrite,NULL);
  
  
  CloseHandle(hCom);
  
  }
  </code></pre>
  
  <p>But it doesn't work</p>