C# convert integer to hex and back again
如何转换以下内容?
2934(整数)到B76(十六进制)
让我解释一下我想做什么。我的数据库中有以整数形式存储的用户ID。我不想让用户引用他们的ID,而是让他们使用十六进制值。主要原因是它比较短。
所以我不仅需要从整数到十六进制,还需要从十六进制到整数。
有没有一种简单的方法可以在C中完成这项工作?
1 2 3 4 5 6 | // Store integer 182 int intValue = 182; // Convert integer 182 as a hex in a string variable string hexValue = intValue.ToString("X"); // Convert the hex string back to the number int intAgain = int.Parse(hexValue, System.Globalization.NumberStyles.HexNumber); |
从http:/ / / kb8 www.geekpedia.com _ how-do-i-convert-from-decimal-to-hex-and-hex-to-decimal.html </P >
使用: </P >
1 2 3 | int myInt = 2934; string myHex = myInt.ToString("X"); // Gives you hexadecimal int myNewInt = Convert.ToInt32(myHex, 16); // Back to int again. |
如何对这convert之间的十六进制数字和字符串:要(C编程指南#)方法的更多信息和算例。 </P >
在下面的convert尽到它对十六进制 </P >
1 2 3 | public static string ToHex(this int value) { return String.Format("0x{0:X}", value); } |
和倒了 </P >
1 2 3 4 5 6 7 | public static int FromHex(string value) { // strip the leading 0x if ( value.StartsWith("0x", StringComparison.OrdinalIgnoreCase)) { value = value.Substring(2); } return Int32.Parse(value, NumberStyles.HexNumber); } |
1 2 3 4 5 6 7 8 9 | string HexFromID(int ID) { return ID.ToString("X"); } int IDFromHex(string HexID) { return int.Parse(HexID, System.Globalization.NumberStyles.HexNumber); } |
我真的值这个问题,虽然。你的化妆师,在今天的声明的目标值,它shorter,威尔,但这并不是一个目标的T细胞本身。你真的让它easier均指到今天,记得或easier型。 </P >
如果你记得easier均对,然后你再把backwards一步。我们知道它的仍然是同样的尺寸,不同的编码。但你的用户不会知道那是限制的字母A到F,和SO的ID将在同一概念空间的方法occupy作为,如果他们的字母A—Z是allowed。所以instead有利的一类记忆电话号码,它更像一个GUID的记忆(的当量长度)。 </P >
如果你typing instead均有利,可以对使用的用户现在使用的keypad黑鸭子的Keyboard的主。它的likely被更难对型,因为它不会是一个词,他们的手指recognize。 </P >
一个更好的选择是多到让他们选择一个真正的username争。 </P >
1 2 3 | int valInt = 12; Console.WriteLine(valInt.ToString("X")); // C ~ possibly single-digit output Console.WriteLine(valInt.ToString("X2")); // 0C ~ always double-digit output |
对十六进制: </P >
1 | string hex = intValue.ToString("X"); |
对国际: </P >
1 | int intValue = int.Parse(hex, System.Globalization.NumberStyles.HexNumber) |
我创造了我自己的十六进制字符串转换int到溶液的方法和BP在我发现这个答案。不surprisingly,它的considerably快比在溶液.net因为有overhead少的代码。 </P >
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | /// <summary> /// Convert an integer to a string of hexidecimal numbers. /// </summary> /// <param name="n">The int to convert to Hex representation</param> /// <param name="len">number of digits in the hex string. Pads with leading zeros.</param> /// <returns></returns> private static String IntToHexString(int n, int len) { char[] ch = new char[len--]; for (int i = len; i >= 0; i--) { ch[len - i] = ByteToHexChar((byte)((uint)(n >> 4 * i) & 15)); } return new String(ch); } /// <summary> /// Convert a byte to a hexidecimal char /// </summary> /// <param name="b"></param> /// <returns></returns> private static char ByteToHexChar(byte b) { if (b < 0 || b > 15) throw new Exception("IntToHexChar: input out of range for Hex value"); return b < 10 ? (char)(b + 48) : (char)(b + 55); } /// <summary> /// Convert a hexidecimal string to an base 10 integer /// </summary> /// <param name="str"></param> /// <returns></returns> private static int HexStringToInt(String str) { int value = 0; for (int i = 0; i < str.Length; i++) { value += HexCharToInt(str[i]) << ((str.Length - 1 - i) * 4); } return value; } /// <summary> /// Convert a hex char to it an integer. /// </summary> /// <param name="ch"></param> /// <returns></returns> private static int HexCharToInt(char ch) { if (ch < 48 || (ch > 57 && ch < 65) || ch > 70) throw new Exception("HexCharToInt: input out of range for Hex value"); return (ch < 58) ? ch - 48 : ch - 55; } |
调速代码: </P >
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | static void Main(string[] args) { int num = 3500; long start = System.Diagnostics.Stopwatch.GetTimestamp(); for (int i = 0; i < 2000000; i++) if (num != HexStringToInt(IntToHexString(num, 3))) Console.WriteLine(num +" =" + HexStringToInt(IntToHexString(num, 3))); long end = System.Diagnostics.Stopwatch.GetTimestamp(); Console.WriteLine(((double)end - (double)start)/(double)System.Diagnostics.Stopwatch.Frequency); for (int i = 0; i < 2000000; i++) if (num != Convert.ToInt32(num.ToString("X3"), 16)) Console.WriteLine(i); end = System.Diagnostics.Stopwatch.GetTimestamp(); Console.WriteLine(((double)end - (double)start)/(double)System.Diagnostics.Stopwatch.Frequency); Console.ReadLine(); } |
结果: </P >
1 2 3 4 5 6 | Digits : MyCode : .Net 1 : 0.21 : 0.45 2 : 0.31 : 0.56 4 : 0.51 : 0.78 6 : 0.70 : 1.02 8 : 0.90 : 1.25 |
一fashionably belated响应,但有一些你所
在printf整数的十六进制值与零PADDING ON(如果需要): </P >
1 2 3 | int intValue = 1234; Console.WriteLine("{0,0:D4} {0,0:X3}", intValue); |
docs.microsoft.com https:/ / / / /在美国的标准库类型/机械/如何对垫的一个号码-与- zeros领先 </P >
我不能写评论,但一些人仍会是什么misleadingly错"的
NET框架 </P >
Very well explained and few programming lines
GOOD JOB
1 2 3 4 5 6 | // Store integer 182 int intValue = 182; // Convert integer 182 as a hex in a string variable string hexValue = intValue.ToString("X"); // Convert the hex string back to the number int intAgain = int.Parse(hexValue, System.Globalization.NumberStyles.HexNumber); |
Pascal,C # > > </P >
http://files.hddguru.com/download/Software/Seagate/St_mem.pas
从某个旧的老式的PASCAL转换到C #过程研究 </P >
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | /// <summary> /// Conver number from Decadic to Hexadecimal /// </summary> /// <param name="w"></param> /// <returns></returns> public string MakeHex(int w) { try { char[] b = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}; char[] S = new char[7]; S[0] = b[(w >> 24) & 15]; S[1] = b[(w >> 20) & 15]; S[2] = b[(w >> 16) & 15]; S[3] = b[(w >> 12) & 15]; S[4] = b[(w >> 8) & 15]; S[5] = b[(w >> 4) & 15]; S[6] = b[w & 15]; string _MakeHex = new string(S, 0, S.Count()); return _MakeHex; } catch (Exception ex) { throw; } } |
国际对十六进制: </P >
int a = 72;
Console.WriteLine("{0:X}", a);
十六进制:对国际 </P >
int b = 0xB76;
Console.WriteLine(b);