/ Published in: JavaScript
Expand |
Embed | Plain Text
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; }
Comments
Subscribe to comments
You need to login to post a comment.

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