Announcement

Collapse
No announcement yet.

Converting Hex to numbers

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

  • Converting Hex to numbers

    Hi All

    I'm confused on how to convert Hex to numbers in PB, I saw two different forum posts and they give different instructions

    in one post https://forum.powerbasic.com/forum/u...241#post528241
    it says to use the following formula

    Code:
     
     a$ = "ff3a21"  a&=val("&h"+a$)  'get the numercal value in a&


    while an older post https://forum.powerbasic.com/forum/u...2797#post62797

    said that it need to add a leading zero as shown below

    Code:
    A$ = "FFFF"
    PRINT VAL$("&H" + A$)
    PRINT VAL$("&H0" + A$)
    
    Results:
    -1
    65535
    
    Also note that VAL() is capable of working with binary and octal strings too:
    A$ = "1010101"
    PRINT VAL("&B0" + A$)
    A$ = "76543"
    PRINT VAL("&O" + A$)

    So which one should I follow ?



  • #2
    A$="FFFF" denotes either an integer(range-32,768 to 32,767) or a word (range 0 to 65535).
    PRINT VAL$("&H" + A$) gives you the integer result -1
    PRINT VAL$("&H0" + A$) gives you the WORD result 65535
    Rod
    I want not 'not', not Knot, not Knott, not Nott, not knot, not naught, not nought, but aught.

    Comment


    • #3
      Whether you start your Hex value with a 0 or not depends on whether you want the value returned as a signed (INT, LONG,) or unsigned (WORD, DWORD) number..



      VAL Function:
      If the string_expression contains a leading zero, the result is returned as an unsigned value; otherwise, a signed value is returned.

      Comment


      • #4
        It seems that if your hex string is for 4-byte signed values, you have to do something like this to be sure VAL will return the correct value:

        If len(hx$) = 4 then hx$ = "0" + hx$
        print val("&h" + hx$)

        For 8-byte signed values, change "= 4" to "= 8"



        Comment


        • #5
          Originally posted by Mark Bullivant View Post
          It seems that if your hex string is for 4-byte signed values, you have to do something like this to be sure VAL will return the correct value:

          If len(hx$) = 4 then hx$ = "0" + hx$
          print val("&h" + hx$)

          For 8-byte signed values, change "= 4" to "= 8"
          If your hex string is for 4-byte signed values, you do NOT want to prepend a "0", that will return an unsigned value.
          STR$(VAL("&H" & "CCCC")) = -13108 (signed - INTEGER)
          STR$(VAL("&H" & "0CCCC")) = 52428 (unsigned - DWORD)

          Note, this only makes a difference if the first Hex character is greater than 7!



          Comment


          • #6
            Code:
            'Suspected bug.
            '
            'I was testing various combinations of target type, highest bit set,
            'with/without prefix zero on hex and Type suffix (%, &, ??, ???) on hex.
            'Using PBWin 10.04 and PBCC 6.04, VAL() gives wrong result for DWORD when most
            'significant bit it not set. WORD and LONG work as expected.
            'Prefix zero is work around for this bug.
            #compile exe
            #dim all
            
            function pbmain () as long
              local Var1, Var2 as dword
            
              Var1 = val("&hCCCC")
              Var2 = val("&h0CCCC")
            
              ? str$(Var1) + " " + "(My PC shows 4294954188)" + $crlf + _
                str$(Var2) + "         " + "52428 is correct"
              'waitkey$ 'uncomment for PBCC
            end function
            (added - for 32 bit types it is bit 31 that marks negative for a LONG. In this case bit 15 is the highest set bit.
            52428 is correct for DWORD and LONG)
            Dale

            Comment


            • #7
              Thanks All
              will need to add prefix 0 as we use PB win 10

              Comment


              • #8
                Stuart, in the context of long integers, VAL will return negative values for hex strings "&H80000000" through "&HFFFFFFFF", as expected, but will also return negative values for hex strings "&H8000" through "&HFFFF".

                Comment


                • #9
                  Mark,

                  There are no negative numbers like that. Bit 31 is ALWAYS set for negatives (in LONGs)((bit 15 in INTEGERS) Check like Wikipedia on Two's Compliment'

                  Cheers,
                  Dale

                  Comment


                  • #10
                    The range &H80000000 - &HFFFFFFFF are all the possible values a 32-bit integer can have where bit 31 is set.

                    &H80000000 = &B1000000000000000 = -2147483648 (dec) in 2's complement.

                    &HFFFFFFFF = &B1111111111111111 = -1 in 2's complement.

                    And a similar case applies to &H8000 - &HFFFF for 16-bit integers.

                    No?

                    Comment


                    • #11
                      Yes, but in post 8 you only mentioned LONGs, not INTEGERs; and
                      "&H8000" through "&HFFFF should not be negative in a LONG. (Just a typo?)

                      Cheers,
                      Dale

                      Comment


                      • #12
                        I have ran into the problem Dale mentions it also applies to constants.This is another work around for what it is worth.


                        Var1 = VAL("&hCCCC???")
                        Var2 = VAL("&h0CCCC???")

                        Comment


                        • #13
                          In the earlier post, I was trying to describe the problem with VAL in the context of long or 32-bit signed integers. So, yes, in that context VAL should return negative values for hex strings from "&h80000000" to "&hFFFFFFFF", but VAL (not me) also returns negative values for "&h8000" to "&hFFFF". Anyway, if you use Bill's solution the problem goes away.

                          Comment


                          • #14
                            So, using Bill's idea:

                            INPUT "Enter long value in hex "; hx$
                            PRINT VAL("&h"+hx$+"&")

                            Comment


                            • #15
                              From post 13
                              also returns negative values for "&h8000" to "&hFFFF"
                              A line from Help for VAL()
                              i& = VAL("&HF5F3") ' Hex, returns -2573 (signed)
                              Mark, It complies with Help, though I don't agree that it should. There are four leading "F"s missing (in this case), while putting a "0" to mark as unsigned standing for missing leading "0"s, which we commonly do.

                              Note - I just tried LongVar = val("&h800") and it resulted in positive. Why should leading shown (in hex digit) "1" in bit 11 be a positive in a 32 bit var, but a leading shown (in hex digit) "1" in bit 15 of a 32 bit var gives negative.

                              Thankyou Mark.

                              Adam, IMO val() needs to be consistent regarding hex. And Help needs to be more specific. (repeat, IMO; your problem to decide if you agree with me.)
                              Dale

                              Comment

                              Working...
                              X