十进制数转换为BCD码
如题,把十进制数转换为BCD码,例如十进制数12转换为BCD码为00010010,程序如何去写,谢谢 --------------------编程问答-------------------- 好象有相应的汇编指令。 --------------------编程问答-------------------- 不知道怎么转呀, --------------------编程问答-------------------- '使用除余法把十进制转换成二进制Public Function DecToBin(ByVal InputString As String) As String
Dim MyResult As String
Dim MyMod As Integer
Try
Dim MyData As Integer = CInt(InputString)
Do While MyData
MyMod = MyData Mod 2
MyData = MyData \ 2
MyResult = Trim(CStr(MyMod)) & MyResult
Loop
Catch ex As Exception
MsgBox("转换数据出现错误,最大可能是数据输入不符合规范")
End Try
Return MyResult
End Function
'使用幂乘法将二进制转换成十进制
Public Function BinToDec(ByVal InputString As String) As String
Dim MyLength, i, MyResult As Long
MyLength = InputString.Length
Try
For i = 0 To MyLength - 1
MyResult = MyResult + Val(Mid(InputString, i + 1, 1)) * (2 ^ (MyLength - i - 1))
Next
Catch ex As Exception
MsgBox("转换数据出现错误,最大可能是数据输入不符合规范")
End Try
Return CStr(MyResult)
End Function
'使用标准数字格式字符串进行十进制转换成十六进制
Public Function DecToHex(ByVal InputString As String) As String
Try
Dim MyData As Long = CInt(InputString)
Dim MyResult As String = MyData.ToString("X")
Return MyResult
Catch ex As Exception
MsgBox("转换数据出现错误,最大可能是数据输入不符合规范")
End Try
End Function
'使用幂乘法将十六进制转换成十进制
Public Function HexToDec(ByVal InputString As String) As String
Dim MyChar As String
Dim MyLength, i, MyResult As Long
Try
MyLength = InputString.Length
For i = 0 To MyLength - 1
MyChar = Mid(InputString, i + 1, 1)
Select Case MyChar
Case 0 : MyChar = 0
Case 1 : MyChar = 1
Case 2 : MyChar = 2
Case 3 : MyChar = 3
Case 4 : MyChar = 4
Case 5 : MyChar = 5
Case 6 : MyChar = 6
Case 7 : MyChar = 7
Case 8 : MyChar = 8
Case 9 : MyChar = 9
Case "A" : MyChar = 10
Case "B" : MyChar = 11
Case "C" : MyChar = 12
Case "D" : MyChar = 13
Case "E" : MyChar = 14
Case "F" : MyChar = 15
End Select
'从字符串中获取十进制的真实值
MyResult += Val(MyChar) * (16 ^ (MyLength - i - 1))
Next
Catch ex As Exception
MsgBox("转换数据出现错误,最大可能是数据输入不符合规范")
End Try
Return CStr(MyResult)
End Function
'使用除余法把十进制转换成其他进制
Public Function DecToOther(ByVal InputString As String, ByVal Other As String) As String
Dim MyResult As String
Dim MyMod As Integer
Dim n As Integer = CInt(Other)
Try
Dim MyData As Integer = CInt(InputString)
Do While MyData
MyMod = MyData Mod n
MyData = MyData \ n
MyResult = Trim(CStr(MyMod)) & MyResult
Loop
Catch ex As Exception
MsgBox("转换数据出现错误,最大可能是数据输入不符合规范")
End Try
Return MyResult
End Function
'使用幂乘法将其他进制转换成十进制
Public Function OtherToDec(ByVal InputString As String, ByVal Other As String) As String
Dim MyResult, i As Integer
Try
Dim MyLength As Integer = InputString.Length
Dim n As Integer = CInt(Other)
For i = 0 To MyLength - 1
MyResult += Val(Mid(InputString, i + 1, 1)) * (n ^ (MyLength - i - 1))
Next
Catch ex As Exception
MsgBox("转换数据出现错误,最大可能是数据输入不符合规范")
End Try
Return CStr(MyResult)
End Function --------------------编程问答-------------------- 以上是各进制的转换。不知道是楼主要的不是。 --------------------编程问答--------------------
--------------------编程问答-------------------- 给一段VB6的代码供参考:
Sub Main()
Console.WriteLine(IntToBCD(12)) '00010010
Console.ReadLine()
End Sub
Function IntToBCD(ByVal nValue as Integer) As String
Dim result As String = String.Empty
Dim s As String = nValue.ToString()
Dim i As Integer
For i = 0 To nValue.ToString().Length - 1
result = result + Convert.ToString(Convert.ToInt32(s(i).ToString()), 2).PadLeft(4, "0")
Next
Return result
End Function
Option Explicit--------------------编程问答-------------------- Sub Main()
Dim ssBin As String
Dim sHex As String
Dim i As Integer
Dim sj As String
Public Function HexToBin(ByVal sHex As String) As String
Const s1 = "0000101001101111000", s2 = "0125A4936DB7FEC8"
Dim i As Integer, sBin As String
sHex = UCase(sHex)
For i = 1 To Len(sHex)
sBin = sBin & Mid(s1, InStr(1, s2, Mid(sHex, i, 1)), 4)
Next i
HexToBin = sBin
ssBin = sBin
End Function
Private Sub Command1_Click()
sj = Text1
For i = 1 To Len(sj)
sHex = Mid(sj, i, 1)
HexToBin (sHex)
Text2 = Text2 & ssBin
DoEvents
Next
End Sub
Private Sub Command2_Click()
sHex = "1"
HexToBin (sHex)
End Sub
Private Sub Form_Load()
Text1 = ""
Text2 = ""
End Sub
Console.WriteLine(IntToBCD(12)) '00010010
Console.ReadLine()
End Sub
Function IntToBCD(ByVal nValue as Integer) As String
Dim result As String = String.Empty
Dim s As String = nValue.ToString()
Dim i As Integer
For i = 0 To nValue.ToString().Length - 1
result = result + Convert.ToString(Convert.ToInt32(s(i).ToString()), 2).PadLeft(4, "0")
Next
Return result
End Function
报s(i)出错,IntToBCD()方法返回的是一个字符串,我现在是要把这字符串存到一个字节里,应该怎么处理,
--------------------编程问答-------------------- Sub Main()
Console.WriteLine(IntToBCD(12)) '00010010
Console.ReadLine()
End Sub
Function IntToBCD(ByVal nValue as Integer) As Byte
Dim result As String = String.Empty
Dim s As String = nValue.ToString()
Dim i As Integer
For i = 0 To nValue.ToString().Length - 1
result = result + Convert.ToString(Convert.ToInt32(s(i).ToString()), 2).PadLeft(4, "0")
Next
Return Convert.ToByte(result ,16)
End Function --------------------编程问答-------------------- '使用除余法把十进制转换成二进制
Public Function DecToBin(ByVal InputString As String) As String
Dim MyResult As String
Dim MyMod As Integer
Try
Dim MyData As Integer = CInt(InputString)
Do While MyData
MyMod = MyData Mod 2
MyData = MyData \ 2
MyResult = Trim(CStr(MyMod)) & MyResult
Loop
Catch ex As Exception
MsgBox("转换数据出现错误,最大可能是数据输入不符合规范")
End Try
Return MyResult
End Function
这个方法不可以吗?我工程里MODBUS协议转化就是用的这个哦!错误捕获机制都有的。 --------------------编程问答-------------------- VB.NET调试通过
--------------------编程问答--------------------
Dim ssBin As String
Dim sHex As String
Dim i As Integer
Dim sj As String
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TextBox1.Text = ""
TextBox2.Text = ""
End Sub
Public Function HexToBin(ByVal sHex As String) As String
Const s1 = "0000101001101111000", s2 = "0125A4936DB7FEC8"
Dim i As Integer, sBin As String
sHex = UCase(sHex)
For i = 1 To Len(sHex)
sBin = sBin & Mid(s1, InStr(1, s2, Mid(sHex, i, 1)), 4)
Next i
HexToBin = sBin
ssBin = sBin
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
sj = TextBox1.Text
For i = 1 To Len(sj)
sHex = Mid(sj, i, 1)
HexToBin(sHex)
TextBox2.Text = TextBox2.Text & ssBin
Next i
End Sub
事实上BCD码类似16进制数,
你的10进制的12与16进制数&H12都是00010010,如果在计算机内部运行,就直接使用16进制数表示方式,但每2个BCD码组成1个BYTE类型的16进制数。这在数字称的串口通信中经常用到
补充:.NET技术 , VB.NET