十進位制,八進位制,十六進製制,二進位制相互轉換大全

2022-06-02 07:00:05 字數 8106 閱讀 6643

' 用途:將十進位制轉化為二進位制

' 輸入:dec(十進位制數)

' 輸入資料型別:long

' 輸出:dec_to_bin(二進位制數)

' 輸出資料型別:string

' 輸入的最大數為2147483647,輸出最大數為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

' 用途:將二進位制轉化為十進位制

' 輸入:bin(二進位制數)

' 輸入資料型別:string

' 輸出:bin_to_dec(十進位制數)

' 輸出資料型別:long

' 輸入的最大數為1111111111111111111111111111111(31個1),輸出最大數為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

' 用途:將十六進製制轉化為二進位制

' 輸入:hex(十六進製制數)

' 輸入資料型別:string

' 輸出:hex_to_bin(二進位制數)

' 輸出資料型別:string

' 輸入的最大數為***個字元

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

' 用途:將二進位制轉化為十六進製制

' 輸入:bin(二進位制數)

' 輸入資料型別:string

' 輸出:bin_to_hex(十六進製制數)

' 輸出資料型別:string

' 輸入的最大數為***個字元

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)

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

while left(h, 1) = "0"

h = right(h, len(h) - 1)

wend

bin_to_hex = h

end function

' 用途:將十六進製制轉化為十進位制

' 輸入:hex(十六進製制數)

' 輸入資料型別:string

' 輸出:hex_to_dec(十進位制數)

' 輸出資料型別:long

' 輸入的最大數為7fffffff,輸出的最大數為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

' 用途:將十進位制轉化為十六進製制

' 輸入:dec(十進位制數)

' 輸入資料型別:long

' 輸出:dec_to_hex(十六進製制數)

' 輸出資料型別:string

' 輸入的最大數為2147483647,輸出最大數為7fffffff

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 a

case "10": a = "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

' 用途:將十進位制轉化為八進位制

' 輸入:dec(十進位制數)

' 輸入資料型別:long

' 輸出:dec_to_oct(八進位制數)

' 輸出資料型別:string

' 輸入的最大數為2147483647,輸出最大數為177public 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

' 用途:將八進位制轉化為十進位制

' 輸入:oct(八進位制數)

' 輸入資料型別:string

' 輸出:oct_to_dec(十進位制數)

' 輸出資料型別:long

' 輸入的最大數為177********,輸出的最大數為2147483647

public function oct_to_dec(byval oct as strin

ing) 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

' 用途:將二進位制轉化為八進位制

' 輸入:bin(二進位制數)

' 輸入資料型別:string

' 輸出:bin_to_oct(八進位制數)

' 輸出資料型別:string

' 輸入的最大數為***個字元

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"

case "100": h = h & "4"

case "101": h = h & "5"

case "110": h = h & "6"

case "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

' 用途:將八進位制轉化為二進位制

' 輸入:oct(八進位制數)

' 輸入資料型別:string

' 輸出:oct_to_bin(二進位制數)

' 輸出資料型別:string

' 輸入的最大數為***個字元

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

' 用途:將八進位制轉化為十六進製制

' 輸入:oct(八進位制數)

' 輸入資料型別:string

' 輸出:oct_to_hex(十六進製制數)

' 輸出資料型別:string

' 輸入的最大數為***個字元

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

' 用途:將十六進製制轉化為八進位制

' 輸入:hex(十六進製制數)

' 輸入資料型別:string

' 輸出:hex_to_oct(八進位制數)

' 輸出資料型別:string

' 輸入的最大數為***個字元

public function hex_to_oct(byval hex as string) as string

dim bin a

s string

hex = ucase(hex)

bin = hex_to_bin(hex)

hex_to_oct = bin_to_oct(bin)

end function

vb自帶函式:

十進位制轉八進位制:oct(num)

十六進製制轉八進位制:oct("&h" & num)

十進位制轉十六進製制:hex(num)

八進位制轉十六進製制:hex("&o" & num)

十六進製制轉換為十進位制

dim str as stringstr =

= clng("&h" & str)

十進位制轉二進位制

十進位制轉二進位制 整數及小數部分 1 把該十進位制數,用二因式分解,取餘。以235為例,轉為二進位制 235除以2得117,餘1 117除以2得58,餘1 58除以2得29,餘0 29除以2得14,餘1 14除以2得7,餘0 7除以2得3,餘1 3除以2得1,餘1 從得到的1開始寫起,餘數倒排,加...

十進位制數和二進位制數之間的相互轉換教案

教學題目十進位制數和二進位制數之間的相互轉換1 2 了解二進位制及十進位制的數值特點熟練掌握二進位制和十進位制的轉換方法 課程目標 教學重點二進位制和十進位制的轉換方法 學生通過探索掌握二進位制與十進位制之間的轉換,做到本學科與數學學科完美整合1 2 1 2 1 了解二進位制和十進位制的概念 聯絡生...

進製和十進位制轉換教師教學案

姓名分數家長評議 再平凡的人們都有他獨特的理想,再困頓的生活都有他光采的價值,不需要羨慕功成名遂的人,他們年少也曾經不知所措,你想從他們身上獲得秘訣,他只會老實告訴你 放手去實現你的理想!矚慫潤厲釤瘞睞櫪廡賴。有兩個年輕人,去求助一位老人,他們問著相同的問題 我有許多的理想和抱負,總是笨手笨腳,不知...