VB type conversion

Source: Internet
Author: User
Tags binary to decimal decimal to binary

Purpose: Convert decimal to binary.
Input: Dec (decimal number)
Input data type: Long
Output: dec_to_bin (binary number)
Output Data Type: String
The maximum number of inputs is 2147483647, and the maximum number of outputs is 1111111111111111111111111111111 (31 1)
Public Function dec_to_bin (Dec as long) as string
Dec_to_bin = ""
Do While dec> 0
Dec_to_bin = dec mod 2 & dec_to_bin
Dec = DEC/2
Loop
End Function

Purpose: Convert binary to decimal.
Input: Bin (Binary)
Input data type: String
Output: bin_to_dec (decimal number)
Output Data Type: Long
The maximum number of inputs is 1111111111111111111111111111111 (31 1), and the maximum number of outputs is 2147483647.
Public Function bin_to_dec (byval bin as string) as long
Dim I as long
For I = 1 to Len (BIN)
Bin_to_dec = bin_to_dec * 2 + val (mid (bin, I, 1 ))
Next I
End Function

Purpose: Convert hexadecimal to binary.
Input: Hex (hexadecimal number)
Input data type: String
Output: hex_to_bin (binary number)
Output Data Type: String
The maximum number of characters entered is 2147483647 characters.
Public Function hex_to_bin (byval hex as string) as string
Dim I as long
Dim B as string

Hex = ucase (HEX)
For I = 1 to Len (HEX)
Select case mid (Hex, I, 1)
Case "0": B = B <0000"
Case "1": B = B <0001"
Case "2": B = B <0010"
Case "3": B = B <0011"
Case "4": B = B <0100"
Case "5": B = B <0101"
Case "6": B = B <0110"
Case "7": B = B <0111"
Case "8": B = B <1000"
Case "9": B = B <1001"
Case "A": B = B <1010"
Case "B": B = B <1011"
Case "C": B = B <1100"
Case "D": B = B <1101"
Case "E": B = B <1110"
Case "F": B = B <1111"
End select
Next I
While left (B, 1) = "0"
B = right (B, Len (B)-1)
Wend
Hex_to_bin = B
End Function

Purpose: Convert binary to hexadecimal
Input: Bin (Binary)
Input data type: String
Output: bin_to_hex (hexadecimal number)
Output Data Type: String
The maximum number of characters entered is 2147483647 characters.
Public Function bin_to_hex (byval bin as string) as string
Dim I as long
Dim h as string
If Len (BIN) mod 4 <> 0 then
Bin = string (4-len (BIN) mod 4, "0") & Bin
End if

For I = 1 to Len (BIN) Step 4
Select case mid (bin, I, 4)
Example "0000": H = H & "0"
Example "0001": H = H & "1"
Example "0010": H = H & "2"
Example "0011": H = H & "3"
Example "0100": H = H & "4"
Case "0101": H = H & "5"
Example "0110": H = H & "6"
Example "0111": H = H & "7"
Example "1000": H = H & "8"
Example "1001": H = H & "9"
Example "1010": H = H & ""
Example "1011": H = H & "B"
Example "1100": H = H & "C"
Example "1101": H = H & "D"
Example "1110": H = H & "e"
Example "1111": H = H & "F"
End select
Next I
While left (H, 1) = "0"
H = right (H, Len (H)-1)
Wend
Bin_to_hex = H
End Function

Purpose: Convert hexadecimal to decimal.
Input: Hex (hexadecimal number)
Input data type: String
Output: hex_to_dec (decimal number)
Output Data Type: Long
The maximum number of inputs is 7 fffff, and the maximum number of outputs is 2147483647
Public Function hex_to_dec (byval hex as string) as long
Dim I as long
Dim B as long

Hex = ucase (HEX)
For I = 1 to Len (HEX)
Select case mid (Hex, Len (HEX)-I + 1, 1)
Case "0": B = B + 16 ^ (I-1) * 0
Case "1": B = B + 16 ^ (I-1) * 1
Case "2": B = B + 16 ^ (I-1) * 2
Case "3": B = B + 16 ^ (I-1) * 3
Case "4": B = B + 16 ^ (I-1) * 4
Case "5": B = B + 16 ^ (I-1) * 5
Case "6": B = B + 16 ^ (I-1) * 6
Case "7": B = B + 16 ^ (I-1) * 7
Case "8": B = B + 16 ^ (I-1) * 8
Case "9": B = B + 16 ^ (I-1) * 9
Case "A": B = B + 16 ^ (I-1) * 10
Case "B": B = B + 16 ^ (I-1) * 11
Case "C": B = B + 16 ^ (I-1) * 12
Case "D": B = B + 16 ^ (I-1) * 13
Case "E": B = B + 16 ^ (I-1) * 14
Case "F": B = B + 16 ^ (I-1) * 15
End select
Next I
Hex_to_dec = B
End Function
Purpose: Convert decimal to hexadecimal
Input: Dec (decimal number)
Input data type: Long
Output: dec_to_hex (hexadecimal number)
Output Data Type: String
The maximum number of inputs is 2147483647, and the maximum number of outputs is 7 fffff.
Public Function dec_to_hex (Dec as long) as string
Dim A as string
Dec_to_hex = ""
Do While dec> 0
A = CSTR (Dec mod 16)
Select case
Case "10": A = ""
Case "11": A = "B"
Case "12": A = "C"
Case "13": A = "D"
Case "14": A = "e"
Case "15": A = "F"
End select
Dec_to_hex = A & dec_to_hex
Dec = DEC/16
Loop
End Function

Purpose: Convert decimal to octal.
Input: Dec (decimal number)
Input data type: Long
Output: dec_to_oct (octal number)
Output Data Type: String
The maximum number of inputs is 2147483647, and the maximum number of outputs is 17777777777.
Public Function dec_to_oct (Dec as long) as string
Dec_to_oct = ""
Do While dec> 0
Dec_to_oct = dec mod 8 & dec_to_oct
Dec = DEC/8
Loop
End Function

Purpose: Convert octal to decimal.
Input: Oct (octal)
Input data type: String
Output: oct_to_dec (decimal number)
Output Data Type: Long
The maximum number of inputs is 17777777777, and the maximum number of outputs is 2147483647.
Public Function oct_to_dec (byval Oct as string) as long
Dim I as long
Dim B as long

For I = 1 to Len (OCT)
Select case mid (Oct, Len (OCT)-I + 1, 1)
Case "0": B = B + 8 ^ (I-1) * 0
Case "1": B = B + 8 ^ (I-1) * 1
Case "2": B = B + 8 ^ (I-1) * 2
Case "3": B = B + 8 ^ (I-1) * 3
Case "4": B = B + 8 ^ (I-1) * 4
Case "5": B = B + 8 ^ (I-1) * 5
Case "6": B = B + 8 ^ (I-1) * 6
Case "7": B = B + 8 ^ (I-1) * 7
End select
Next I
Oct_to_dec = B
End Function

Purpose: Convert binary to octal
Input: Bin (Binary)
Input data type: String
Output: bin_to_oct (eight digits)
Output Data Type: String
The maximum number of characters entered is 2147483647 characters.
Public Function bin_to_oct (byval bin as string) as string
Dim I as long
Dim h as string
If Len (BIN) mod 3 <> 0 then
Bin = string (3-len (BIN) mod 3, "0") & Bin
End if

For I = 1 to Len (BIN) Step 3
Select case mid (bin, I, 3)
Case "000": H = H & "0"
Case "001": H = H & "1"
Case "010": H = H & "2"
Case "011": H = H & "3"
Example "100": H = H & "4"
Case "101": H = H & "5"
Example "110": H = H & "6"
Example "111": H = H & "7"
End select
Next I
While left (H, 1) = "0"
H = right (H, Len (H)-1)
Wend
Bin_to_oct = H
End Function

Purpose: Convert octal to binary
Input: Oct (octal)
Input data type: String
Output: oct_to_bin (Binary)
Output Data Type: String
The maximum number of characters entered is 2147483647 characters.
Public Function oct_to_bin (byval Oct as string) as string
Dim I as long
Dim B as string

For I = 1 to Len (OCT)
Select case mid (Oct, I, 1)
Case "0": B = B & "000"
Case "1": B = B & "001"
Case "2": B = B & "010"
Case "3": B = B & "011"
Case "4": B = B <100"
Case "5": B = B <101"
Case "6": B = B <110"
Case "7": B = B <111"
End select
Next I
While left (B, 1) = "0"
B = right (B, Len (B)-1)
Wend
Oct_to_bin = B
End Function

Purpose: Convert octal to hexadecimal
Input: Oct (octal)
Input data type: String
Output: oct_to_hex (hexadecimal number)
Output Data Type: String
The maximum number of characters entered is 2147483647 characters.
Public Function oct_to_hex (byval Oct as string) as string
Dim bin as string
Bin = oct_to_bin (OCT)
Oct_to_hex = bin_to_hex (BIN)
End Function

Purpose: Convert hexadecimal to octal
Input: Hex (hexadecimal number)
Input data type: String
Output: hex_to_oct (eight digits)
Output Data Type: String
The maximum number of characters entered is 2147483647 characters.
Public Function hex_to_oct (byval hex as string) as string
Dim bin as string
Hex = ucase (HEX)
Bin = hex_to_bin (HEX)
Hex_to_oct = bin_to_oct (BIN)
End Function


VB built-in functions:
Decimal to octal: Oct (Num)
Hexadecimal to octal: Oct ("& H" & num)
Convert decimal to hexadecimal: Hex (Num)
Octal to hexadecimal: Hex ("& O" & num)

Convert hexadecimal to decimal
Dim STR as string
STR = text2.text
Text10.text = clng ("& H" & Str)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.