C++ number conversion to hexadecimal string method

  • 2020-04-02 03:06:23
  • OfStack

This article illustrates the method of converting Numbers into hexadecimal strings in C++. Share with you for your reference. The details are as follows:


function toHex(number, min)
{
  var hexes = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'];
  var hex = '';
  var min = min || 2;
  var nibble;
  do
  {
    nibble = number & 0x0f;
    number = number >> 4;
    hex = hexes[nibble] + hex;
  }
  while( number );
  if( hex.length < min )
  {
    hex = new Array(min-hex.length+1).join('0') + hex;
  }
  return '0x'+hex;
}

Hope that the article described in the C++ programming to help you.


Related articles: