help float converting

Y

Yasin Cepeci

I ve get float data from serial port. I ve taken it in the form of hex by
modbus protocol. I know it is float but I couldnt convert it
there is a few sample data below;

B3 33 43 34 = 180.699997
33 33 43 33 = 179.199997
B3 33 43 34= 180.699997
CC CD 43 33=179.800003

But how can I found it. I couldnt resolve it.
 
N

Nicholas Paldino [.NET/C# MVP]

Yasin,

Unfortunately, there is nothing in the framework that will help you with
this. I would say use the BitConverter class, but it will not convert the
byte array using the modbus protocol. It uses the IEEE format for floating
point numbers.

You will have to write this yourself.

Hope this helps.
 
R

rossum

I ve get float data from serial port. I ve taken it in the form of hex by
modbus protocol. I know it is float but I couldnt convert it
there is a few sample data below;

B3 33 43 34 = 180.699997
33 33 43 33 = 179.199997
B3 33 43 34= 180.699997
CC CD 43 33=179.800003

But how can I found it. I couldnt resolve it.
It looks as if you just need to reorder the bytes and cast to float.

180.70000 -> 33-B3-34-43
179.20000 -> 33-33-33-43
180.70000 -> 33-B3-34-43
179.80000 -> CD-CC-33-43

Code used:

static void Main() {

float[] test = { 180.699997F,
179.199997F,
180.699997F,
179.800003F };
foreach (float ff in test) {
byte[] bytes = BitConverter.GetBytes(ff);
Console.WriteLine("{0:F5} -> {1}",
ff, BitConverter.ToString(bytes));
}
} // end Main()

rossum
 
Y

Yasin Cepeci

O I See but How can I turn the converting proccess below reverse. in other
words convert to float from hex or binary(32bit).
10101010101011111101101010101010--->xxx.xxxxxF; to float value:)





haber iletisinde sunlari said:
I ve get float data from serial port. I ve taken it in the form of hex by
modbus protocol. I know it is float but I couldnt convert it
there is a few sample data below;

B3 33 43 34 = 180.699997
33 33 43 33 = 179.199997
B3 33 43 34= 180.699997
CC CD 43 33=179.800003

But how can I found it. I couldnt resolve it.
It looks as if you just need to reorder the bytes and cast to float.

180.70000 -> 33-B3-34-43
179.20000 -> 33-33-33-43
180.70000 -> 33-B3-34-43
179.80000 -> CD-CC-33-43

Code used:

static void Main() {

float[] test = { 180.699997F,
179.199997F,
180.699997F,
179.800003F };
foreach (float ff in test) {
byte[] bytes = BitConverter.GetBytes(ff);
Console.WriteLine("{0:F5} -> {1}",
ff, BitConverter.ToString(bytes));
}
} // end Main()

rossum
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Yasin said:
O I See but How can I turn the converting proccess below reverse. in other
words convert to float from hex or binary(32bit).
10101010101011111101101010101010--->xxx.xxxxxF; to float value:)

Us the BitConverter.ToString method to convert four bytes of a byte
array into a float value.
haber iletisinde sunlari said:
I ve get float data from serial port. I ve taken it in the form of hex by
modbus protocol. I know it is float but I couldnt convert it
there is a few sample data below;

B3 33 43 34 = 180.699997
33 33 43 33 = 179.199997
B3 33 43 34= 180.699997
CC CD 43 33=179.800003

But how can I found it. I couldnt resolve it.
It looks as if you just need to reorder the bytes and cast to float.

180.70000 -> 33-B3-34-43
179.20000 -> 33-33-33-43
180.70000 -> 33-B3-34-43
179.80000 -> CD-CC-33-43

Code used:

static void Main() {

float[] test = { 180.699997F,
179.199997F,
180.699997F,
179.800003F };
foreach (float ff in test) {
byte[] bytes = BitConverter.GetBytes(ff);
Console.WriteLine("{0:F5} -> {1}",
ff, BitConverter.ToString(bytes));
}
} // end Main()

rossum
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top