title: Alternate way to get the Byte Length of a Hex string
body: <p>I have created a function to count the byte length of an incoming hex string, then convert that length into hexidecimal. It first assigns the Byte Length of the incoming string to an int, then I convert the int to a string. After assigning the byte length of my incoming string to an int, I check to see if it is more than 255, if it is, I insert a zero so that I have 2 bytes returned, instead of 3-bits. </p>
  
  <p>I do the follwing:</p>
  
  <p>1) <strong>Takes in the Hex string and divides the number by 2.</strong> </p>
  
  <pre><code>static int ByteLen(std::string sHexStr)
  {
      return (sHexStr.length() / 2);
  }
  </code></pre>
  
  <p>2) <strong>Takes in Hex string, then converts to a Hex format string with itoa()</strong></p>
  
  <pre><code>static std::string ByteLenStr(std::string sHexStr)
  {
      //Assign the length to an int 
      int iLen = ByteLen(sHexStr);
      std::string sTemp = "";
      std::string sZero = "0";
      std::string sLen = "";
      char buffer [1000];
  
       if (iLen &gt; 255)
       {
          //returns the number passed converted to hex base-16 
          //if it is over 255 then it will insert a 0 infront 
                  //so to have 2 bytes instead of 3-bits
          sTemp = itoa (iLen,buffer,16);
          sLen = sTemp.insert(0,sZero);               
                  return sLen;
       }
  
       else{
                  return itoa (iLen,buffer,16);
       }
  }
  </code></pre>
  
  <p>I convert the length to hexidecimal. This seems to work fine, however I am looking for maybe a more simpler way to format the text like I would in C# with the ToString("X2") method. Is this it for C++ or does my method work well enough? </p>
  
  <p><strong>Here is how I would do it in C#:</strong></p>
  
  <pre><code>public static int ByteLen(string sHexStr)
          {
              return (sHexStr.Length / 2);
          }
  
          public static string ByteLenStr(string sHexStr)
          {
              int iLen = ByteLen(sHexStr);
  
              if (iLen &gt; 255)
                  return iLen.ToString("X4");
              else
                  return iLen.ToString("X2");
          }
  </code></pre>
  
  <p>My logic may be off a bit in C++, but the C# method is good enough for me in what I want to do. </p>
  
  <p>Thank you for your time.</p>