简体   繁体   English

c#十六进制到位转换

[英]c# hex to bit conversion

I'm trying to convert the hexadecimal representation of a 64-bit number (eg, the string "FFFFFFFFF" ) to binary representation ( "11111..." ). 我正在尝试将64位数字的十六进制表示(例如,字符串"FFFFFFFFF" )转换为二进制表示( "11111..." )。

I've tried 我试过了

string result = Convert.ToString(Convert.ToUInt64(value, 16), 2);

but this results in a confusing compiler error: 但这会导致令人困惑的编译器错误:

The best overloaded method match for 'System.Convert.ToString(object, System.IFormatProvider)' has some invalid arguments 'System.Convert.ToString(object,System.IFormatProvider)'的最佳重载方法匹配有一些无效的参数

Argument 2: cannot convert from 'int' to 'System.IFormatProvider' 参数2:无法从'int'转换为'System.IFormatProvider'

What's wrong with the following code? 以下代码有什么问题?

string hex = "FFFFFFFFFFFFFFFF";

// Returns -1
long longValue = Convert.ToInt64(hex, 16);

// Returns 1111111111111111111111111111111111111111111111111111111111111111
string binRepresentation = Convert.ToString(longValue, 2);

Pretty much what you wrote (only fixed the ulong to long cast), and returns what you expect. 几乎你写的东西(只修了ulonglong演员),并返回你所期望的。

Edit: undeleted this answer, as even if the long representation is signed, the binary representation is actually what you expect. 编辑:取消删除此答案,因为即使long表示已签名,二进制表示实际上是您所期望的。

There might be a better solution, but check if this works: 可能有更好的解决方案,但请检查是否有效:

public static string HexToBinary(string hexValue)
{
    ulong number = UInt64.Parse(hexValue, System.Globalization.NumberStyles.HexNumber);

    byte[] bytes = BitConverter.GetBytes(number);

    string binaryString = string.Empty;
    foreach (byte singleByte in bytes)
    {
        binaryString += Convert.ToString(singleByte, 2);
    }

    return binaryString;
}

The most convenient way would be to use Convert.ToString(Int64, Int32) , but there is no overload for ulong. 最方便的方法是使用Convert.ToString(Int64, Int32) ,但ulong没有重载。 Another solution is Convert.ToString(UInt64, IFormatProvider) and write your own IFormatProvider. 另一个解决方案是Convert.ToString(UInt64, IFormatProvider)并编写自己的IFormatProvider。 By looking at the examples I found an IFormatProvider that formats numbers in binary, octal and hex string representation: http://msdn.microsoft.com/en-us/library/system.icustomformatter.aspx . 通过查看示例,我找到了一个IFormatProvider,它以二进制,八进制和十六进制字符串表示格式化数字: http//msdn.microsoft.com/en-us/library/system.icustomformatter.aspx The code there looks very similar to what I provided, so I thinks its not a bad solution. 那里的代码与我提供的代码非常相似,所以我认为它不是一个糟糕的解决方案。

The best choice is : 最好的选择是:

public static string hex2bin(string value)
        {
            return Convert.ToString(Convert.ToInt32(value, 16), 2).PadLeft(value.Length * 4, '0');
        }

Here's a brute approach, no pancy 64bit limit: 这是一个粗暴的方法,没有pancy 64bit限制:

string HexStringToBinary(string hexString)
{
    var lup = new Dictionary<char, string>{
            { '0', "0000"},
            { '1', "0001"},
            { '2', "0010"}, 
            { '3', "0011"},

            { '4', "0100"}, 
            { '5', "0101"}, 
            { '6', "0110"}, 
            { '7', "0111"},

            { '8', "1000"}, 
            { '9', "1001"}, 
            { 'A', "1010"}, 
            { 'B', "1011"},

            { 'C', "1100"}, 
            { 'D', "1101"}, 
            { 'E', "1110"}, 
            { 'F', "1111"}};                

    var ret = string.Join("", from character in hexString
                              select lup[character]);
    return ret;
}

If you used this to convert the hex string into a BitArray then the task of producing the binary representation is trivial: 如果使用这种以十六进制字符串转换成BitArray然后产生二进制表示的任务很简单:

BitArray barray = ConvertHexToBitArray(string hexData)
var sbuild = new StringBuilder();
for (int i = 0; i < barray.Length; i++)
{
    sbuild.Append(barray[i] ? "1" : "0");
}
Console.WriteLine(sbuild.ToString());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM