title: How to efficiently convert baudrate from int to speed_t?
body: <p>The functions <code>cfsetospeed</code> and <code>cfsetispeed</code> <a href="https://linux.die.net/man/3/cfsetospeed" rel="noreferrer">take baud rate as type <code>speed_t</code></a>:</p>
  
  <pre><code>int cfsetispeed(struct termios *termios_p, speed_t speed);
  int cfsetospeed(struct termios *termios_p, speed_t speed);
  </code></pre>
  
  <p>The type <code>speed_t</code> is basically an integer, but casting is not a solution, as can be seen from the baudrate definitions in <a href="http://www.delorie.com/djgpp/doc/incs/termios.h" rel="noreferrer"><code>termios.h</code></a>:</p>
  
  <pre><code>#define B0  0x00000000
  #define B50 0x00000001
  #define B75 0x00000002
  // ...
  #define B9600   0x0000000d
  #define B19200  0x0000000e
  #define B38400  0x0000000f
  </code></pre>
  
  <p>When I process user input (e.g. from file), I convert a string such as "9600" to integer 9600, and then to B9600. I'm basically looking for a generic way to convert the following:</p>
  
  <pre><code>// 0 -&gt; B0
  // 50 -&gt; B50
  // 75 -&gt; B75
  // ...
  // 9600 -&gt; B9600 
  // 19200 -&gt; B19200
  // 38400 -&gt; B38400
  </code></pre>
  
  <p><a href="https://www.teuniz.net/RS-232/" rel="noreferrer">One way</a> to convert from <code>int</code> (such as 9600) to <code>speed_t</code> (such as B9600) is a switch-case structure. Is there a better way to achieve this?</p>