Announcement

Collapse
No announcement yet.

Decimal to signed Hex

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Decimal to signed Hex

    I need a quick and dirty way to convert decimal numbers
    into 32-bit signed and/or 32-bit unsigned hexadecimal.

    Are there any sources for this anywhere?

    Thanks in advance,
    Noble D. Bell


    ------------------
    Noble D. Bell - Owner
    Noble Software Publishing
    http://www.noblesoftware.com
    Noble D. Bell - Owner
    Noble Software Publishing
    http://www.noblesoftware.com

  • #2
    You mean other than the PowerBASIC HEX$ function?

    -- Eric

    ------------------
    Perfect Sync Development Tools
    Perfect Sync Web Site
    Contact Us: mailto:[email protected][email protected]</A>
    "Not my circus, not my monkeys."

    Comment


    • #3
      Originally posted by Eric Pearson:
      You mean other than the PowerBASIC HEX$ function?

      -- Eric

      Yes, I love the HEX$ function but I need this for a QuickBasic
      client of mine. Gee, I have two QuickBasic clients and I am
      trying get both of them to buy PB.

      Thx




      ------------------
      Noble D. Bell - Owner
      Noble Software Publishing
      http://www.noblesoftware.com
      Noble D. Bell - Owner
      Noble Software Publishing
      http://www.noblesoftware.com

      Comment


      • #4
        You mean other than the QuickBASIC HEX$ function? ...

        QB doesn't support DWORDs, of course. How were you planning to pass a DWORD to the
        HEX$ function?

        ------------------
        Tom Hanlin
        PowerBASIC Staff

        Comment


        • #5
          Noble - this is about as dirty as you can get, but it works.

          Code:
          FUNCTION DEC2HEX$(JTEM#)
            DO
              BYX = &H30 + (JTEM# AND &H0F)
              IF BYX > &H39 THEN BYX = BYX + &H07
              H$ = CHR$(BYX) + H$
              JTEM# = INT(JTEM# / 16)
            LOOP UNTIL JTEM# = 0
            FUNCTION = H$
          END FUNCTION
          Pass the value to be converted as a double-precision number, and you'll get back a hexadecimal string.

          ------------------


          [This message has been edited by Gary Akins (edited May 25, 2001).]

          Comment


          • #6
            This is what the client is trying to do:

            &hffffe500 - signed hex

            He wants to put this value into memory

            def seg = &he000

            offset: &h1164 to &h1167

            This relates to my earlier post about variable memory locations.
            I have the routines working to copy variables to and from memory
            locations.

            He is telling me that the values he is peek'ing at in the
            memory locations when placed in MSB order do not equal the
            decimal value that he enters from his keyboard to be stored.

            ie. He enters -6912 at the keyboard (&hffffe500). After you run
            the routines to place the contents to memory and then look
            back at them with peek you get 0,0,216,197.

            I don't believe that I/he is fully understanding the whole
            concept. When he hired me to do this project is was far less
            complex than it is shaping out to be (Prime example of
            specifications creep)!

            To my understanding: The number that he is typing in at the
            keyboard is not the number that he is seeing in the memory
            locations that the motor controller is looking at and that is
            what needs to be there.

            My memory routines that I finally settled on using work.

            a = number you want to store someplace
            n = length to transfer

            sm=varseg(a)
            os=varptr(a)
            for i=0 to n-1
            def seg = sm
            byte = peek(os+i)
            def seg = known-sm
            poke known-os+i,byte
            next i

            To retrieve value I use:

            def seg = known-sm
            b$=""
            for i=0 to n-1
            b$=b$+chr$(peek(known-os+i))
            next i

            result = cvd(b$) - for 4 bytes
            print result

            end


            Am I doing something wrong here?

            Thanks in advance,
            noble



            ------------------
            Noble D. Bell - Owner
            Noble Software Publishing
            http://www.noblesoftware.com
            Noble D. Bell - Owner
            Noble Software Publishing
            http://www.noblesoftware.com

            Comment


            • #7
              &hffffe500 - signed hex
              Later you use cvd (four-byte-string); but CVD operates on an eight-byte entity, not a four-byte entity.

              The question is, "what is signed hex?" That term has no meaning.

              Do the four bytes in memory represent a 32-bit signed integer in Intel Format?

              If so, it's just a LONG.

              If it's an unsigned 32-bit Integer in Intel format, the it's a (PB) DWORD.

              Since DWORD is not supported by MSQB, just take it as a LONG, and if it's let than zero, add 4,24x,xxx,xxx to it.

              (I forget the xxx's. It's x'100000000'). (2^31)

              Lastly, if you are working in Microsoft BASIC, your single- and doub-precision values might be in MBF format rather than IEEE format, so that's another consideration.

              I don't believe that I/he is fully understanding the whole concept
              The two of you and me, too!

              MCM

              Michael Mattias
              Tal Systems Inc. (retired)
              Racine WI USA
              [email protected]
              http://www.talsystems.com

              Comment


              • #8
                def seg = known-sm
                poke known-os+i,byte
                This looks very peculiar. What's it do?
                (I forget the xxx's. It's x'100000000'). (2^31)
                2^32, actually, although an exact solution is not going to help unless the problem
                becomes more clear...!

                ------------------
                Tom Hanlin
                PowerBASIC Staff

                Comment

                Working...
                X