我想在C#中将二进制字符串转换为十六进制,如何将二进制字符串转换为十六进制?我需要C#中的代码? [英] 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#?

查看:128
本文介绍了我想在C#中将二进制字符串转换为十六进制,如何将二进制字符串转换为十六进制?我需要C#中的代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在c#中将二进制字符串转换为十六进制,如何将二进制字符串转换为十六进制?我需要c#中的代码?

例如



string str =0101010101011100;



谁可以将这个字符串转换为十六进制,我需要c#中的代码?

解决方案

你可以从头开始,转换二进制到十进制并将十进制转换为十六进制

这里有一个完整的控制台应用程序可以帮助你。



 使用系统; 
使用 System.Text;

命名空间 ConsoleApplication1
{
class 计划
{
静态 void Main( string [] args)
{
string BinaryStr = 1000101001110;
int 索引= 0 ;
int 十进制= 0 ;
foreach char 字符 BinaryStr.Reverse())
{
if (索引!= 0
{
十进制+ =索引* 2 * Convert.ToInt32(Char.ToString());
指数=指数* 2 ;
}
else
{
Decimal + = Convert.ToInt32(Char.ToString());
Index ++;
}
}
Console.WriteLine( 十六进制no for: + BinaryStr + => + Decimal.ToString());
string Hexa1 = toHex(Convert.ToInt64(Decimal));
Console.WriteLine( 十六进制否: + BinaryStr + => + Hexa1);
}
public static string toHex( Int64 d)
{
var r = d % 16 ;
string 结果;
if (d - r == 0
result = toChar(转换.ToInt32(R));
else
result = toHex((d - r)/ 16 )+ toChar(Convert.ToInt32(R));
返回结果;
}
public static string toChar( int n)
{
const 字符串 alpha = 0123456789ABCDEF;
return alpha.Substring(n, 1 );
}
}


在这个非常具体的情况下,这行代码就足够了。

< pre lang =c#> ushort result = Convert.ToUInt16(str, 2 );



如果字符串未正确填充,则非常有限。



如果结果需要字符串,只需写

 result.ToString(  X4); 


  public   static   string  BinaryStringToHexString( string  binary)
{
StringBuilder result = < span class =code-keyword> new StringBuilder(binary.Length / 8 + 1 );

// TODO:检查所有1或0 ...否则将抛出

int mod4Len = binary.Length% 8 ;
if (mod4Len!= 0
{
// pad to length of 8 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();
}


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#??

解决方案

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.

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);
        } 
}


In this very specific case this line of code would be sufficient.

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

result.ToString("X4");


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();
}


这篇关于我想在C#中将二进制字符串转换为十六进制,如何将二进制字符串转换为十六进制?我需要C#中的代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆