UInt32数据的表示 比如一个txt文件中有32个字符全部为0或1,如何才能把这32个字符写入UInt32类型,32位0或1的二进制表示4个字节,但不知道该怎么转,请各位指点。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 2进制转换成16进制的 在赋给UINT32? 刚写的,凑合看一下:-------namespace TestSync{ class Program { static void Main(string[] args) { string str = "11101000"; //101010的字符串, Char[] chars = str.ToCharArray(); UInt32 result = 0; for (int i = 0; i < chars.Length; i++) { Console.WriteLine(chars[i]); result += ((UInt32)Math.Pow(2, chars.Length - 1 - i)) * Convert.ToUInt32(chars[i].ToString()); } Console.WriteLine(result); } }} class Program { static void Main(string[] args) { string input = "00000000000000000000000000000011"; UInt32 output = Convert.ToUInt32(input, 2); } } class Program { static void Main(string[] args) { string input = "00000000000000000000000000000011"; //UInt32 output = Convert.ToUInt32(input, 2); UInt32 output = 0; foreach (char a in input.ToCharArray()) { UInt32 tmp = 0; if ('0' == a) { tmp = 0; } else if ('1' == a) { tmp = 1; } else { //error; return; } output <<= 1; output |= tmp; } } } 先当字符串读入,然后转化为int类型,转化时指定目标数据的进制为2进制. System.IO.StreamReader sr = new System.IO.StreamReader("C:\\1.txt");string bb = sr.ReadToEnd();uint aa=Convert.ToUInt32(bb, 2); string str = "11101000";Console.WriteLine(Convert.ToUInt32(str, 2));可以直接把字符串作为二进制转换。不错。 byte[] bytes = new byte[4];uint u=BitConverter.ToUInt32(bytes, 0); 首先将文件中的32个字符读到字符串str中,再利用Convert.ToUInt32(str, 2)函数即可达到目的。 byte[] bytes = new byte[4];uint u=BitConverter.ToUInt32(bytes, 0); BitConverter.ToUInt32具体可参考msdn Winform实现帮助功能 C# ftp上传图片的时候,报错“无法访问已释放的资源” C#查询SQL数据库行数问题。 高分!!!发个贴问几个问题 写程序过程中遇到些问题,,想不明白,,,请高手指教下, this.Context.Parameters["targetdir"]//获得值是空值???????? 求助!!~~~~急 急死人了,帮忙~~~ 调用子窗体的问题 C# 线程处理数据的问题 如何用c#调用Cygwin生成的dll? 初学数据结构链表中有点不明白的,高手过来指导下。
-------
namespace TestSync
{
class Program
{
static void Main(string[] args)
{
string str = "11101000"; //101010的字符串,
Char[] chars = str.ToCharArray();
UInt32 result = 0; for (int i = 0; i < chars.Length; i++)
{
Console.WriteLine(chars[i]);
result += ((UInt32)Math.Pow(2, chars.Length - 1 - i)) * Convert.ToUInt32(chars[i].ToString());
}
Console.WriteLine(result);
}
}
}
class Program
{
static void Main(string[] args)
{
string input = "00000000000000000000000000000011";
UInt32 output = Convert.ToUInt32(input, 2);
}
}
class Program
{
static void Main(string[] args)
{
string input = "00000000000000000000000000000011";
//UInt32 output = Convert.ToUInt32(input, 2);
UInt32 output = 0;
foreach (char a in input.ToCharArray())
{
UInt32 tmp = 0;
if ('0' == a)
{
tmp = 0;
}
else if ('1' == a)
{
tmp = 1;
}
else
{
//error;
return;
} output <<= 1;
output |= tmp;
}
}
}
string bb = sr.ReadToEnd();
uint aa=Convert.ToUInt32(bb, 2);
Console.WriteLine(Convert.ToUInt32(str, 2));可以直接把字符串作为二进制转换。不错。
uint u=BitConverter.ToUInt32(bytes, 0);
uint u=BitConverter.ToUInt32(bytes, 0);