-
Incrementing Help
Hi Forum, I know this is quite a simple problem but I am having a hard time trying to figure it out.
How do I increment a counter variable in HEX?
Counter = Counter + 1 ' this is what I normally do to increment a variable
I assume that if I do something like this: that it will increment a variable in HEX
Counter = Counter + $01
Please correct me if I am wrong.
Srig
-
incrementing is incremeting... adding is adding.
there's no difference between a=a+1 against a=a+$1
BUT if you want to add 10 in hex... yep a=a+$10
only the modifier do a difference you can add hex, bin and decimal in the same formula too
a=a+%00000111+$10+128
-
Hi Srigopal
As value $01=01=%01,so what you write $01 or 01 is no difference.
Compiler only know bit ,byte,word(data type). No more Hex or Dec.
Mcus work only on binary.
Your output (eg.serout,lcd out)can be binary,ASCII,Decimal and Hex.
a.majid
-
Now I have another question! What about the result. Will that be in hex form or a decimal representation of the hex value.
for example lets say do the following
counter = counter + $01 will the result in counter variable now have the HEX representation. or will it have the decimal representation.
Also lets say I do the following
PORTB = 128 'which is 1000000 in binary.
this means that PORTB.7= 1 while the rest of the pins in portB = 0
if I am not mistaken this is the same thing as saying PORTB = $80.
So basically it does not matter if you are incrementing in HEX form or Decimal form because the output will eventually be converted back to binary form.
-
It doesn't matter how you "view" the number (hex, binary, or decimal), the value is still the same. So, adding one is the same, whether you add one "in hex" ($01) or "in binary" (%00000001) or "in decimal" (1). You still just have "a number".
One thing that might help clarify this for you is to use the Calculator in Windows (if you're using Windows). Using the "Scientific View" (View >> Scientific), type in a number, then change the radio buttons to Hex, or Bin, or Dec. The VALUE hasn't changed, just the way it's represented.