473,657 Members | 2,451 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

converting byte to float

Hi
I have to convert byte value to float in C#.Float is in IEEE 754 format.
byte value is= 70 23 22 195 in a byte array.
and its float value is = -150.0909
what is the algoritm for this conversion?
thanks for help
Nov 17 '05 #1
3 5740
Hi Aykut,

You can use a MemoryStream to create a Stream of your byte data, then read a float (Single) from the stream using a BinaryReader.

byte[] b = new byte[]{70, 23, 22, 195};
float f = 0;
using(MemoryStr eam ms = new MemoryStream(b) )
{
using(BinaryRea der br = new BinaryReader(ms ))
{
f = br.ReadSingle() ;
}
}

--
Happy coding!
Morten Wennevik [C# MVP]
Nov 17 '05 #2
> byte value is= 70 23 22 195 in a byte array.
and its float value is = -150.0909
what is the algoritm for this conversion?


I think System.BitConve rter might do what you want.

public static float ToSingle(
byte[] value,
int startIndex
);
Nov 17 '05 #3
Thanks for the replies .
But I d rather like to know the algorithm behind it.
if it isnt too complicated :)
regards
Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
4383
by: Giovanni Azua | last post by:
Hello all, I need to return back to TSQL two numeric values from an Extended Stored Procedure developed in C. As result my C dll program produces two float values, the TSQL side expects to have exactly: numeric(10, 5) and numeric. Given that the structure to fill-up is DBNUMERIC defined as:
8
2911
by: Rick | last post by:
Hi, Does C have some handy functions to convert chars, ints and floats to bit arrays? I need to store that stuff binary so a few functions would be great. Converting chars and ints isn't difficult but floats are giving me a headache. And how about converting them back? I need a function like : <bitarray> convertFloatToBits( <number>, <signed or not>, <length default at 32 bits> ) By the way, the C program has to be suitable for a...
2
15848
by: Benjamin Rutt | last post by:
Does anyone have C code laying around to do this? I have to read in some binary data files that contains some 4-byte IBM/370 floating point values. I would like a function to convert 4-byte IBM/370 float values into IEEE 754 'float' values that I can use directly with any modern C environment (that uses IEEE 754). I can handle endian conversion myself (the files are stored as big endian, and I'll process them on a little endian...
2
6265
by: Sisnaz | last post by:
I'm reading byte information from a C++ program via TCP sockets. At one time I thought I came across away to convert C Float data type into a VB single data type. I'm reading the C data type in bytes from the socket stream but can't seem to convert it. Any help would be greatly appreciated. Here is my data: The actual values in the C++ application: Node 1 LL40 - Position
9
30507
by: Gregory.A.Book | last post by:
I am interested in converting sets of 4 bytes to floats in C++. I have a library that reads image data and returns the data as an array of unsigned chars. The image data is stored as 4-byte floats. How can I convert the sets of 4 bytes to floats? Thanks, Greg Book
116
35833
by: Dilip | last post by:
Recently in our code, I ran into a situation where were stuffing a float inside a double. The precision was extended automatically because of that. To make a long story short, this caused problems elsewhere in another part of the system where that figure was used for some calculation and some eventual truncation led to the system going haywire. So my question is, given this code: int main() { float f = 59.89F;
4
4796
by: Yasin Cepeci | last post by:
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.
2
2880
by: John Fisher | last post by:
Hi Group, troubles with converting signed 32.32, little-endian, 2's complement back to floating point. I have been trying to brew it myself. I am running Python 2.5 on a Mac. Here is the C-code I have been trying to leverage: double FPuint8ArrayToFPDouble(uint8 *buffer, int startIndex) { uint32 resultDec = 0;
9
5158
by: ssubbarayan | last post by:
Hi all, I am trying a program to convert floating point values to a byte array and printing the same to the screen.The idea behind this is we already have an existing function which can do byte level parsing what ever may be the type of data.The data would be coming from an external environment.When I parse int,char at byte level,I get right values,where as floating point just prints 0.000000 to the screen.Given below is a sample program...
0
8305
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8823
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8726
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8503
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6163
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4301
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1604
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.