最近用c#解析二进制文件,以下是代码:
FileStream fs = new FileStream(@"F:\1.dat", FileMode.Open); BinaryReader br = new BinaryReader(fs, Encoding.GetEncoding("gb2312")); for (int j = 0; j < (fs.Length) / 156; j++)//156是结构体的总字节数大小
{
long ltine = br.ReadInt32(); DateTime stocktime= UTCToDateTime(ltine); byte[] byte_code = br.ReadBytes(12);
string strcode = Encoding.Default.GetString(byte_code).TrimEnd('\0'); byte[] byte_name = br.ReadBytes(15);
string strname = Encoding.Default.GetString(byte_name).TrimEnd('\0'); short flag = br.ReadInt16(); float preJieSuanPrice = br.ReadSingle();
//byte[] byte_preJieSuanPrice = br.ReadBytes(4);
//float preJieSuanPrice = BitConverter.ToSingle(byte_preJieSuanPrice,0); ...... }读到这里就有问题了,
float preJieSuanPrice = br.ReadSingle();
正常值应该是720.75000,但这里读出的是6.263535E-39,请问是方法不对吗?还是?
FileStream fs = new FileStream(@"F:\1.dat", FileMode.Open); BinaryReader br = new BinaryReader(fs, Encoding.GetEncoding("gb2312")); for (int j = 0; j < (fs.Length) / 156; j++)//156是结构体的总字节数大小
{
long ltine = br.ReadInt32(); DateTime stocktime= UTCToDateTime(ltine); byte[] byte_code = br.ReadBytes(12);
string strcode = Encoding.Default.GetString(byte_code).TrimEnd('\0'); byte[] byte_name = br.ReadBytes(15);
string strname = Encoding.Default.GetString(byte_name).TrimEnd('\0'); short flag = br.ReadInt16(); float preJieSuanPrice = br.ReadSingle();
//byte[] byte_preJieSuanPrice = br.ReadBytes(4);
//float preJieSuanPrice = BitConverter.ToSingle(byte_preJieSuanPrice,0); ...... }读到这里就有问题了,
float preJieSuanPrice = br.ReadSingle();
正常值应该是720.75000,但这里读出的是6.263535E-39,请问是方法不对吗?还是?
解决方案 »
- replace 替换问题
- TabControlEx 控件有误 无法在 DLL“user32.dll”中找到名为“FindWindowGraphicsEx”的入口点。
- 去哪里查常用集合类的数据结构
- src="http://IM.CQ3W.CN/DZK.asp?Shop=1001&webWadarIndex=3&DZK_WindowTitle=商讯IM">
- 刚才搞错了。amandag(高歌) 来取分,仍然感谢lovefootball(蟑螂)
- 有个Exception居然 Catch 不住,特高分请教高手
- c#编程问题(类似于web)
- 向各位大侠请问,如果保存TextBox中的数据,但是不影响格式?不够加分
- 怎样将数据打包呀
- IL是解释运行的吗?
- linq to sql 如何删除未插入的数据?
- C#系统日志,跪求,在线等
BinaryReader br = new BinaryReader(fs);byte[] buffer= br.ReadBytes((int)fs.Length);
youStruct=(youStruct)BytesToStruct(buffer,youStruct.GetType());//得到你结构体
。。br.Close();
fs.Close();
public Object BytesToStruct(Byte[] bytes, Type strcutType)
{
Int32 size = Marshal.SizeOf(strcutType);
IntPtr buffer = Marshal.AllocHGlobal(size);
try
{
Marshal.Copy(bytes, 0, buffer, size);
return Marshal.PtrToStructure(buffer, strcutType);
}
finally
{
Marshal.FreeHGlobal(buffer);
}
}
//byte[] byte_preJieSuanPrice = br.ReadBytes(4);你把这四个字节的内容贴出来看看