Posted By

aadsm on 11/11/09


Tagged

number hex String integer convert


Versions (?)

Convert a Number to an hexadecimal String


 / Published in: JavaScript
 

  1. function toHex(number, min)
  2. {
  3. var hexes = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'];
  4. var hex = '';
  5. var min = min || 2;
  6. var nibble;
  7.  
  8. do
  9. {
  10. nibble = number & 0x0f;
  11. number = number >> 4;
  12. hex = hexes[nibble] + hex;
  13. }
  14. while( number );
  15.  
  16. if( hex.length < min )
  17. {
  18. hex = new Array(min-hex.length+1).join('0') + hex;
  19. }
  20.  
  21. return '0x'+hex;
  22. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: topinnovation on April 26, 2010

Number system conversion using C#.NET Decimal to Hexadecimal Input: Range Min:0, Max:18446744073709551615 or (2^64 - 1). Datatype UInt64 Processing input.ToString(“X”); Output: Datatype string Range Min:0h, Max:FFFFFFFFFFFFFFFFh.

Hexadecimal to Decimal Input: Range Min:0h, Max:FFFFFFFFFFFFFFFFh. Datatype string Processing UInt64.Parse(input, System.Globalization.NumberStyles.HexNumber).ToString(); Output: Range Min:0, Max:18446744073709551615 or (2^64 - 1). Datatype string

Decimal to Binary Input: Range Min:0, Max:18446744073709551615 or (2^64 - 1). Datatype UInt64 Processing ... string binaryStr = String.Empty; while (input > 0) { binaryStr += (input % 2).ToString(); input /= 2; } // Arrange the humanreadable format MSB on leftmost end and LSB on righmost end. binaryStr = ReverseString(binaryStr); return binaryStr; ... private static string ReverseString(string str) { char[] charArray = str.ToCharArray(); Array.Reverse(charArray); return new string(charArray); } Output: Range 0 – 64 bit Datatype string

Binary to Decimal Input: Range 0 to 64 bit Datatype string Processing Convert.ToUInt64(input, 2).ToString(); Output: Range Min:0, Max:18446744073709551615 or (2^64 - 1). Datatype string

Hexadecimal to Binary Processing 1. Hexadecimal to Decimal 2. Decimal to Binary

Binary to Hexadecimal Processing 1. Binary to Decimal 2. Decimal to Hexadecimal

You need to login to post a comment.