By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,953 Members | 1,143 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,953 IT Pros & Developers. It's quick & easy.

subtracting base16

P: 23
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.
Sep 22 '07 #1
Share this Question
Share on Google+
27 Replies


Expert 5K+
P: 8,434
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 "hexadecimal", or "hex" for short).

For example...
Expand|Select|Wrap|Line Numbers
  1. Debug.Print &HFFFF - &H1
  2. Debug.Print Hex$(&HFFFF - &H1)
In VB6, at least, this will print...
-2
FFFE
Sep 22 '07 #2

P: 23
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)
Sep 22 '07 #3

Expert 5K+
P: 8,434
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.
Sep 23 '07 #4

P: 23
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$.
Sep 23 '07 #5

Expert 5K+
P: 8,434
What edition? As in "Eductional", "Professional" 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.
Sep 23 '07 #6

P: 23
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..
Sep 23 '07 #7

Expert 5K+
P: 8,434
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.

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.
Sep 23 '07 #8

P: 23
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..
Sep 24 '07 #9

P: 23
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
Expand|Select|Wrap|Line Numbers
  1. If hexfinal = &HFFFF Then
  2. Text1.Text = "FFFF"
  3. End If
  4.  
So that i can work around it to have all 4 digits?
Sep 24 '07 #10

Expert 5K+
P: 8,434
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.
Sep 24 '07 #11

Expert 5K+
P: 8,434
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.
Sep 24 '07 #12

P: 23
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.
Sep 24 '07 #13

Expert 5K+
P: 8,434
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...
Expand|Select|Wrap|Line Numbers
  1. hexfinal = hex(Val("&H"& hexfinal) + &H100)
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.
Sep 25 '07 #14

P: 23
hexfinal is a string, right? The Hex() function expects a number. This would work if you convert the string to numeric format. For example...
Expand|Select|Wrap|Line Numbers
  1. hexfinal = hex(Val("&H"& hexfinal) + &H100)
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
Sep 25 '07 #15

P: 23
ok your idea to get the value of the string.. sort of works...

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Load()
  2.   hexfinal = "&H0000FFFF"
  3. End Sub
  4.  
  5. Private Sub L2_Click()
  6.   If l2on = False Then
  7.     L2.ForeColor = &HFFFFFF
  8.     l2on = True
  9.     hexfinal = Hex(Val(hexfinal) - &HFFFF0100)
  10.     Text1.Text = "0000" & hexfinal
  11.     Text2.Text = "0010" & hexfinal
  12.   ElseIf l2on = True Then
  13.     L2.ForeColor = &H808080
  14.     l2on = False
  15.     hexfinal = Hex(Val(hexfinal) + Val("&H00000100"))
  16.     Text1.Text = hexfinal
  17.     Text2.Text = hexfinal
  18.   End If
  19. End Sub
  20.  
that was my last attemp..

the subtraction works.. i get FEFF like i wanted..

put the addition.. always results as the number 100 and not FFFF
Sep 25 '07 #16

Expert 5K+
P: 8,434
I'd like to try out your code, but I need you to fill in a couple of gaps. I assume Text1 and Text2 are textboxes, and hexfinal is obviously a string. But what are L2 and l2on?
Sep 26 '07 #17

Expert 5K+
P: 8,434
I think I should point out, though, that the result of VAL("FEFF") is zero, while VAL("&HFEFF") is -257 (or if you could get VB to treat it as unsigned, 65,279).
Sep 26 '07 #18

P: 23
I'd like to try out your code, but I need you to fill in a couple of gaps. I assume Text1 and Text2 are textboxes, and hexfinal is obviously a string. But what are L2 and l2on?
L2 is a button, l2on is boolean
Sep 26 '07 #19

P: 23
I think I should point out, though, that the result of VAL("FEFF") is zero, while VAL("&HFEFF") is -257 (or if you could get VB to treat it as unsigned, 65,279).
that did it.

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Load()
  2. hexfinal = "0000FFFF"
  3. End Sub
  4. Private Sub L2_Click()
  5. If l2on = False Then
  6. L2.ForeColor = &HFFFFFF
  7. l2on = True
  8. hexfinal = Hex(Val("&H" & hexfinal) - &HFFFF0100)
  9. Text1.Text = "0000" & hexfinal
  10. Text2.Text = "0010" & hexfinal
  11. ElseIf l2on = True Then
  12. L2.ForeColor = &H808080
  13. l2on = False
  14. hexfinal = Hex(Val(("&H" & hexfinal) + &H100))
  15. Text1.Text = "0000" & hexfinal
  16. Text2.Text = "0010" & hexfinal
  17. End If
  18. End Sub
  19.  
works great now
Sep 26 '07 #20

P: 23
hexfinal = Hex(Val(("&H" & hexfinal) + &H8000))
I believe you have too many parentheses. I think this should be...

Old version:
hexfinal = Hex(Val(("&H" & hexfinal) + &H8000))
New version:
hexfinal = Hex(Val("&H" & hexfinal) + &H8000)
Sep 26 '07 #21

Expert 5K+
P: 8,434
Sorry, my goof. I think I must have hit the Edit button instead of Reply. Damn! :(
Sep 26 '07 #22

P: 23
Sorry, my goof. I think I must have hit the Edit button instead of Reply. Damn! :(
lo its cool.. but im still have the problem with 8000.. it goes to 7FFF in subtraction, but jumps to FFFFFFFF is addition.
Sep 26 '07 #23

Expert 5K+
P: 8,434
lo its cool.. but im still have the problem with 8000.. it goes to 7FFF in subtraction, but jumps to FFFFFFFF is addition.
Can you show us the exact code again? I think I zapped your last version when I did the accidental edit.
Sep 26 '07 #24

QVeen72
Expert 100+
P: 1,445
lo its cool.. but im still have the problem with 8000.. it goes to 7FFF in subtraction, but jumps to FFFFFFFF is addition.
Hi,

Late into this thread,
Actually &H8000 is equivalent to 32768
but if you use Val function or CDec Function, they Start Showing Negative Value.
Val("&H8000") = -32768
Val("&H8001") = -32767 And so on.
You can also use CLng, but again to a certain Limit ..

Better Idea will be to use CDbl Function..

CDbl("&H8000") = 32768
CDbl("&H8001") = 32769


Regards
Veena
Sep 27 '07 #25

P: 23
Hi,

Late into this thread,
Actually &H8000 is equivalent to 32768
but if u use Val function or CDec Function, they Start Showing Negative Value.
Val("&H8000") = -32768
Val("&H8001") = -32767 And So On..
u can also use CLng, but again to a certain Limit ..

Better Idea will be to use CDbl Function..

CDbl("&H8000") = 32768
CDbl("&H8001") = 32769


Regards
Veena
that doesn't make a difference for this project.. but i got it.. i dont know why i had this problem but... now

Expand|Select|Wrap|Line Numbers
  1. hexfinal = Hex(Val((("&H" & hexfinal) + &H8000) - &HFFFF0000))
  2.  
gets me FFFF like i wanted..
Sep 27 '07 #26

Expert 5K+
P: 8,434
hexfinal = Hex(Val((("&H" & hexfinal) + &H8000) - &HFFFF0000))
Hm... still seems as though you have too many parentheses there. But I guess if it works, it works. :)

Congratulations!
Sep 27 '07 #27

Expert 5K+
P: 8,434
... Better Idea will be to use CDbl Function ...
Thanks Veena, that's useful to know. I had no idea CDbl would produce a different result.
Sep 27 '07 #28

Post your reply

Sign in to post your reply or Sign up for a free account.