473,738 Members | 6,332 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

converting float to individual bytes

TK
I'm used to programming in c or c++ in which my problem is simple.

I want to be able to enter a value on a page (like 3.2), and then read
it as a 32-bit float and break it into it's individual bytes.
I've tried using bitwise operators, but they seem to convert the value
into an integer first, and i've tried using the toString() method to
convert it into a hex value so i can parse it, but that also seems to
first convert it into an integer.

any help would be much appreciated.
Jul 23 '05 #1
25 6737
TK wrote on 03 jun 2005 in comp.lang.javas cript:
I'm used to programming in c or c++ in which my problem is simple.

I want to be able to enter a value on a page (like 3.2), and then read
it as a 32-bit float and break it into it's individual bytes.
I've tried using bitwise operators, but they seem to convert the value
into an integer first, and i've tried using the toString() method to
convert it into a hex value so i can parse it, but that also seems to
first convert it into an integer.

any help would be much appreciated.


Please look at the very explicit source of:

<http://babbage.cs.qc.e du/courses/cs341/IEEE-754hex32.html>

and all will be revealed to you.

--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Jul 23 '05 #2
TK
Evertjan. wrote:
TK wrote on 03 jun 2005 in comp.lang.javas cript:

I'm used to programming in c or c++ in which my problem is simple.

I want to be able to enter a value on a page (like 3.2), and then read
it as a 32-bit float and break it into it's individual bytes.
I've tried using bitwise operators, but they seem to convert the value
into an integer first, and i've tried using the toString() method to
convert it into a hex value so i can parse it, but that also seems to
first convert it into an integer.

any help would be much appreciated.

Please look at the very explicit source of:

<http://babbage.cs.qc.e du/courses/cs341/IEEE-754hex32.html>

and all will be revealed to you.


that appears to do a lot more than I need. Is there a simpler way?
All I need is to be able to input a value like 3.2 on screen, and
display each byte seperatly as 0x40 0x4C 0xCC 0xCC.

Jul 23 '05 #3
TK wrote on 03 jun 2005 in comp.lang.javas cript:
Evertjan. wrote:
TK wrote on 03 jun 2005 in comp.lang.javas cript:

I'm used to programming in c or c++ in which my problem is simple.

I want to be able to enter a value on a page (like 3.2), and then read
it as a 32-bit float and break it into it's individual bytes.
I've tried using bitwise operators, but they seem to convert the value
into an integer first, and i've tried using the toString() method to
convert it into a hex value so i can parse it, but that also seems to
first convert it into an integer.

any help would be much appreciated.
Please look at the very explicit source of:

<http://babbage.cs.qc.e du/courses/cs341/IEEE-754hex32.html>

and all will be revealed to you.


that appears to do a lot more than I need. Is there a simpler way?


But that is not what you asked!
All I need is to be able to input a value like 3.2 on screen, and
display each byte seperatly as 0x40 0x4C 0xCC 0xCC.


Impossible, because that format only supports integers, and a definition
of what you compoundly want is not clear.

What would those bytes represent, if not a complicated as on the babbage
site above?

--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Jul 23 '05 #4
"TK" <to****@hotmail .com> skrev i meddelandet
news:11******** *****@corp.supe rnews.com...
<snip>
All I need is to be able to input a value like 3.2 on screen, and
display each byte seperatly as 0x40 0x4C 0xCC 0xCC.


What if the native byte order is different on the machine?
What if float/fixed implementations vary on different platforms?

--
Joakim Braun
Jul 23 '05 #5
On 03/06/2005 18:20, TK wrote:
I want to be able to enter a value on a page (like 3.2), and then read
it as a 32-bit float and break it into it's individual bytes.
All numbers are represented internally as 64-bit, double-precision
values, according to IEEE 754. No built-in operators or functions will
provide you with direct access to this representation.
I've tried using bitwise operators, but they seem to convert the value
into an integer first
To a 32-bit, signed integer to be precise. That is how they are defined
by ECMA-262. The only exception is unsigned right shift (>>>), which
converts its left-hand operand to an unsigned integer.
i've tried using the toString() method to convert it into a hex value
so i can parse it, but that also seems to first convert it into an
integer.
Only base-10 representations are required to provide a floating-point
component when converting to a string. All other representations are
implementation dependent.
any help would be much appreciated.


I don't think much help can be provided. You must remember that
ECMAScript is a very high-level language. The ability to perform
low-level operations would have to be provided especially by the host
environment as an extension, or you'll have to write your own
string-handling code.

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #6
TK
Michael Winter wrote:
All numbers are represented internally as 64-bit, double-precision
values, according to IEEE 754. No built-in operators or functions will
provide you with direct access to this representation.


that would explain part of my problem. The documentation i had read
claimed all variables were floats, so I assumed they were IEEE 754
32-bit floats, as I'm used to in C++.

So, I guess this means the example provided by Evertanj is pretty much
just a starting point for what I need. and here i thought it was doing
more than I required.
Jul 23 '05 #7
TK
>>All I need is to be able to input a value like 3.2 on screen, and
display each byte seperatly as 0x40 0x4C 0xCC 0xCC.

Impossible, because that format only supports integers, and a definition
of what you compoundly want is not clear.

What would those bytes represent, if not a complicated as on the babbage
site above?


Yes, the hex format can show decimal values if you're using the IEEE-754
format.
which is the reason for my question. I'm trying to read in the value on
a web page that is going to be stored on a device that uses the IEEE-754
byte format, but has the low byte stored first. that is the reason I
need to be able to break the value down to the individual bytes, so I
can send them in reverse order.
Jul 23 '05 #8
JRS: In article <11************ *@corp.supernew s.com>, dated Fri, 3 Jun
2005 11:20:00, seen in news:comp.lang. javascript, TK
<to****@hotmail .com> posted :
I'm used to programming in c or c++ in which my problem is simple.

I want to be able to enter a value on a page (like 3.2), and then read
it as a 32-bit float and break it into it's individual bytes.
I've tried using bitwise operators, but they seem to convert the value
into an integer first, and i've tried using the toString() method to
convert it into a hex value so i can parse it, but that also seems to
first convert it into an integer.


To me, .toString(radix ) does not seem to first convert to integer.

Javascript does not have 32-bit floats, at least according to ECMA-262
Edn 3 IIRC. Floats are IEEE Doubles, occupying 8 bytes for the value.

Javascript provides no direct access to the value as bytes.

You can use arithmetic-type operations to separate out the sign, to
determine the base-2 exponent and the corresponding mantissa, use
..toString(radi x) or otherwise to convert to binary, and build the 8
bytes that truly represent an IEEE Double or the four for the
corresponding Single.

In fact, judging by my system, .toString(2) will for large numbers give
the mantissa in binary and the corresponding exponent in decimal, which
you can convert with another toString; just multiply your input by 1e100
and subtract 100 from the exponent (0.0 will need special treatment).

Your grammar-checker is broken.

--
© John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
<URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #9
TK wrote on 03 jun 2005 in comp.lang.javas cript:
All I need is to be able to input a value like 3.2 on screen, and
display each byte seperatly as 0x40 0x4C 0xCC 0xCC.
Impossible, because that format only supports integers, and a
definition of what you compoundly want is not clear.

What would those bytes represent, if not a complicated as on the
babbage site above?


Yes, the hex format can show decimal values if you're using the
IEEE-754 format.
which is the reason for my question. I'm trying to read in the value
on a web page that is going to be stored on a device that uses the
IEEE-754 byte format, but has the low byte stored first.


Even "the low byte stored first" has to be defined.

Is that a temporal "first" or a "spacial" one?
that is the
reason I need to be able to break the value down to the individual
bytes, so I can send them in reverse order.


So it is a serial device?

--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Jul 23 '05 #10

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

Similar topics

1
3366
by: Erinys | last post by:
Hi, I need to access the individual bytes in a string in my javascript function. If the characters in the string were all ascii characters then there would not be a problem, however in my case the string may contain ISO-2022-JP or UTF8 characters, so these characters may be larger than 1 byte each. Is there a way to extract the individual bytes, or convert a string to an array of bytes in javascript?
10
22792
by: pavithra.eswaran | last post by:
Hi, I would like to convert a single precision hexadecimal number to floating point. The following program seems to work fine.. But I do not want to use scanf. I already have a 32 bit hexadecimal number and would like to convert it into float. Can anyone tell me how to do it? int main() { float theFloat;
3
2341
by: Pete Davis | last post by:
I've never done this in C# so I don't know what the appropriate way of doing it is. I've got an array of bytes and I need to convert the array into "usable" data. For example, the first 4 bytes need to be converted to an enum. The next 4 bytes to a 32-bit int. And so on. I'm basically populating a struct with the data. How does one do this in C#? Thanks.
8
11156
by: avsrk | last post by:
Hello Folks , General C data types question , more geared up towards embedded folks . I have a positive float quantity with a fractional part (one decimal point) which occupies 4 bytes . Now i want to stuff it in to a two byte quantity like unsigned short and then reconvert it back to float . We can use any method like type casting or directly doing low level
9
30518
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
35937
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
4797
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.
9
5171
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
8968
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8787
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
9334
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
9259
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,...
0
9208
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8208
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6750
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...
1
3279
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
2744
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.