Click here to Skip to main content
15,881,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to convert a binary string into hexadecimal in c#, How can i convert the binary string into hexadecimal? i need code in c#??
for example

string str="0101010101011100";

who can i convert this string to the hexadecimal, i need code in c#??
Posted
Updated 16-Sep-14 19:01pm
v2
Comments
BillWoodruff 17-Sep-14 1:00am    
Is the "binary string" to be converted always the same length: if so, what is that length ? Do you need to do error checking, like making sure every digit in the string is either zero, or one, and making sure that the length of the string is always evenly divided by eight (length of a byte) ?
_Maxxx_ 17-Sep-14 1:07am    
I'm not having a go at you, I am genuinely interested. Could you explain why you haven't just searched for a solution using Google? I searched for "convert binary string to hexadecimal c#" for example, and found 138,000 hits, many of which have code samples?
(Of course, thanks to Prasad below, it will now be 138,001)
BillWoodruff 17-Sep-14 1:30am    
Because CP QA is where everyone he knows gets their answers quick ?
Member 11063279 24-Sep-14 10:25am    
yup it's the best plat form :)

you can make it from the beginning , convert Binary to decimal and convert decimal to hexadecimal
here are a complete console application that can help you in that.

C#
using System;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string BinaryStr = "1000101001110";
            int Index = 0;
            int Decimal = 0;
            foreach (char Char in BinaryStr.Reverse())
            {
                if (Index != 0)
                {
                    Decimal += Index * 2 * Convert.ToInt32(Char.ToString());
                    Index = Index * 2;
                }
                else
                {
                    Decimal += Convert.ToInt32(Char.ToString());
                    Index++;
                }
            }
            Console.WriteLine("hexadecimal no for : " + BinaryStr + "   =>  " + Decimal.ToString());
            string Hexa1 = toHex(Convert.ToInt64(Decimal));
 Console.WriteLine("hexadecimal no for : " + BinaryStr + "   =>  " + Hexa1 );
        }
        public static string toHex(Int64 d)
        {
            var r = d % 16;
            string result;
            if (d - r == 0)
                result = toChar(Convert.ToInt32(r));
            else
                result = toHex((d - r) / 16) + toChar(Convert.ToInt32(r));
            return result;
        }
        public static string toChar(int n)
        {
            const string alpha = "0123456789ABCDEF";
            return alpha.Substring(n, 1);
        } 
}
 
Share this answer
 
In this very specific case this line of code would be sufficient.
C#
ushort result = Convert.ToUInt16(str, 2);

Very limited if the string is not padded correctly, though.

If the a string is wanted as a result, just write
C#
result.ToString("X4");
 
Share this answer
 
C#
public static string BinaryStringToHexString(string binary)
{
    StringBuilder result = new StringBuilder(binary.Length / 8 + 1);

    // TODO: check all 1's or 0's... Will 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();
}
 
Share this answer
 
Comments
_Maxxx_ 17-Sep-14 1:08am    
it's traditional and polite to quote your sources
BillWoodruff 17-Sep-14 1:29am    
My vote of #1: you fail to note that you copied this code from StackOverflow, and give credit to its author, Mitch Wheat.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900