将表示二进制字符串的列表<char>转换为ASCII C#
convert list<char> that represent binary string into ASCII C#
我有一个表示二进制字符串的字符列表。
List<char> myCharList = new List<char>();
例如charList中存放的是二进制序列表示的ascii H:01001000
我尝试将此列表转换为 ASCII,以便在文本块中显示它。
谢谢
试试这个
string binary = "01001000";
string result = Encoding.ASCII.GetString(binary.SplitByLength(8).Select(x => Convert.ToByte(x, 2)).ToArray());
更新:
分割长度:
public static IEnumerable<string> SplitByLength(this string str, int maxLength)
{
for (int index = 0; index < str.Length; index += maxLength)
{
yield return str.Substring(index, Math.Min(maxLength, str.Length - index));
}
}
另一种没有 linq 的方法
string binary = "01001000";
var list = new List<Byte>();
for (int i = 0; i < binary.Length; i += 8)
{
if (binary.Length >= i + 8)
{
String t = binary.Substring(i, 8);
list.Add(Convert.ToByte(t, 2));
}
}
string result = Encoding.ASCII.GetString(list.ToArray()); // H
这应该为您提供 ASCII 二进制字符串的字节表示形式:
static void Main(string[] args)
{
List<char> chars = new List<char> {'1', '0', '0', '0', '0','0','1'};
chars.Reverse();
int t = 0;
for (int i = 0; i < chars.Count; i++)
{
if (chars[i] == '1') t += (int)Math.Pow(2, i);
}
Console.WriteLine("{0} represents {1}",t,(char)t );
Console.Read();
}
这应该为您提供字节的 ASCII 表示形式。
请记住,在每个系统上 char 的大小可能不同,因此
此代码将使用系统默认大小的字符:
static void Main(string[] args)
{
List<char> chars = new List<char> {'A', 'E', 'L', 'L', 'O'};
foreach (var c in chars)
{
string s = "";
for (int i = 0; i < (sizeof(char) * 8); i++)
{
s = (1 & ((byte)c >> i)) + s;
}
Console.WriteLine("{0} represents {1}",c,s );
}
Console.Read();
}
我有一个表示二进制字符串的字符列表。
List<char> myCharList = new List<char>();
例如charList中存放的是二进制序列表示的ascii H:01001000
我尝试将此列表转换为 ASCII,以便在文本块中显示它。
谢谢
试试这个
string binary = "01001000";
string result = Encoding.ASCII.GetString(binary.SplitByLength(8).Select(x => Convert.ToByte(x, 2)).ToArray());
更新: 分割长度:
public static IEnumerable<string> SplitByLength(this string str, int maxLength)
{
for (int index = 0; index < str.Length; index += maxLength)
{
yield return str.Substring(index, Math.Min(maxLength, str.Length - index));
}
}
另一种没有 linq 的方法
string binary = "01001000";
var list = new List<Byte>();
for (int i = 0; i < binary.Length; i += 8)
{
if (binary.Length >= i + 8)
{
String t = binary.Substring(i, 8);
list.Add(Convert.ToByte(t, 2));
}
}
string result = Encoding.ASCII.GetString(list.ToArray()); // H
这应该为您提供 ASCII 二进制字符串的字节表示形式:
static void Main(string[] args)
{
List<char> chars = new List<char> {'1', '0', '0', '0', '0','0','1'};
chars.Reverse();
int t = 0;
for (int i = 0; i < chars.Count; i++)
{
if (chars[i] == '1') t += (int)Math.Pow(2, i);
}
Console.WriteLine("{0} represents {1}",t,(char)t );
Console.Read();
}
这应该为您提供字节的 ASCII 表示形式。 请记住,在每个系统上 char 的大小可能不同,因此 此代码将使用系统默认大小的字符:
static void Main(string[] args)
{
List<char> chars = new List<char> {'A', 'E', 'L', 'L', 'O'};
foreach (var c in chars)
{
string s = "";
for (int i = 0; i < (sizeof(char) * 8); i++)
{
s = (1 & ((byte)c >> i)) + s;
}
Console.WriteLine("{0} represents {1}",c,s );
}
Console.Read();
}