字节转换问题-0,0,0,127 [英] Byte Conversion problem - 0,0,0,127

查看:126
本文介绍了字节转换问题-0,0,0,127的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我正在尝试将字节数组转换为可读格式(第二列是时间,但不必强制转换为时间,可以强制转换为字符串),我期望的是一个系列零,如下表所示,代表午夜.
日期....时间...........打开........等
10/07/2010 23:00:000 1.39330 1.39480 1.39090 1.39330 67817
10/08/2010 0:00:000 1.39330 1.39340 1.39020 1.39120 34728
10/08/2010 1:00:000 1.39130 1.39210 1.38490 1.38780 40004

同一列中其他数字的字节数组可以很好地转换.
例如:

Hi,
I''m trying to convert a byte array across to a readable format (the 2nd column is time but does not have to be cast as time, it could be a cast as a string), what I''m expecting is a series of zero''s like in the table below, which would represent midnight.
Date.........Time...........Open........etc
10/07/2010 23:00:000 1.39330 1.39480 1.39090 1.39330 67817
10/08/2010 0:00:000 1.39330 1.39340 1.39020 1.39120 34728
10/08/2010 1:00:000 1.39130 1.39210 1.38490 1.38780 40004

The byte array for the other numbers in the same column convert over fine.
For example:

byte[] bytes = { 0, 156, 96, 72 };
float Time = System.BitConverter.ToSingle(single, 0);


可以理解为230000.0(第二行的第一行),可以根据需要解析为一个时间.其他人也都工作正常.但是午夜


Works out as 230000.0 (top line 2nd column) which I can make sense of and parse out as a time if I need to. The others all work fine too. But midnight

byte[] bytes = { 0, 0, 0, 127 };
float Time = System.BitConverter.ToSingle(single, 0)



返回 1.70141183E + 38 这不是我期望的.:confused:

任何帮助将不胜感激.

问候
Flatstrap



returns 1.70141183E+38 which is not what I expect.:confused:

Any help would be greatly appreciated.

Regards
Flatstrap

推荐答案



我认为您的期望是不正确的.测试您的假设,例如

Hi,

I think your expectation is incorrect. Test your assumption, e.g.

class Program {
  static void Main(string[] args) {
    ShowBytes(0.0F);
    ShowBytes(1.70141183E+38F);
  }

  private static void ShowBytes(Single s) {
    Byte[] b = BitConverter.GetBytes(s);
    Console.WriteLine("{0} {{ {1}, {2}, {3}, {4} }}", s, b[0], b[1], b[2], b[3]);
  }



输出为



Output is

<pre>0 { 0, 0, 0, 0 }
1.701412E+38 { 0, 0, 0, 127 }




艾伦.




Alan.


不要尝试将其转换为 float .浮点数是有损的,并且具有舍入错误.将其转换为字符串 int uint ulong .您也可以保留 byte s,但将它们输出为十六进制.
Don''t try to convert it to float. Float is lossy, and has rounding errors. Convert it to string, int, uint, long or ulong. You may also keep the bytes, but output them as hex.


感谢大家的回复...
艾伦(Alan),我喜欢您的坦率回应:) Toli感谢您的建议.
对于任何对此进行搜索的人,我发现了一个简单的解决方案...

Thanks for the replies guys...
Alan I like your frank response :) And Toli thanks for the advice.
For anyone searching this I found a simple solution...

private float GetFloat()
{
byte[] bytes = { 0, 0, 0, 127 }
if (bytes[3] == 0) return 0.0f;
.
.
.
* code here*.
}




对于任何分钟数,这实际上将返回0.

欢呼声
Flatstrap




This effectively returns 0 for any minute number.

Cheers
Flatstrap


这篇关于字节转换问题-0,0,0,127的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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