Converting long string of binary to hex c#

前端 未结 10 967
無奈伤痛
無奈伤痛 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:40

    I just knocked this up. Maybe you can use as a starting point...

    public static string BinaryStringToHexString(string binary)
    {
        if (string.IsNullOrEmpty(binary))
            return binary;
    
        StringBuilder result = new StringBuilder(binary.Length / 8 + 1);
    
        // TODO: check all 1's or 0's... throw otherwise
    
        int mod4Len = binary.Length % 8;
        if (mod4Len != 0)
        {
            // pad to length multiple of 8
            binary = binary.PadLeft(((binary.Length / 8) + 1) * 8, '0');
        }
    
        for (int i = 0; i < binary.Length; i += 8)
        {
            string eightBits = binary.Substring(i, 8);
            result.AppendFormat("{0:X2}", Convert.ToByte(eightBits, 2));
        }
    
        return result.ToString();
    }
    
    0 讨论(0)
  • 2020-11-29 11:47

    You can take the input number four digit at a time. Convert this digit to ex ( as you did is ok ) then concat the string all together. So you obtain a string representing the number in hex, independetly from the size. Depending on where start MSB on your input string, may be the output string you obtain the way i described must be reversed.

    0 讨论(0)
  • 2020-11-29 11:51

    If you want to iterate over the hexadecimal representation of each byte in the string, you could use the following extension. I've combined Mitch's answer with this.

    static class StringExtensions
    {
        public static IEnumerable<string> ToHex(this String s) {
            if (s == null)
                throw new ArgumentNullException("s");
    
            int mod4Len = s.Length % 8;
            if (mod4Len != 0)
            {
                // pad to length multiple of 8
                s = s.PadLeft(((s.Length / 8) + 1) * 8, '0');
            }
    
            int numBitsInByte = 8;
            for (var i = 0; i < s.Length; i += numBitsInByte)
            {
                string eightBits = s.Substring(i, numBitsInByte);
                yield return string.Format("{0:X2}", Convert.ToByte(eightBits, 2));
            }
        }
    }
    

    Example:

    string test = "0110011010010111001001110101011100110100001101101000011001010110001101101011";
    
    foreach (var hexVal in test.ToHex())
    {
        Console.WriteLine(hexVal);  
    }
    

    Prints

    06
    69
    72
    75
    73
    43
    68
    65
    63
    6B
    
    0 讨论(0)
  • 2020-11-29 11:52

    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;
    if(rest != 0)
        bin = new string('0', 4-rest) + bin; //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 11:55

    This might help you:

    string HexConverted(string strBinary)
        {
            string strHex = Convert.ToInt32(strBinary,2).ToString("X");
            return strHex;
        }
    
    0 讨论(0)
  • 2020-11-29 11:58
    static string BinToHex(string bin)
    {
        if (bin == null)
            throw new ArgumentNullException("bin");
        if (bin.Length % 8 != 0)
            throw new ArgumentException("The length must be a multiple of 8", "bin");
    
        var hex = Enumerable.Range(0, bin.Length / 8)
                         .Select(i => bin.Substring(8 * i, 8))
                         .Select(s => Convert.ToByte(s, 2))
                         .Select(b => b.ToString("x2"));
        return String.Join(null, hex);
    }
    
    0 讨论(0)
提交回复
热议问题