微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在C#中为String到Binary

如何解决在C#中为String到Binary

| 我有一个函数可以将字符串转换为十六进制,
public static string ConvertToHex(string asciiString)
{
    string hex = \"\";
    foreach (char c in asciiString)
    {
         int tmp = c;
         hex += String.Format(\"{0:x2}\",(uint)System.Convert.ToUInt32(tmp.ToString()));
    }
    return hex;
}
您能帮我根据我的示例函数向Binary函数写另一个字符串吗?
public static string ConvertToBin(string asciiString)
{
    string bin = \"\";
    foreach (char c in asciiString)
    {
        int tmp = c;
        bin += String.Format(\"{0:x2}\",(uint)System.Convert.????(tmp.ToString()));
    }
    return bin;
}
    

解决方法

干得好:
public static byte[] ConvertToByteArray(string str,Encoding encoding)
{
    return encoding.GetBytes(str);
}

public static String ToBinary(Byte[] data)
{
    return string.Join(\" \",data.Select(byt => Convert.ToString(byt,2).PadLeft(8,\'0\')));
}

// Use any sort of encoding you like. 
var binaryString = ToBinary(ConvertToByteArray(\"Welcome,World!\",Encoding.ASCII));
    ,听起来您基本上是想将ASCII字符串,或更优选地,将byte [](因为您可以使用首选的编码模式将字符串编码为byte [])变成一串零。即10101001001010010010010100101001010010100101001010101000010111101101010 这将为您做...
//Formats a byte[] into a binary string (010010010010100101010)
public string Format(byte[] data)
{
    //storage for the resulting string
    string result = string.Empty;
    //iterate through the byte[]
    foreach(byte value in data)
    {
        //storage for the individual byte
        string binarybyte = Convert.ToString(value,2);
        //if the binarybyte is not 8 characters long,its not a proper result
        while(binarybyte.Length < 8)
        {
            //prepend the value with a 0
            binarybyte = \"0\" + binarybyte;
        }
        //append the binarybyte to the result
        result += binarybyte;
    }
    //return the result
    return result;
}
    ,下面将为您提供每个字符低字节的十六进制编码,看起来像您要的内容:
StringBuilder sb = new StringBuilder();
foreach (char c in asciiString)
{
    uint i = (uint)c;
    sb.AppendFormat(\"{0:X2}\",(i & 0xff));
}
return sb.ToString();
    ,这是扩展功能:
        public static string ToBinary(this string data,bool formatBits = false)
        {
            char[] buffer = new char[(((data.Length * 8) + (formatBits ? (data.Length - 1) : 0)))];
            int index = 0;
            for (int i = 0; i < data.Length; i++)
            {
                string binary = Convert.ToString(data[i],\'0\');
                for (int j = 0; j < 8; j++)
                {
                    buffer[index] = binary[j];
                    index++;
                }
                if (formatBits && i < (data.Length - 1))
                {
                    buffer[index] = \' \';
                    index++;
                }
            }
            return new string(buffer);
        }
您可以像这样使用它:
Console.WriteLine(\"Testing\".ToBinary());
如果您添加\'true \'作为参数,它将自动分隔每个二进制序列。     

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。