by Xei (1 Submission)
Category: Math/Dates
Compatability: Visual Basic 4.0 (32-bit)
Difficulty: Unknown Difficulty
Originally Published: Fri 20th July 2001
Date Added: Mon 8th February 2021
Rating: (1 Votes)
This will allow a user to change any number less than 1 Trillion into Hex and Hex Back To Decimal. I am working on an application to support
'* This project requires 1 Textbox and 2 Command Buttons. *
'* Before we get right into the source code I'll explain how Hex really works. Its sometimes hard*
'* to find a good example of Hexadecimal. Hexadecimal is a Base 16 type. *
'* 0 - 9 = 0 - 9 *
'* 10 = A *
'* 11 = B *
'* 12 = C *
'* 13 = D *
'* 14 = E *
'* 15 = F *
'* Lets say that we want to change the number 12504 into Hexadecimal: *
'* So we have to find a number that is just less than 12504, we will have to go up by 16x each *
'* time. So, 16 * 16 = 256, 16 * 256 = 4096, 16 * 4096 = 65536. Now if our number was larger than*
'* 65536 then we may use 65536, but 4096 is currently our closest lower number. *
'* 12504 / 4096 = 3.05 (never round up in Hex, always round down, you will never have a number *
'* that is devided more than 15x if your doing right). So 3 in decimal is = 3 In Hex, therefore *
'* Answer = 3. Now 12504 - (4096 * 3) = 216. Our next number down the list from 4096 is 256, but *
'* our number is less than 256 so Answer = 30. Now we move on to the next number on the list *
'* which is 16(our final number), so 216 / 16 = 13.5. 13 in decimal is = D in Hex, so *
'* Answer = 30D. 216 - (16*13) = 8. Now that our remainder is less than 16 we just simply add *
'* the 8 to the answer. So our final Answer = 30D8 in Hexadecimal. How hard was that? If you *
'* are having trouble understanding just read it a few more times and try some more On your own *
'* using this program to check your work. *
'* An alternative to using this program is to use the Hex$ command. But I would suggest using *
'* this program because it can transfer it back from Hex to Decimal and you also learn how hex *
'* works. *
'* Edit this code to whatever pleases you. *
'* Coded by: Xei *
Private Function DecimalToHex(Number As Long) As String
Dim DevideTree As Long
Dim Alpha As String
Dim Base As Single
Dim lTemp As Single
DecimalToHex = ""
DevideTree = 1
Do Until DevideTree > Number
DevideTree = DevideTree * 16
Loop
DevideTree = DevideTree \ 16
Do Until DevideTree = 0
lTemp = Number \ DevideTree
Number = Number - (DevideTree * lTemp)
Alpha = lTemp
Call GetHex(Alpha)
DecimalToHex = DecimalToHex & Alpha
DevideTree = DevideTree \ 16
Loop
End Function
Private Function GetHex(Number As String)
Select Case Number
Case 10
Number = "A"
Case 11
Number = "B"
Case 12
Number = "C"
Case 13
Number = "D"
Case 14
Number = "E"
Case 15
Number = "F"
Case Else
Number = Number
End Select
End Function
Private Function GetDecimal(Alpha As String)
Select Case Alpha
Case "A"
Alpha = 10
Case "B"
Alpha = 11
Case "C"
Alpha = 12
Case "D"
Alpha = 13
Case "E"
Alpha = 14
Case "F"
Alpha = 15
Case Else
Alpha = Alpha
End Select
End Function
Private Function HexToDecimal(Number As String) As Long
Dim DevideTree As Long
Dim Alpha As String
Dim Length As Long
Dim sLoop As Single
DevideTree = 1
Length = Len(Number)
sLoop = 1
Do Until sLoop = Length
DevideTree = DevideTree * 16
sLoop = sLoop + 1
Loop
sLoop = 1
Do Until sLoop = Length
Alpha = Mid(Number, sLoop, 1)
Call GetDecimal(Alpha)
HexToDecimal = HexToDecimal + (DevideTree * Alpha)
DevideTree = DevideTree \ 16
sLoop = sLoop + 1
Loop
Alpha = Mid(Number, Length, 1)
Call GetDecimal(Alpha)
HexToDecimal = HexToDecimal + Alpha
End Function
Private Sub Command1_Click()
Text1.Text = DecimalToHex(Text1.Text)
End Sub
Private Sub Command2_Click()
Text1.Text = HexToDecimal(Text1.Text)
End Sub
Private Sub Form_Load()
Form1.Left = Screen.Width \ 2 - (Form1.Width)
Form1.Top = Screen.Height \ 2 - (Form1.Height)
Command1.Caption = "Decimal To Hex"
Command2.Caption = "Hex To Decimal"
Command1.Left = 120
Command1.Top = 720
Command2.Left = 1680
Command2.Top = 720
Command1.Width = 1575
Command2.Width = 1575
Command1.Height = 495
Command2.Height = 495
Text1.Height = 285
Text1.Width = 3135
Text1.Left = 120
Text1.Top = 360
Text1.Text = "97904219"
Form1.Width = 3735
Form1.Height = 2070
End Sub
No comments have been posted about This will allow a user to change any number less than 1 Trillion into Hex and Hex Back To Decimal. . Why not be the first to post a comment about This will allow a user to change any number less than 1 Trillion into Hex and Hex Back To Decimal. .