Converting long string of binary to hex c#

前端 未结 10 968
無奈伤痛
無奈伤痛 2020-11-29 11:25

I\'m looking for a way to convert a long string of binary to a hex string.

the binary string looks something like this \"01100110100101110010011101010111001101

相关标签:
10条回答
  • 2020-11-29 11:59

    Considering four bits can be expressed by one hex value, you can simply go by groups of four and convert them seperately, the value won't change that way.

    string bin = "11110110";
    
    int rest = bin.Length % 4;
    bin = bin.PadLeft(rest, '0'); //pad the length out to by divideable by 4
    
    string output = "";
    
    for(int i = 0; i <= bin.Length - 4; i +=4)
    {
        output += string.Format("{0:X}", Convert.ToByte(bin.Substring(i, 4), 2));
    }
    
    0 讨论(0)
  • 2020-11-29 12:01

    I came up with this method. I am new to programming and C# but I hope you will appreciate it:

    static string BinToHex(string bin)
    {
        StringBuilder binary = new StringBuilder(bin);
        bool isNegative = false;
        if (binary[0] == '-')
        {
            isNegative = true;
            binary.Remove(0, 1);
        }
    
        for (int i = 0, length = binary.Length; i < (4 - length % 4) % 4; i++) //padding leading zeros
        {
            binary.Insert(0, '0');
        }
    
        StringBuilder hexadecimal = new StringBuilder();
        StringBuilder word = new StringBuilder("0000");
        for (int i = 0; i < binary.Length; i += 4)
        {
            for (int j = i; j < i + 4; j++)
            {
                word[j % 4] = binary[j];
            }
    
            switch (word.ToString())
            {
                case "0000": hexadecimal.Append('0'); break;
                case "0001": hexadecimal.Append('1'); break;
                case "0010": hexadecimal.Append('2'); break;
                case "0011": hexadecimal.Append('3'); break;
                case "0100": hexadecimal.Append('4'); break;
                case "0101": hexadecimal.Append('5'); break;
                case "0110": hexadecimal.Append('6'); break;
                case "0111": hexadecimal.Append('7'); break;
                case "1000": hexadecimal.Append('8'); break;
                case "1001": hexadecimal.Append('9'); break;
                case "1010": hexadecimal.Append('A'); break;
                case "1011": hexadecimal.Append('B'); break;
                case "1100": hexadecimal.Append('C'); break;
                case "1101": hexadecimal.Append('D'); break;
                case "1110": hexadecimal.Append('E'); break;
                case "1111": hexadecimal.Append('F'); break;
                default:
                    return "Invalid number";
            }
        }
    
        if (isNegative)
        {
            hexadecimal.Insert(0, '-');
        }
    
        return hexadecimal.ToString();
    }
    
    0 讨论(0)
  • 2020-11-29 12:01

    If you're using .NET 4.0 or later and if you're willing to use System.Numerics.dll (for BigInteger class), the following solution works fine:

    public static string ConvertBigBinaryToHex(string bigBinary)
    {
        BigInteger bigInt = BigInteger.Zero;
        int exponent = 0;
    
        for (int i = bigBinary.Length - 1; i >= 0; i--, exponent++)
        {
            if (bigBinary[i] == '1')
                bigInt += BigInteger.Pow(2, exponent);
        }
    
        return bigInt.ToString("X");
    }
    
    0 讨论(0)
  • 2020-11-29 12:03
    Convert.ToInt32("1011", 2).ToString("X");
    

    For string longer than this, you can simply break it into multiple bytes:

    var binary = "0110011010010111001001110101011100110100001101101000011001010110001101101011";
    var hex = string.Join(" ", 
                Enumerable.Range(0, binary.Length / 8)
                .Select(i => Convert.ToByte(binary.Substring(i * 8, 8), 2).ToString("X2")));
    
    0 讨论(0)
提交回复
热议问题