How do you trasfer a hex value to a number?
Printable View
How do you trasfer a hex value to a number?
Use the Val() function:
Code:?val("&h10")
16
This will work if the hex number is not that big:
Code:Dim strNumber As String
Dim clngNewNum As Long
strNumber = Text1.Text
clngNewNum = CLng(Val("&H" & strNumber))
Text2.Text = clngNewNum
Actually, this is more reliable, there is something wrong with that other function:
Code:Dim pintCount As Integer
Dim pintLen As Integer
Dim str As String
Dim str2 As String
Dim lngTemp As Long
Dim lngNewNum As Long
str = Trim(Text1.Text)
pintLen = Len(str)
For pintCount = 1 To pintLen
str2 = Left$(str, 1)
Select Case str2
Case 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "A", "a", "b", "B", "c", "C", "d", "D", "e", "E", "f", "F"
Select Case str2
Case "A", "a"
lngTemp = 10
Case "B", "b"
lngTemp = 11
Case "C", "c"
lngTemp = 12
Case "D", "d"
lngTemp = 13
Case "E", "e"
lngTemp = 14
Case "F", "f"
lngTemp = 15
Case Else
lngTemp = Val(str2)
End Select
lngNewNum = lngNewNum + lngTemp * 16 ^ (pintLen - 1)
Case Else
MsgBox ("Please enter valid hex number")
Exit Sub
End Select
str = Right$(str, Len(str) - 1)
pintLen = pintLen - 1
Next pintCount
Text2.Text = lngNewNum
actually I just wanted a way to figure it out in my head hehe =-)
so
a=10
b=11
c=12
d=13
e=14
f=15
There is no E?
so &E12 = 26?
Code:?Val(&hE12)
3602
How do you do it manually to get the number?
Type the "?VAL()" thing into the immediate window and hit enter. Press Ctrl+G to open the immediate window if it's not already.
The problem with the Val() function is that if the hex number is too big, it will return a negative number.
A quick hex to decimal tutorial:
You start from the right and each place equals 16 to whatever power starting with zero. And then you add them all together. For example:
A2B3 = 10*16^3 + 2*16^2 + 11*16^1 + 3*16^0
= 40,960 + 512 + 176 + 3
= 41,651
Hope this helps;)
no.
How do I figure the number out on paper. Thats
all I really want.
No, I'm pretty sure that's not a problem with Val(). I believe it's a problem with the CLng() function you put around the Val().
Give me an example of a Hex number that comes back negative using Val()...
Code:Print Val("&HFFFF")
I stand corrected. Learn somethin new every day.
Evan, did you look at my last post, that is how you would do it on paper.
Megatron and jmcswain, put ?val(&hffff) in the Itermediate window and what do you get? -1 that is not correct, it should equal 65535. Also, I removed the clng from it and it still works the same.
Evan, the "&H" prefix is always the same that is what tells VB that it is a Hex value in the first place!
Each Hex character represents 4 Binary bits, so "&HE" is equal to 1110 in binary and thus 14 in decimal. Hex is a base 16 number system ((log 16)/(log 2)) = 4, decimal as you well know (all humans use it in their heads!) is base 10, and binary is base 2.
Octal is base 8 and therefore each Octal character is worth 3 binary bits ((log 8)/(log 2)) = 3.
Practice using binary, Octal, Hex and decimal and you will soon be able to convert between them in your head!!!
It's not easy to calculate Hex on paper (without a calculator) because everything is in powers of 16, which is hard for most people to do casually, but it goes like this:
To convert:
&H2E35
Each digit is multiplied by 16 raised to the power of the digits place (starting with zero), then all of the results are added together.
So &H2E35 = 11,829Code:(5 * 16^0) = 5 * 1 = 5
(3 * 16^1) = 3 * 16 = 48
(14 * 16^2) = 14 * (16 * 16) = 3584
(2 * 16^3) = 2 * (16 * 16 * 16) = 8192
---------------------------------------------
Total = 11829
I hope that helps !!!
I never said it didn't.Quote:
Originally posted by seoptimizer2001
Megatron and jmcswain, put ?val(&hffff) in the Itermediate window and what do you get? -1 that is not correct, it should equal 65535. Also, I removed the clng from it and it still works the same.
Its sometimes actually easier to conver HEX to Binary, then do the math on paper - this way you are only using powers of 2.
Each digit in hex represents 4 digits in binary:
In binary, each digit is multiplied by 2 raised to the power of the digits place starting with zero.
If you are give a binary number like 1001 -
So binary 1001 = 9, and because you will only be multiplying by 1 or 0, you will then either be adding 2 raised to that number or not, so:Code:(1 * 2^0) = (1 * 1) = 1
(0 * 2^1) = (0 * 2) = 0
(0 * 2^2) = (0 * 4) = 0
(1 * 2^3) = (1 * 8) = 8
-----------------------
Total = 9
This means that if you keep doubling the number that each digit represents, it makes it much easier to deal with larger binary numbers:Code:1 * 1 = 1
0 * 2 = 0
0 * 4 = 0
1 * 8 = 8
----------
Total = 9
Now if you can keep track of the 8/4/2/1 combination - you can then more easily convert a number 0-15 to a binary number 0000-1111. for example, what makes up 12 when using 8/4/2/1 ??? - (8+4) = 12 so in binary:Code:11011010 =
0 * 1 = 0
1 * 2 = 2
0 * 4 = 0
1 * 8 = 0
1 * 16 = 16
0 * 32 = 0
1 * 64 = 64
1 * 128 = 128
---------------
Total = 210
Here is where the Hex comes in - each digit in hex is described as a number between 0 and 15 (0-F), so if we look at the digits in the original hex number &H2E35, we should be able to convert then to 4 groups of 4 binary number:Code:8/4/2/1
1 1 0 0 = 1100 = 12
Now that we have converted the digits to binary, now line them up -Code:8/4/2/1
2 = 0 0 1 0 (2)
E = (14) = 1 1 1 0 (8 + 4 + 2)
3 = 0 0 1 1 (2 + 1)
5 = 0 1 0 1 (4 + 1)
Now you have a larger binary number (0010111000110101) - if you can double the number 2, enough times to cover all of the digits, then the math becomes much easier:Code:0010 1110 0011 0101
2 E 3 5
Anyway - this may or may not be easier for you. I have memorized the powers of 2 over time (1-2-4-8-16-32-etc.), and that seams to be much easier then remembering the powers of 16 (16-256-4096-65536-etc.)Code:1 * 1 = 1
0 * 2 = 0
1 * 4 = 4
0 * 8 = 0
1 * 16 = 16
1 * 32 = 32
0 * 64 = 0
0 * 128 = 0
0 * 256 = 0
1 * 512 = 512
1 * 1024 = 1024
1 * 2048 = 2048
0 * 4096 = 0
1 * 8192 = 8192
0 * 16384 = 0
0 * 32768 = 0
---------------------
Total = 11829
:) :) :) :) :) :)
Sorry Megatron, I didn't see jmcswain's post asking a question, now I see that yours is just an answer to his question.;)