HighDots Forums  

converting float to individual bytes

Javascript JavaScript language (comp.lang.javascript)


Discuss converting float to individual bytes in the Javascript forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
TK
 
Posts: n/a

Default converting float to individual bytes - 06-03-2005 , 01:20 PM






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.

Reply With Quote
  #2  
Old   
Evertjan.
 
Posts: n/a

Default Re: converting float to individual bytes - 06-03-2005 , 01:32 PM






TK wrote on 03 jun 2005 in comp.lang.javascript:

Quote:
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.edu/courses/cs341/IEEE-754hex32.html>

and all will be revealed to you.

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



Reply With Quote
  #3  
Old   
TK
 
Posts: n/a

Default Re: converting float to individual bytes - 06-03-2005 , 02:04 PM



Evertjan. wrote:
Quote:
TK wrote on 03 jun 2005 in comp.lang.javascript:


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.edu/courses/cs3...-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.



Reply With Quote
  #4  
Old   
Evertjan.
 
Posts: n/a

Default Re: converting float to individual bytes - 06-03-2005 , 02:09 PM



TK wrote on 03 jun 2005 in comp.lang.javascript:

Quote:
Evertjan. wrote:
TK wrote on 03 jun 2005 in comp.lang.javascript:


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.edu/courses/cs3...-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!

Quote:
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)



Reply With Quote
  #5  
Old   
Joakim Braun
 
Posts: n/a

Default Re: converting float to individual bytes - 06-03-2005 , 02:10 PM



"TK" <tok135 (AT) hotmail (DOT) com> skrev i meddelandet
news:11a16ssabnave7d (AT) corp (DOT) supernews.com...
<snip>
Quote:
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




Reply With Quote
  #6  
Old   
Michael Winter
 
Posts: n/a

Default Re: converting float to individual bytes - 06-03-2005 , 02:10 PM



On 03/06/2005 18:20, TK wrote:

Quote:
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.

Quote:
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.

Quote:
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.

Quote:
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.


Reply With Quote
  #7  
Old   
TK
 
Posts: n/a

Default Re: converting float to individual bytes - 06-03-2005 , 04:07 PM



Michael Winter wrote:
Quote:
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.


Reply With Quote
  #8  
Old   
TK
 
Posts: n/a

Default Re: converting float to individual bytes - 06-03-2005 , 04:17 PM



Quote:
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.


Reply With Quote
  #9  
Old   
Dr John Stockton
 
Posts: n/a

Default Re: converting float to individual bytes - 06-03-2005 , 04:46 PM



JRS: In article <11a149ui4ca96d4 (AT) corp (DOT) supernews.com>, dated Fri, 3 Jun
2005 11:20:00, seen in news:comp.lang.javascript, TK
<tok135 (AT) hotmail (DOT) com> posted :
Quote:
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(radix) 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.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.


Reply With Quote
  #10  
Old   
Dr John Stockton
 
Posts: n/a

Default Re: converting float to individual bytes - 06-04-2005 , 10:55 AM



JRS: In article <x21oe.68516$Of5.39662 (AT) nntpserver (DOT) swip.net>, dated Fri,
3 Jun 2005 20:10:15, seen in news:comp.lang.javascript, Joakim Braun
<joakim.braun (AT) jfbraun (DOT) removethis.com> posted :
Quote:
"TK" <tok135 (AT) hotmail (DOT) com> skrev i meddelandet
news:11a16ssabnave7d (AT) corp (DOT) supernews.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?
The ECMA standard requires type Number to be an IEEE Double, and integer
to be signed 32-bit (there is at least one unsigned operator).

ECMA-262 is linked from the newsgroup FAQ; read both.

<FAQENTRY> The link in 2.6 should be annotated (PDF) if it is PDF;
otherwise, I suspect it is not the standard but an HTML page linking to
it. </FAQENTRY>

From what the OP has posted since the first article, it appears that his
is not a Web application, but is only to be used locally.

He should therefore specify his system(s),

One of the news:microsoft.public.scripting.* newsgroups might be better;
they know more there about those things that can be done off-Web.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.