14 个解决方案
#1
没有,你可以先转16进制,可以方便些,写函数吧
#2
vb自带转16进制的函数HEX(),十进制转二进制我前一阵子在csdn上给别人写过一个,代码没有保存,我去找找,找得到的话等一下给你贴出来
Hex 函数
返回代表十六进制数值的 String。
语法
Hex(number)
必要的 number 参数为任何有效的数值表达式或字符串表达式。
说明
如果 number 还不是一个整数,那么在执行前会先被四舍五入成最接近的整数。
如果 number 为 所得为
Null Null
Empty 零 (0)
任何其他的数字 最多可到八个十六进制字符。
适当范围内的数字,前缀以 &H,可以直接表示十六进制数字。例如,十六进制表示法的 &H10 代表十进制的 16。
Hex 函数
返回代表十六进制数值的 String。
语法
Hex(number)
必要的 number 参数为任何有效的数值表达式或字符串表达式。
说明
如果 number 还不是一个整数,那么在执行前会先被四舍五入成最接近的整数。
如果 number 为 所得为
Null Null
Empty 零 (0)
任何其他的数字 最多可到八个十六进制字符。
适当范围内的数字,前缀以 &H,可以直接表示十六进制数字。例如,十六进制表示法的 &H10 代表十进制的 16。
#3
呵呵,没找到自己回过的那一帖,却找到了别人写的,借花献佛吧
Option Explicit
Private Function TenturnTwo(ByVal varNum As Long) As String
Dim returnString As String
Dim ModNum As Integer
Do While varNum > 0
ModNum = varNum Mod 2
varNum = varNum \ 2
returnString = Trim(Str(ModNum)) + returnString
Loop
TenturnTwo = returnString
End Function
Private Function TwoturnTen(ByVal varString As String) As Long
Dim Slen As Long
Dim I As Long
Dim returnNum As Long
Slen = Len(varString)
For I = 0 To Slen - 1
returnNum = returnNum + Val(Mid(varString, I + 1, 1)) * (2 ^ (Slen - I - 1))
Next
TwoturnTen = returnNum
End Function
Private Sub Command3_Click()
MsgBox TenturnTwo(5)
MsgBox TwoturnTen("101")
End Sub
Option Explicit
Private Function TenturnTwo(ByVal varNum As Long) As String
Dim returnString As String
Dim ModNum As Integer
Do While varNum > 0
ModNum = varNum Mod 2
varNum = varNum \ 2
returnString = Trim(Str(ModNum)) + returnString
Loop
TenturnTwo = returnString
End Function
Private Function TwoturnTen(ByVal varString As String) As Long
Dim Slen As Long
Dim I As Long
Dim returnNum As Long
Slen = Len(varString)
For I = 0 To Slen - 1
returnNum = returnNum + Val(Mid(varString, I + 1, 1)) * (2 ^ (Slen - I - 1))
Next
TwoturnTen = returnNum
End Function
Private Sub Command3_Click()
MsgBox TenturnTwo(5)
MsgBox TwoturnTen("101")
End Sub
#4
楼上兄,带有小数的就不行了
#5
刚才没看清楚题目,刚才写了两个函数作数制转换,不过就如 lmhgd(小菜) 说的,暂时之能处理整数。小数的比较麻烦,我再想想
Private Sub Command1_Click()
Debug.Print DecToBin(100)
Debug.Print BinToDec("001100100")
End Sub
Private Function DecToBin(intInput As Integer) As String
Dim strTemp As String
Dim i As Integer
strTemp = Oct(intInput)
For i = 1 To Len(strTemp)
Select Case Mid(strTemp, i, 1)
Case "0"
DecToBin = DecToBin + "000"
Case "1"
DecToBin = DecToBin + "001"
Case "2"
DecToBin = DecToBin + "010"
Case "3"
DecToBin = DecToBin + "011"
Case "4"
DecToBin = DecToBin + "100"
Case "5"
DecToBin = DecToBin + "101"
Case "6"
DecToBin = DecToBin + "110"
Case "7"
DecToBin = DecToBin + "111"
End Select
Next
End Function
Private Function BinToDec(strInput As String) As Long
Dim i As Integer
For i = 0 To Len(strInput) - 1
BinToDec = BinToDec + 2 ^ i * Val(Mid(strInput, Len(strInput) - i, 1))
Next
End Function
Private Sub Command1_Click()
Debug.Print DecToBin(100)
Debug.Print BinToDec("001100100")
End Sub
Private Function DecToBin(intInput As Integer) As String
Dim strTemp As String
Dim i As Integer
strTemp = Oct(intInput)
For i = 1 To Len(strTemp)
Select Case Mid(strTemp, i, 1)
Case "0"
DecToBin = DecToBin + "000"
Case "1"
DecToBin = DecToBin + "001"
Case "2"
DecToBin = DecToBin + "010"
Case "3"
DecToBin = DecToBin + "011"
Case "4"
DecToBin = DecToBin + "100"
Case "5"
DecToBin = DecToBin + "101"
Case "6"
DecToBin = DecToBin + "110"
Case "7"
DecToBin = DecToBin + "111"
End Select
Next
End Function
Private Function BinToDec(strInput As String) As Long
Dim i As Integer
For i = 0 To Len(strInput) - 1
BinToDec = BinToDec + 2 ^ i * Val(Mid(strInput, Len(strInput) - i, 1))
Next
End Function
#6
//楼上兄,带有小数的就不行了
至于小数,你可以分别取出小数和整数部分转换为2进制,然后再合并
至于小数,你可以分别取出小数和整数部分转换为2进制,然后再合并
#7
我用 chewinggum(口香糖·向星星前进) 的代码该了一下,可以实现2 to 10的转换
10 to 2 应好写多了(精确到0.0000000000)
Option Explicit
Private Function TenturnTwo(ByVal varNum As Variant) As String
Dim returnString(1) As String
Dim ModNum As Integer
Dim I As Integer
Dim Num(1) As String
On Error Resume Next
Dim ClsNum
ClsNum = Split(varNum, ".")
Num(0) = ClsNum(0)
Num(1) = Format("0." & ClsNum(1), "0.0000000000")
'ÕûÊý²¿·Ö
Do While Val(Num(0)) > 0
ModNum = Num(0) Mod 2
Num(0) = Num(0) \ 2
returnString(0) = Trim(Str(ModNum)) + returnString(0)
Loop
'СÊý²¿·Ö
I = 0
Do While Val(Num(1)) <> 0
On Error GoTo Err:
ModNum = Left(Num(1) * 2, 2)
returnString(1) = returnString(1) & Trim(Str(ModNum))
ClsNum = Split(Num(1) * 2, ".")
Num(1) = Format("0." & ClsNum(1), "0.0000000000")
I = I + 1
If I > 10 Then
Num(1) = "0"
End If
Loop
Err:
TenturnTwo = returnString(0) & "." & returnString(1)
End Function
Private Function TwoturnTen(ByVal varString As String) As Long
Dim Slen As Long
Dim I As Long
Dim returnNum As Long
Slen = Len(varString)
For I = 0 To Slen - 1
returnNum = returnNum + Val(Mid(varString, I + 1, 1)) * (2 ^ (Slen - I - 1))
Next
TwoturnTen = returnNum
End Function
Private Sub Command1_Click()
MsgBox TenturnTwo(1.125)
MsgBox TwoturnTen("101")
End Sub
10 to 2 应好写多了(精确到0.0000000000)
Option Explicit
Private Function TenturnTwo(ByVal varNum As Variant) As String
Dim returnString(1) As String
Dim ModNum As Integer
Dim I As Integer
Dim Num(1) As String
On Error Resume Next
Dim ClsNum
ClsNum = Split(varNum, ".")
Num(0) = ClsNum(0)
Num(1) = Format("0." & ClsNum(1), "0.0000000000")
'ÕûÊý²¿·Ö
Do While Val(Num(0)) > 0
ModNum = Num(0) Mod 2
Num(0) = Num(0) \ 2
returnString(0) = Trim(Str(ModNum)) + returnString(0)
Loop
'СÊý²¿·Ö
I = 0
Do While Val(Num(1)) <> 0
On Error GoTo Err:
ModNum = Left(Num(1) * 2, 2)
returnString(1) = returnString(1) & Trim(Str(ModNum))
ClsNum = Split(Num(1) * 2, ".")
Num(1) = Format("0." & ClsNum(1), "0.0000000000")
I = I + 1
If I > 10 Then
Num(1) = "0"
End If
Loop
Err:
TenturnTwo = returnString(0) & "." & returnString(1)
End Function
Private Function TwoturnTen(ByVal varString As String) As Long
Dim Slen As Long
Dim I As Long
Dim returnNum As Long
Slen = Len(varString)
For I = 0 To Slen - 1
returnNum = returnNum + Val(Mid(varString, I + 1, 1)) * (2 ^ (Slen - I - 1))
Next
TwoturnTen = returnNum
End Function
Private Sub Command1_Click()
MsgBox TenturnTwo(1.125)
MsgBox TwoturnTen("101")
End Sub
#8
多谢谢各位大侠,己解决了
#9
多谢谢各位大侠,己解决了
#10
结帖了,恭喜
#11
呵呵,,不知道能不能捡到分啊???哈哈
#12
哈哈,我也试试
#13
不错解决问及了就好
#14
其实可以扩展一下,也就是n制进制数可以除以n求余
#1
没有,你可以先转16进制,可以方便些,写函数吧
#2
vb自带转16进制的函数HEX(),十进制转二进制我前一阵子在csdn上给别人写过一个,代码没有保存,我去找找,找得到的话等一下给你贴出来
Hex 函数
返回代表十六进制数值的 String。
语法
Hex(number)
必要的 number 参数为任何有效的数值表达式或字符串表达式。
说明
如果 number 还不是一个整数,那么在执行前会先被四舍五入成最接近的整数。
如果 number 为 所得为
Null Null
Empty 零 (0)
任何其他的数字 最多可到八个十六进制字符。
适当范围内的数字,前缀以 &H,可以直接表示十六进制数字。例如,十六进制表示法的 &H10 代表十进制的 16。
Hex 函数
返回代表十六进制数值的 String。
语法
Hex(number)
必要的 number 参数为任何有效的数值表达式或字符串表达式。
说明
如果 number 还不是一个整数,那么在执行前会先被四舍五入成最接近的整数。
如果 number 为 所得为
Null Null
Empty 零 (0)
任何其他的数字 最多可到八个十六进制字符。
适当范围内的数字,前缀以 &H,可以直接表示十六进制数字。例如,十六进制表示法的 &H10 代表十进制的 16。
#3
呵呵,没找到自己回过的那一帖,却找到了别人写的,借花献佛吧
Option Explicit
Private Function TenturnTwo(ByVal varNum As Long) As String
Dim returnString As String
Dim ModNum As Integer
Do While varNum > 0
ModNum = varNum Mod 2
varNum = varNum \ 2
returnString = Trim(Str(ModNum)) + returnString
Loop
TenturnTwo = returnString
End Function
Private Function TwoturnTen(ByVal varString As String) As Long
Dim Slen As Long
Dim I As Long
Dim returnNum As Long
Slen = Len(varString)
For I = 0 To Slen - 1
returnNum = returnNum + Val(Mid(varString, I + 1, 1)) * (2 ^ (Slen - I - 1))
Next
TwoturnTen = returnNum
End Function
Private Sub Command3_Click()
MsgBox TenturnTwo(5)
MsgBox TwoturnTen("101")
End Sub
Option Explicit
Private Function TenturnTwo(ByVal varNum As Long) As String
Dim returnString As String
Dim ModNum As Integer
Do While varNum > 0
ModNum = varNum Mod 2
varNum = varNum \ 2
returnString = Trim(Str(ModNum)) + returnString
Loop
TenturnTwo = returnString
End Function
Private Function TwoturnTen(ByVal varString As String) As Long
Dim Slen As Long
Dim I As Long
Dim returnNum As Long
Slen = Len(varString)
For I = 0 To Slen - 1
returnNum = returnNum + Val(Mid(varString, I + 1, 1)) * (2 ^ (Slen - I - 1))
Next
TwoturnTen = returnNum
End Function
Private Sub Command3_Click()
MsgBox TenturnTwo(5)
MsgBox TwoturnTen("101")
End Sub
#4
楼上兄,带有小数的就不行了
#5
刚才没看清楚题目,刚才写了两个函数作数制转换,不过就如 lmhgd(小菜) 说的,暂时之能处理整数。小数的比较麻烦,我再想想
Private Sub Command1_Click()
Debug.Print DecToBin(100)
Debug.Print BinToDec("001100100")
End Sub
Private Function DecToBin(intInput As Integer) As String
Dim strTemp As String
Dim i As Integer
strTemp = Oct(intInput)
For i = 1 To Len(strTemp)
Select Case Mid(strTemp, i, 1)
Case "0"
DecToBin = DecToBin + "000"
Case "1"
DecToBin = DecToBin + "001"
Case "2"
DecToBin = DecToBin + "010"
Case "3"
DecToBin = DecToBin + "011"
Case "4"
DecToBin = DecToBin + "100"
Case "5"
DecToBin = DecToBin + "101"
Case "6"
DecToBin = DecToBin + "110"
Case "7"
DecToBin = DecToBin + "111"
End Select
Next
End Function
Private Function BinToDec(strInput As String) As Long
Dim i As Integer
For i = 0 To Len(strInput) - 1
BinToDec = BinToDec + 2 ^ i * Val(Mid(strInput, Len(strInput) - i, 1))
Next
End Function
Private Sub Command1_Click()
Debug.Print DecToBin(100)
Debug.Print BinToDec("001100100")
End Sub
Private Function DecToBin(intInput As Integer) As String
Dim strTemp As String
Dim i As Integer
strTemp = Oct(intInput)
For i = 1 To Len(strTemp)
Select Case Mid(strTemp, i, 1)
Case "0"
DecToBin = DecToBin + "000"
Case "1"
DecToBin = DecToBin + "001"
Case "2"
DecToBin = DecToBin + "010"
Case "3"
DecToBin = DecToBin + "011"
Case "4"
DecToBin = DecToBin + "100"
Case "5"
DecToBin = DecToBin + "101"
Case "6"
DecToBin = DecToBin + "110"
Case "7"
DecToBin = DecToBin + "111"
End Select
Next
End Function
Private Function BinToDec(strInput As String) As Long
Dim i As Integer
For i = 0 To Len(strInput) - 1
BinToDec = BinToDec + 2 ^ i * Val(Mid(strInput, Len(strInput) - i, 1))
Next
End Function
#6
//楼上兄,带有小数的就不行了
至于小数,你可以分别取出小数和整数部分转换为2进制,然后再合并
至于小数,你可以分别取出小数和整数部分转换为2进制,然后再合并
#7
我用 chewinggum(口香糖·向星星前进) 的代码该了一下,可以实现2 to 10的转换
10 to 2 应好写多了(精确到0.0000000000)
Option Explicit
Private Function TenturnTwo(ByVal varNum As Variant) As String
Dim returnString(1) As String
Dim ModNum As Integer
Dim I As Integer
Dim Num(1) As String
On Error Resume Next
Dim ClsNum
ClsNum = Split(varNum, ".")
Num(0) = ClsNum(0)
Num(1) = Format("0." & ClsNum(1), "0.0000000000")
'ÕûÊý²¿·Ö
Do While Val(Num(0)) > 0
ModNum = Num(0) Mod 2
Num(0) = Num(0) \ 2
returnString(0) = Trim(Str(ModNum)) + returnString(0)
Loop
'СÊý²¿·Ö
I = 0
Do While Val(Num(1)) <> 0
On Error GoTo Err:
ModNum = Left(Num(1) * 2, 2)
returnString(1) = returnString(1) & Trim(Str(ModNum))
ClsNum = Split(Num(1) * 2, ".")
Num(1) = Format("0." & ClsNum(1), "0.0000000000")
I = I + 1
If I > 10 Then
Num(1) = "0"
End If
Loop
Err:
TenturnTwo = returnString(0) & "." & returnString(1)
End Function
Private Function TwoturnTen(ByVal varString As String) As Long
Dim Slen As Long
Dim I As Long
Dim returnNum As Long
Slen = Len(varString)
For I = 0 To Slen - 1
returnNum = returnNum + Val(Mid(varString, I + 1, 1)) * (2 ^ (Slen - I - 1))
Next
TwoturnTen = returnNum
End Function
Private Sub Command1_Click()
MsgBox TenturnTwo(1.125)
MsgBox TwoturnTen("101")
End Sub
10 to 2 应好写多了(精确到0.0000000000)
Option Explicit
Private Function TenturnTwo(ByVal varNum As Variant) As String
Dim returnString(1) As String
Dim ModNum As Integer
Dim I As Integer
Dim Num(1) As String
On Error Resume Next
Dim ClsNum
ClsNum = Split(varNum, ".")
Num(0) = ClsNum(0)
Num(1) = Format("0." & ClsNum(1), "0.0000000000")
'ÕûÊý²¿·Ö
Do While Val(Num(0)) > 0
ModNum = Num(0) Mod 2
Num(0) = Num(0) \ 2
returnString(0) = Trim(Str(ModNum)) + returnString(0)
Loop
'СÊý²¿·Ö
I = 0
Do While Val(Num(1)) <> 0
On Error GoTo Err:
ModNum = Left(Num(1) * 2, 2)
returnString(1) = returnString(1) & Trim(Str(ModNum))
ClsNum = Split(Num(1) * 2, ".")
Num(1) = Format("0." & ClsNum(1), "0.0000000000")
I = I + 1
If I > 10 Then
Num(1) = "0"
End If
Loop
Err:
TenturnTwo = returnString(0) & "." & returnString(1)
End Function
Private Function TwoturnTen(ByVal varString As String) As Long
Dim Slen As Long
Dim I As Long
Dim returnNum As Long
Slen = Len(varString)
For I = 0 To Slen - 1
returnNum = returnNum + Val(Mid(varString, I + 1, 1)) * (2 ^ (Slen - I - 1))
Next
TwoturnTen = returnNum
End Function
Private Sub Command1_Click()
MsgBox TenturnTwo(1.125)
MsgBox TwoturnTen("101")
End Sub
#8
多谢谢各位大侠,己解决了
#9
多谢谢各位大侠,己解决了
#10
结帖了,恭喜
#11
呵呵,,不知道能不能捡到分啊???哈哈
#12
哈哈,我也试试
#13
不错解决问及了就好
#14
其实可以扩展一下,也就是n制进制数可以除以n求余