converting float to individual bytes

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • TK

    converting float to individual bytes

    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.
  • Evertjan.

    #2
    Re: converting float to individual bytes

    TK wrote on 03 jun 2005 in comp.lang.javas cript:
    [color=blue]
    > 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.
    >[/color]

    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)

    Comment

    • TK

      #3
      Re: converting float to individual bytes

      Evertjan. wrote:[color=blue]
      > TK wrote on 03 jun 2005 in comp.lang.javas cript:
      >
      >[color=green]
      >>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.
      >>[/color]
      >
      >
      > 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.
      >[/color]

      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.

      Comment

      • Evertjan.

        #4
        Re: converting float to individual bytes

        TK wrote on 03 jun 2005 in comp.lang.javas cript:
        [color=blue]
        > Evertjan. wrote:[color=green]
        >> TK wrote on 03 jun 2005 in comp.lang.javas cript:
        >>
        >>[color=darkred]
        >>>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.
        >>>[/color]
        >>
        >>
        >> 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.
        >>[/color]
        >
        > that appears to do a lot more than I need. Is there a simpler way?[/color]

        But that is not what you asked!
        [color=blue]
        > 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.[/color]

        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)

        Comment

        • Joakim Braun

          #5
          Re: converting float to individual bytes

          "TK" <tok135@hotmail .com> skrev i meddelandet
          news:11a16ssabn ave7d@corp.supe rnews.com...
          <snip>[color=blue]
          > 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.[/color]

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

          --
          Joakim Braun


          Comment

          • Michael Winter

            #6
            Re: converting float to individual bytes

            On 03/06/2005 18:20, TK wrote:
            [color=blue]
            > 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.[/color]

            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.
            [color=blue]
            > I've tried using bitwise operators, but they seem to convert the value
            > into an integer first[/color]

            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.
            [color=blue]
            > 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.[/color]

            Only base-10 representations are required to provide a floating-point
            component when converting to a string. All other representations are
            implementation dependent.
            [color=blue]
            > any help would be much appreciated.[/color]

            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.

            Comment

            • TK

              #7
              Re: converting float to individual bytes

              Michael Winter wrote:[color=blue]
              > 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.[/color]

              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.

              Comment

              • TK

                #8
                Re: converting float to individual bytes

                >>All I need is to be able to input a value like 3.2 on screen, and[color=blue][color=green]
                >>display each byte seperatly as 0x40 0x4C 0xCC 0xCC.[/color]
                >
                >
                > 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?[/color]

                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.

                Comment

                • Dr John Stockton

                  #9
                  Re: converting float to individual bytes

                  JRS: In article <11a149ui4ca96d 4@corp.supernew s.com>, dated Fri, 3 Jun
                  2005 11:20:00, seen in news:comp.lang. javascript, TK
                  <tok135@hotmail .com> posted :[color=blue]
                  >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.[/color]

                  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.

                  Comment

                  • Evertjan.

                    #10
                    Re: converting float to individual bytes

                    TK wrote on 03 jun 2005 in comp.lang.javas cript:[color=blue][color=green][color=darkred]
                    >>>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.[/color]
                    >>
                    >> 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?[/color]
                    >
                    > 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.[/color]

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

                    Is that a temporal "first" or a "spacial" one?
                    [color=blue]
                    > 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.[/color]

                    So it is a serial device?

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

                    Comment

                    • Dr John Stockton

                      #11
                      Re: converting float to individual bytes

                      JRS: In article <x21oe.68516$Of 5.39662@nntpser ver.swip.net>, dated Fri,
                      3 Jun 2005 20:10:15, seen in news:comp.lang. javascript, Joakim Braun
                      <joakim.braun@j fbraun.removeth is.com> posted :[color=blue]
                      >"TK" <tok135@hotmail .com> skrev i meddelandet
                      >news:11a16ssab nave7d@corp.sup ernews.com...
                      ><snip>[color=green]
                      >> 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.[/color]
                      >
                      >What if the native byte order is different on the machine?
                      >What if float/fixed implementations vary on different platforms?[/color]

                      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.scriptin g.* 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.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.

                      Comment

                      • TK

                        #12
                        Re: converting float to individual bytes

                        Evertjan. wrote:[color=blue]
                        > TK wrote on 03 jun 2005 in comp.lang.javas cript:
                        >[color=green][color=darkred]
                        >>>>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?[/color]
                        >>
                        >>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.[/color]
                        >
                        >
                        > Even "the low byte stored first" has to be defined.
                        >
                        > Is that a temporal "first" or a "spacial" one?
                        >
                        >[color=green]
                        >>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.[/color]
                        >
                        >
                        > So it is a serial device?
                        >[/color]
                        spatially, it had to be stored first. I have specific register
                        addresses that I need to store each byte into for it to have meaning on
                        the device.

                        it's not serial, it has a built in either net card and very limited web
                        server.

                        Comment

                        • TK

                          #13
                          Re: converting float to individual bytes

                          TK wrote:[color=blue]
                          > Evertjan. wrote:
                          >[color=green]
                          >> TK wrote on 03 jun 2005 in comp.lang.javas cript:
                          >>[color=darkred]
                          >>>>> 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.[/color]
                          >>
                          >>
                          >>
                          >> Even "the low byte stored first" has to be defined.
                          >>
                          >> Is that a temporal "first" or a "spacial" one?
                          >>
                          >>[color=darkred]
                          >>> 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.[/color]
                          >>
                          >>
                          >>
                          >> So it is a serial device?[/color]
                          >
                          > spatially, it had to be stored first. I have specific register
                          > addresses that I need to store each byte into for it to have meaning on
                          > the device.
                          >
                          > it's not serial, it has a built in either net card and very limited web
                          > server.[/color]
                          wow, I don't know how I mis-typed that last post that badly. It should
                          have said "built in ethernet card".

                          Comment

                          • Evertjan.

                            #14
                            Re: converting float to individual bytes

                            TK wrote on 06 jun 2005 in comp.lang.javas cript:
                            [color=blue][color=green][color=darkred]
                            >>> So it is a serial device?[/color]
                            >>
                            >> spatially, it had to be stored first. I have specific register
                            >> addresses that I need to store each byte into for it to have meaning
                            >> on the device.
                            >>
                            >> it's not serial, it has a built in either net card and very limited
                            >> web server.[/color]
                            > wow, I don't know how I mis-typed that last post that badly. It
                            > should have said "built in ethernet card".
                            >[/color]

                            Even so, if you are storing a word [16 bits] in ram memory, and if you
                            want to store the lower byte in the lower address of two consequtive
                            byte addresses, you are still free to store that byte first or last.

                            Only in serial devicese "first" has meaning, imho.

                            If, using javascript, you want to store the number in a bytestring, I
                            can see you could have to exchange the byses of a 16 bit
                            "character"-to-be-stored.

                            Perhaps you could use >>> [or is this jscript-only?]:

                            Unsigned Right Shift Operator (>>>)

                            Right shifts the bits of an expression, without maintaining sign.

                            result = expression1 >>> expression2

                            The >>> operator shifts the bits of expression1 right by the number of
                            bits specified in expression2. Zeroes are filled in from the left.
                            Digits shifted off the right are discarded. For example:

                            var temp
                            temp = -14 >>> 2
                            The variable temp has a value of 1073741820 as -14 (11111111 11111111
                            11111111 11110010 in binary) shifted right two bits equals 1073741820
                            (00111111 11111111 11111111 11111100 in binary).



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

                            Comment

                            • TK

                              #15
                              Re: converting float to individual bytes

                              > Even so, if you are storing a word [16 bits] in ram memory, and if you[color=blue]
                              > want to store the lower byte in the lower address of two consequtive
                              > byte addresses, you are still free to store that byte first or last.
                              >
                              > Only in serial devicese "first" has meaning, imho.
                              >
                              > If, using javascript, you want to store the number in a bytestring, I
                              > can see you could have to exchange the byses of a 16 bit
                              > "character"-to-be-stored.
                              >
                              > Perhaps you could use >>> [or is this jscript-only?]:
                              >
                              > Unsigned Right Shift Operator (>>>)
                              >
                              > Right shifts the bits of an expression, without maintaining sign.
                              >
                              > result = expression1 >>> expression2
                              >
                              > The >>> operator shifts the bits of expression1 right by the number of
                              > bits specified in expression2. Zeroes are filled in from the left.
                              > Digits shifted off the right are discarded. For example:
                              >
                              > var temp
                              > temp = -14 >>> 2
                              > The variable temp has a value of 1073741820 as -14 (11111111 11111111
                              > 11111111 11110010 in binary) shifted right two bits equals 1073741820
                              > (00111111 11111111 11111111 11111100 in binary).
                              >
                              >
                              >[/color]
                              It isn't a serial device, but the reasoning behind the byte order is
                              basically the same. The bytes are stored in memory to build a command,
                              much like a modbus command is built to be sent in serial communications.

                              I attempted the bit-shifting idea, but it was pointed out to me later
                              that the value is converted to a 32-bit integer before the bit-shifting
                              begins. I need to be able to use decimal values.

                              It looks like I have to follow the first example you had sent me, and
                              grind out the bytes the hard way.

                              Comment

                              Working...