如何在C#中的十六進制數和十進制數之間進行轉換?如何轉換C#中十六進制和十進制數字之間的數字?
回答
從小數到十六進制轉換做...
string hexValue = decValue.ToString("X");
從十六進制轉換爲十進制做要麼...
int decValue = int.Parse(hexValue, System.Globalization.NumberStyles.HexNumber);
或
int decValue = Convert.ToInt32(hexValue, 16);
String stringrep = myintvar.ToString("X");
int num = int.Parse("FF", System.Globalization.NumberStyles.HexNumber);
看起來你可以說
Convert.ToInt64(value, 16)
擺脫hexdecimal小數。
其他的方式是:
otherVar.ToString("X");
我得到System.FormatException:指定的格式'x'無效 – 2018-02-12 11:25:41
// Store integer 182
int decValue = 182;
// Convert integer 182 as a hex in a string variable
string hexValue = decValue.ToString("X");
// Convert the hex string back to the number
int decAgain = int.Parse(hexValue, System.Globalization.NumberStyles.HexNumber);
十六進制 - >十進制:
Convert.ToInt64(hexValue, 16);
十進制 - >六角
string.format("{0:x}", decValue);
+1 Convert.ToInt64(hexValue,16)的好處;``如果'0x'前綴是否存在,而其他一些解決方案則不會。 – Craig 2012-01-04 12:53:45
@Craig嗨,所以我需要根據十六進制值大小進行轉換,或者我可以將ToInt64應用於所有十六進制值,會有什麼影響嗎? – user1219310 2014-08-27 14:07:50
static string chex(byte e) // Convert a byte to a string representing that byte in hexadecimal
{
string r = "";
string chars = "ABCDEF";
r += chars[e >> 4];
return r += chars[e &= 0x0F];
} // Easy enough...
static byte CRAZY_BYTE(string t, int i) // Take a byte, if zero return zero, else throw exception (i=0 means false, i>0 means true)
{
if (i == 0) return 0;
throw new Exception(t);
}
static byte hbyte(string e) // Take 2 characters: these are hex chars, convert it to a byte
{ // WARNING: This code will make small children cry. Rated R.
e = e.ToUpper(); //
string msg = "INVALID CHARS"; // The message that will be thrown if the hex str is invalid
byte[] t = new byte[] // Gets the 2 characters and puts them in seperate entries in a byte array.
{ // This will throw an exception if (e.Length != 2).
(byte)e[CRAZY_BYTE("INVALID LENGTH", e.Length^0x02)],
(byte)e[0x01]
};
for (byte i = 0x00; i < 0x02; i++) // Convert those [ascii] characters to [hexadecimal] characters. Error out if either character is invalid.
{
t[i] -= (byte)((t[i] >= 0x30) ? 0x30 : CRAZY_BYTE(msg, 0x01)); // Check for 0-9
t[i] -= (byte)((!(t[i] < 0x0A)) ? (t[i] >= 0x11 ? 0x07 : CRAZY_BYTE(msg, 0x01)) : 0x00); // Check for A-F
}
return t[0x01] |= t[0x00] <<= 0x04; // The moment of truth.
}
這是不是最簡單的方法,但是這個源代碼使你能夠正確的任何類型的八進制數,即23.214, 23和0.512等等。希望這將幫助你..
public string octal_to_decimal(string m_value)
{
double i, j, x = 0;
Int64 main_value;
int k = 0;
bool pw = true, ch;
int position_pt = m_value.IndexOf(".");
if (position_pt == -1)
{
main_value = Convert.ToInt64(m_value);
ch = false;
}
else
{
main_value = Convert.ToInt64(m_value.Remove(position_pt, m_value.Length - position_pt));
ch = true;
}
while (k <= 1)
{
do
{
i = main_value % 10; // Return Remainder
i = i * Convert.ToDouble(Math.Pow(8, x)); // calculate power
if (pw)
x++;
else
x--;
o_to_d = o_to_d + i; // Saving Required calculated value in main variable
main_value = main_value/10; // Dividing the main value
}
while (main_value >= 1);
if (ch)
{
k++;
main_value = Convert.ToInt64(Reversestring(m_value.Remove(0, position_pt + 1)));
}
else
k = 2;
pw = false;
x = -1;
}
return (Convert.ToString(o_to_d));
}
如果這是一個非常大的十六進制字符串超出了正常的整數的能力:
對於.NET 3.5中,我們可以使用BouncyCastle的的的BigInteger類:
String hex = "68c7b05d0000000002f8";
// results in "494809724602834812404472"
String decimal = new Org.BouncyCastle.Math.BigInteger(hex, 16).ToString();
.NET 4.0有BigInteger類。
將字節數組轉換爲十六進制表示的擴展方法。這填充每個字節與前導零。
/// <summary>
/// Turns the byte array into its Hex representation.
/// </summary>
public static string ToHex(this byte[] y)
{
StringBuilder sb = new StringBuilder();
foreach (byte b in y)
{
sb.Append(b.ToString("X").PadLeft(2, "0"[0]));
}
return sb.ToString();
}
如果你想要最大的性能從十六進制轉換做時,十進制數,你可以用十六進制到十進制值的預填充表的方法。
下面是說明這個想法的代碼。我performance tests表明,它可以比Convert.ToInt32快-40%20%(......):
class TableConvert
{
static sbyte[] unhex_table =
{ -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,-1,-1,-1,-1,-1,-1
,-1,10,11,12,13,14,15,-1,-1,-1,-1,-1,-1,-1,-1,-1
,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
,-1,10,11,12,13,14,15,-1,-1,-1,-1,-1,-1,-1,-1,-1
,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
};
public static int Convert(string hexNumber)
{
int decValue = unhex_table[(byte)hexNumber[0]];
for (int i = 1; i < hexNumber.Length; i++)
{
decValue *= 16;
decValue += unhex_table[(byte)hexNumber[i]];
}
return decValue;
}
}
這裏是我的功能:
using System;
using System.Collections.Generic;
class HexadecimalToDecimal
{
static Dictionary<char, int> hexdecval = new Dictionary<char, int>{
{'0', 0},
{'1', 1},
{'2', 2},
{'3', 3},
{'4', 4},
{'5', 5},
{'6', 6},
{'7', 7},
{'8', 8},
{'9', 9},
{'a', 10},
{'b', 11},
{'c', 12},
{'d', 13},
{'e', 14},
{'f', 15},
};
static decimal HexToDec(string hex)
{
decimal result = 0;
hex = hex.ToLower();
for (int i = 0; i < hex.Length; i++)
{
char valAt = hex[hex.Length - 1 - i];
result += hexdecval[valAt] * (int)Math.Pow(16, i);
}
return result;
}
static void Main()
{
Console.WriteLine("Enter Hexadecimal value");
string hex = Console.ReadLine().Trim();
//string hex = "29A";
Console.WriteLine("Hex {0} is dec {1}", hex, HexToDec(hex));
Console.ReadKey();
}
}
我的版本是我想多一點因爲我的C#知識不是很高。 我使用這個算法:http://easyguyevo.hubpages.com/hub/Convert-Hex-to-Decimal(實施例2)
using System;
using System.Collections.Generic;
static class Tool
{
public static string DecToHex(int x)
{
string result = "";
while (x != 0)
{
if ((x % 16) < 10)
result = x % 16 + result;
else
{
string temp = "";
switch (x % 16)
{
case 10: temp = "A"; break;
case 11: temp = "B"; break;
case 12: temp = "C"; break;
case 13: temp = "D"; break;
case 14: temp = "E"; break;
case 15: temp = "F"; break;
}
result = temp + result;
}
x /= 16;
}
return result;
}
public static int HexToDec(string x)
{
int result = 0;
int count = x.Length - 1;
for (int i = 0; i < x.Length; i++)
{
int temp = 0;
switch (x[i])
{
case 'A': temp = 10; break;
case 'B': temp = 11; break;
case 'C': temp = 12; break;
case 'D': temp = 13; break;
case 'E': temp = 14; break;
case 'F': temp = 15; break;
default: temp = -48 + (int)x[i]; break; // -48 because of ASCII
}
result += temp * (int)(Math.Pow(16, count));
count--;
}
return result;
}
}
class Program
{
static void Main(string[] args)
{
Console.Write("Enter Decimal value: ");
int decNum = int.Parse(Console.ReadLine());
Console.WriteLine("Dec {0} is hex {1}", decNum, Tool.DecToHex(decNum));
Console.Write("\nEnter Hexadecimal value: ");
string hexNum = Console.ReadLine().ToUpper();
Console.WriteLine("Hex {0} is dec {1}", hexNum, Tool.HexToDec(hexNum));
Console.ReadKey();
}
}
二進制轉換爲十六進制
Convert.ToString(Convert.ToUInt32(binary1, 2), 16).ToUpper()
十六進制到十進制的轉換
Convert.ToInt32(number, 16);
十進制到十六進制轉換
int.Parse(number, System.Globalization.NumberStyles.HexNumber)
- 1. 如何轉換OCaml中十六進制,十進制,十進制和二進制數之間的數字?
- 2. Java - ByteBuffer和十進制和十六進制之間的轉換
- 3. 將十進制轉換爲十六進制和十六進制
- 4. C++將十六進制字符串轉換爲十六進制char *十六進制數字
- 5. 十六進制和十進制轉換
- 6. 如何在R中的十進制和十六進制之間進行轉換?
- 7. 將八進制數轉換爲十進制和十六進制
- 8. 將十六進制轉換爲十進制數字
- 9. 將字符串十進制轉換爲十六進制數
- 10. 十六進制,字符,數字之間的Java轉換
- 11. C#System.OverflowException二進制到十進制和十六進制轉換
- 12. 如何將十六進制字符串轉換爲十六進制數
- 13. 如何轉換十六進制字符串爲十六進制數
- 14. 如何將十進制整數轉換爲單個十六進制字符? (C)
- 15. 如何將十六進制字符串轉換爲十進制?
- 16. 將數字轉換爲十六進制
- 17. 轉換十六進制字符串十六進制值
- 18. 將字符串轉換爲十六進制到十六進制
- 19. 十進制到十六進制之間的轉換
- 20. 如何比較十六進制數字與shell中的十六進制數字?
- 21. 從十六進制字符串轉換爲十六進制字符數組
- 22. C:轉換十六進制值相同的十進制數
- 23. 將十六進制字符串轉換爲c中的十進制數
- 24. 轉換十六進制到十進制
- 25. 將十六進制字符串數組轉換爲十六進制數組
- 26. C#字符串爲十六進制,十六進制到字節轉換
- 27. 如何將十六進制字符串轉換爲十六進制字節?
- 28. 十六進制和十六進制十進制之間的區別
- 29. 如何將十六進制NSData轉換爲十進制數組?
- 30. 如何在Perl中將十進制數轉換爲十六進制字符串?
我想了解這條線decValue.ToString(「X」)如何將它轉換爲十六進制。 – gizgok 2010-12-07 08:45:45
變量decValue的類型是Int32。 Int32有一個ToString()重載,它可以接受大量格式字符串中的一個,這些格式字符串指示值將如何表示爲字符串。 「X」格式字符串表示十六進制,因此255.ToString(「X」)將返回十六進制字符串「FF」。欲瞭解更多信息,請參閱http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx – 2010-12-07 09:07:47
Convert.ToInt32比int.Parse(...) – 2012-02-03 09:58:16