Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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);

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)


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

Any help would be greatly appreciated.

Regards
Flatstrap
Posted

Hi,

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

C#
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.
 
Share this answer
 
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.
 
Share this answer
 
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*.
}



This effectively returns 0 for any minute number.

Cheers
Flatstrap
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900