VB 二进制,八进制,十进制,十六进制转换
2024-07-21 02:20:50
供稿:网友
public function d_to_b(byval dec as long) as string
do
d_to_b = dec mod 2 & d_to_b
dec = dec / 2
loop while dec
end function
public function b_to_d(byval bin as string) as integer
dim i as long
for i = 1 to len(bin)
b_to_d = b_to_d * 2 + val(mid(bin, i, 1))
next i
end function
public function h_to_b(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
h_to_b = b
end function
public function b_to_h(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)
case "0000": h = h & "0"
case "0001": h = h & "1"
case "0010": h = h & "2"
case "0011": h = h & "3"
case "0100": h = h & "4"
case "0101": h = h & "5"
case "0110": h = h & "6"
case "0111": h = h & "7"
case "1000": h = h & "8"
case "1001": h = h & "9"
case "1010": h = h & "a"
case "1011": h = h & "b"
case "1100": h = h & "c"
case "1101": h = h & "d"
case "1110": h = h & "e"
case "1111": h = h & "f"
end select
next i
b_to_h = h
end function