title: c# byte and bit casting - could this be done better?
body: <p>I'm building arrays of bytes to be communicated over Bluetooth. These bytes are partly built from enumerated types, such as the following :</p>
  
  <pre><code>public enum Motor
  {
     A = 0x00,
     B = 0x01,
     C = 0x02,
     AB = 0x03,
     AC = 0x04,
     BC = 0x05,
  }
  </code></pre>
  
  <p>Later in my code I create a variable called MyMotor of type MyMotor.B. I then pass this variable to a method in which I build my byte array.</p>
  
  <p>My issue is that the software I'm communicating with via Bluetooth expects the hex value of the enumerated value as a string, ie MyMotor.B = byte 0x01 = dec 1 = hex 31. However casting MyMotor directly to a char would result in it evaluating to it's enumerated value ie MyMotor = B = hex 42.</p>
  
  <p>For various reasons I can't change my enurated list, so I've settled on what feels like a very hacked together two line section of code :</p>
  
  <pre><code>String motorchar = Convert.ToString(Convert.ToInt32(MyMotor)); // convert to temp var
  command[5] = (byte)(motorchar[0]); // store hex value of var
  </code></pre>
  
  <p>It works as I'd like ie command[5] = hex31</p>
  
  <p>I wonder if there's a better way. All the articles I've found talk about dealing with entire byte arrays rather than individual bytes and chars.</p>