title: Const Char to Byte Array in C++
body: <p>I am converting a string to a constant char in preparation for sending it over a socket connection as so:</p>
  
  <pre><code>std::ostringstream oss1;
  std::ostringstream oss2;
  
  int code = 52;
  
  oss1 &lt;&lt; "4" &lt;&lt; "1" &lt;&lt; "0" &lt;&lt; "0" &lt;&lt; "0" &lt;&lt; "0" &lt;&lt; 224 + code / 16 &lt;&lt; code % 16;
  
  std::string var1 = oss1.str();
  
  cout &lt;&lt; var1.c_str() &lt;&lt; '\n';
  
  int msg_len3 = strlen(var1.c_str());
  
  comm_send1 = send(sock, var1.c_str(), msg_len3, 0);
  </code></pre>
  
  <p>However, I need the output to be in bytes as so:</p>
  
  <p>Convert constant char from:</p>
  
  <pre><code>4100002274
  </code></pre>
  
  <p>To this:</p>
  
  <pre><code>b'\x04\x01\x00\x00\x00\x00\xe3\x01'
  </code></pre>
  
  <p>In Python 3, which I am translating from I would simply use:</p>
  
  <pre><code>bytevar = bytes(stringvar)
  </code></pre>
  
  <p>...however I am struggling to find an equivalent in C++ which I am new to. Can anyone assist?</p>
  
  <p>Thanks</p>