subtracting base16

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cryoburned
    New Member
    • Sep 2007
    • 23

    subtracting base16

    is there a way i can add and subtract base16 numbers?


    btw base16 is also known as hex.. but i dont want to convert FFFF into 65355 or w/e..

    i want to make F - 1 = E and the like.
  • Killer42
    Recognized Expert Expert
    • Oct 2006
    • 8429

    #2
    Originally posted by cryoburned
    is there a way i can add and subtract base16 numbers?
    Why can't you just do the subtraction normally, and display the result as hex? (base 16 numbers are generally referred to as "hexadecima l", or "hex" for short).

    For example...[CODE=vb]Debug.Print &HFFFF - &H1
    Debug.Print Hex$(&HFFFF - &H1)[/CODE]In VB6, at least, this will print...
    -2
    FFFE

    Comment

    • cryoburned
      New Member
      • Sep 2007
      • 23

      #3
      i couldnt find a direct way to just subtract them..

      but everytime i use the hex$ function, I get a compile error that tells me an array is expected..

      hexfinal = hex$(&HFFF - &H1)

      Comment

      • Killer42
        Recognized Expert Expert
        • Oct 2006
        • 8429

        #4
        Originally posted by cryoburned
        i couldnt find a direct way to just subtract them..

        but everytime i use the hex$ function, I get a compile error that tells me an array is expected..

        hexfinal = hex$(&HFFF - &H1)
        Hm... what version of VB are you using? Perhaps it doesn't include a Hex() or Hex$() function. I use VB6, and a lot of this stuff changed in later versions.

        Comment

        • cryoburned
          New Member
          • Sep 2007
          • 23

          #5
          VB6.

          6.0.8169

          but i dont think mine supports it because usually a code hint appears if you type

          anything and then parentheses.

          aka i type replace and as soon as i type the open parenth a code comment appears..

          that doesnt happen with hex or hex$.

          Comment

          • Killer42
            Recognized Expert Expert
            • Oct 2006
            • 8429

            #6
            What edition? As in "Eductional ", "Profession al" etc. There were some differences in what they provided, though I thought it was mainly in the area of extra ActiveX controls.

            In mine (same version number, I just checked) looking up Hex in the online help shows...

            Hex Function

            Returns a String representing the hexadecimal value of a number.

            Syntax

            Hex
            (number)

            The required number argument is any valid numeric expression or string expression.

            Remarks

            If number is not already a whole number, it is rounded to the nearest whole number before being evaluated.
            ...and so on.

            I can't think of any reason why it would be unavailable on your system. Of course you could write your own conversion function, it wouldn't be that tough.

            Comment

            • cryoburned
              New Member
              • Sep 2007
              • 23

              #7
              eh.. i think i found my problem...

              at the bottom of the about box:
              Version 8176 VBA: Retail version 6.0.8169 Forms3: 11.0.6550

              lol btw it says (number)? thats weird.. i would have thought it'd say integer..

              Comment

              • Killer42
                Recognized Expert Expert
                • Oct 2006
                • 8429

                #8
                Originally posted by cryoburned
                at the bottom of the about box:
                Version 8176 VBA: Retail version 6.0.8169 Forms3: 11.0.6550
                Um... what about it? Mine at home says pretty much the same. Not sure about the "Forms3" entry, but the other two, certainly. At work here the versions are different, but as far as I know, Hex$() function has "always" been available, probably since at least VB3.

                Originally posted by cryoburned
                lol btw it says (number)? thats weird.. i would have thought it'd say integer..
                Why? It doesn't have to be an integer. At least, not in the sense of an Integer data type. It will also accept Long, and probably any numeric type, though I seem to recall the doco mentioned rounding.

                If they used the term "integer" to simply mean a whole number, they'd just confuse people because of the Integer data type.

                Comment

                • cryoburned
                  New Member
                  • Sep 2007
                  • 23

                  #9
                  Originally posted by Killer42
                  Um... what about it? Mine at home says pretty much the same. Not sure about the "Forms3" entry, but the other two, certainly. At work here the versions are different, but as far as I know, Hex$() function has "always" been available, probably since at least VB3.

                  Why? It doesn't have to be an integer. At least, not in the sense of an Integer data type. It will also accept Long, and probably any numeric type, though I seem to recall the doco mentioned rounding.

                  If they used the term "integer" to simply mean a whole number, they'd just confuse people because of the Integer data type.
                  w00t so i got my problem figured out.. i had a global variable named "hex" was screwing with my code..

                  the reason i thought what i said in my other post was the problem is because i thought when it said vba it was my actual version, not the office vba support..

                  Comment

                  • cryoburned
                    New Member
                    • Sep 2007
                    • 23

                    #10
                    but unfortunantly, while it works.. when i add it ( i know this isn't to the subject of this thread.. ) instead of going back to FFFF from FEFF it becomes just FF. Is there a solution other than
                    Code:
                    If hexfinal = &HFFFF Then
                    Text1.Text = "FFFF"
                    End If
                    So that i can work around it to have all 4 digits?

                    Comment

                    • Killer42
                      Recognized Expert Expert
                      • Oct 2006
                      • 8429

                      #11
                      Originally posted by cryoburned
                      w00t so i got my problem figured out.. i had a global variable named "hex" was screwing with my code..

                      the reason i thought what i said in my other post was the problem is because i thought when it said vba it was my actual version, not the office vba support..
                      Ok. Well, glad to hear it's sorted, anyway.

                      Comment

                      • Killer42
                        Recognized Expert Expert
                        • Oct 2006
                        • 8429

                        #12
                        Originally posted by cryoburned
                        but unfortunantly, while it works.. when i add it ( i know this isn't to the subject of this thread.. ) instead of going back to FFFF from FEFF it becomes just FF.
                        Can you explain what you mean? If I do this in the immediate window...
                        ? Hex$(&hfffe + 1)
                        it shows the answer as "FFFF".

                        The fact that VB insists on working with signed numbers can be a pain, I must admit. For instance, &HFFFF converts to decimal -1 when in my opinion it should be 65536.

                        I expect it wraps later (after four bytes?) in .Net versions of VB.

                        Comment

                        • cryoburned
                          New Member
                          • Sep 2007
                          • 23

                          #13
                          i solved that problem too.. coding error...

                          but what about using the hex function to add and subtract variables?

                          hexfinal = hex(hexfinal + &H100)

                          but i get a type mismatch with this.

                          hexfinal = hexfinal + hex(&H100) gets results in the same thing.

                          Comment

                          • Killer42
                            Recognized Expert Expert
                            • Oct 2006
                            • 8429

                            #14
                            Originally posted by cryoburned
                            hexfinal = hex(hexfinal + &H100)

                            but i get a type mismatch with this.
                            hexfinal is a string, right? The Hex() function expects a number. This would work if you convert the string to numeric format. For example...
                            [CODE=vb]hexfinal = hex(Val("&H"& hexfinal) + &H100)[/CODE]It's usually simpler to store everything in numeric format for easy manipulation and just convert to hex where you need it for display or whatever.

                            Comment

                            • cryoburned
                              New Member
                              • Sep 2007
                              • 23

                              #15
                              Originally posted by Killer42
                              hexfinal is a string, right? The Hex() function expects a number. This would work if you convert the string to numeric format. For example...
                              [CODE=vb]hexfinal = hex(Val("&H"& hexfinal) + &H100)[/CODE]It's usually simpler to store everything in numeric format for easy manipulation and just convert to hex where you need it for display or whatever.
                              well thanks for that.. will try at home.. but no matter what i dimmed hexfinal as, there was always a problem.. integer long double variant and string...

                              i did try CInt() but it always every number always came out to zero..

                              expect for FFFF which came out as -1

                              Comment

                              Working...