|
-
May 30th, 2001, 12:35 PM
#1
Thread Starter
Frenzied Member
Hex?
How do you trasfer a hex value to a number?
-
May 30th, 2001, 12:58 PM
#2
Hyperactive Member
-
May 30th, 2001, 01:01 PM
#3
Frenzied Member
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
-
May 30th, 2001, 01:25 PM
#4
Frenzied Member
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
-
May 30th, 2001, 01:57 PM
#5
Thread Starter
Frenzied Member
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?
-
May 30th, 2001, 02:01 PM
#6
Hyperactive Member
-
May 30th, 2001, 02:09 PM
#7
Thread Starter
Frenzied Member
How do you do it manually to get the number?
-
May 30th, 2001, 02:11 PM
#8
Hyperactive Member
Type the "?VAL()" thing into the immediate window and hit enter. Press Ctrl+G to open the immediate window if it's not already.
-
May 30th, 2001, 02:45 PM
#9
Frenzied Member
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
-
May 30th, 2001, 02:51 PM
#10
Thread Starter
Frenzied Member
no.
How do I figure the number out on paper. Thats
all I really want.
-
May 30th, 2001, 02:56 PM
#11
Hyperactive Member
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()...
-
May 30th, 2001, 03:00 PM
#12
Code:
Print Val("&HFFFF")
-
May 30th, 2001, 03:08 PM
#13
Hyperactive Member
I stand corrected. Learn somethin new every day.
-
May 30th, 2001, 03:09 PM
#14
Frenzied Member
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.
-
May 30th, 2001, 03:13 PM
#15
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!!!
-
May 30th, 2001, 03:26 PM
#16
Hyperactive Member
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.
Code:
(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
So &H2E35 = 11,829
I hope that helps !!!
-
May 30th, 2001, 03:32 PM
#17
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.
I never said it didn't.
-
May 30th, 2001, 04:04 PM
#18
Hyperactive Member
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 -
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
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 * 1 = 1
0 * 2 = 0
0 * 4 = 0
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:
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
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:
8/4/2/1
1 1 0 0 = 1100 = 12
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
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 that we have converted the digits to binary, now line them up -
Code:
0010 1110 0011 0101
2 E 3 5
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:
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
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.)
-
May 30th, 2001, 04:52 PM
#19
Frenzied Member
Sorry Megatron, I didn't see jmcswain's post asking a question, now I see that yours is just an answer to his question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|