Imports System.IO
Imports System.IO.Ports
Public Class Form2
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
SerialPort1.Open()
'(Me.Move(Screen.Width - Me.Width) \ 2, (Screen.Height - Me.Height) \ 2)
txtHexadecimal.Text = ""
txtBinary.Text = ""
txtDecimal.Text = ""
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If SerialPort1.IsOpen Then
With txtHexadecimal
.AppendText(SerialPort1.ReadLine & vbCrLf)
.ScrollToCaret()
End With
End If
SerialPort1.Close()
End Sub
Function CheckHexaDecimal(ByVal Index As Integer)
Select Case Index
' ASCII Code 48 - 57 คือ ตัวอักขระ 0 - 9
' ASCII Code 65 - 70 คือ ตัวอักขระ A - F
Case 48 To 57, 65 To 70
Case 8, 13
Case Else
Index = 0
End Select
CheckHexaDecimal = Index
End Function
Private Sub cmdConvert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdConvert.Click
Dim i As Integer
txtBinary.Text = ""
txtDecimal.Text = ""
If Trim$(txtHexadecimal.Text) = "" Or Len(Trim$(txtHexadecimal.Text)) = 0 Then Exit Sub
For i = Len(txtHexadecimal.Text) To 1 Step -1
Select Case Mid$(txtHexadecimal.Text, i, 1)
Case "0"
txtBinary.Text = "0000" & txtBinary.Text
Case "1"
txtBinary.Text = "0001" & txtBinary.Text
Case "2"
txtBinary.Text = "0010" & txtBinary.Text
Case "3"
txtBinary.Text = "0011" & txtBinary.Text
Case "4"
txtBinary.Text = "0100" & txtBinary.Text
Case "5"
txtBinary.Text = "0101" & txtBinary.Text
Case "6"
txtBinary.Text = "0110" & txtBinary.Text
Case "7"
txtBinary.Text = "0111" & txtBinary.Text
Case "8"
txtBinary.Text = "1000" & txtBinary.Text
Case "9"
txtBinary.Text = "1001" & txtBinary.Text
Case "A"
txtBinary.Text = "1010" & txtBinary.Text
Case "B"
txtBinary.Text = "1011" & txtBinary.Text
Case "C"
txtBinary.Text = "1100" & txtBinary.Text
Case "D"
txtBinary.Text = "1101" & txtBinary.Text
Case "E"
txtBinary.Text = "1110" & txtBinary.Text
Case "F"
txtBinary.Text = "1111" & txtBinary.Text
End Select
Next
Dim Dec As Double
Dim BitWeight As Integer
For i = Len(txtBinary.Text) To 1 Step -1
If Mid$(txtBinary.Text, i, 1) <> 0 Then Dec = Dec + ((2 ^ BitWeight) * Mid$(txtBinary.Text, i, 1))
BitWeight = BitWeight + 1
Next
txtDecimal.Text = Dec
' เรียกใช้ฟังค์ชั่นโดยการคำนวณหาค่าเลขฐาน 10 จากสมการโดยตรง
' ของเดิม
'MsgBox(HexToDecimal(txtHexadecimal.Text))
MsgBox(Hex(txtHexadecimal.Text))
End Sub
End Class
'Function แปลงเลขฐาน 10 เป็น ฐาน 16
Private Function DecimalToHex(ByVal Dec As Long) As String
Dim TextResult As String = ""
Dim TextConvert As String = ""
Do While (Dec > 0)
TextConvert = (Dec Mod 16)
Select Case TextConvert
Case "10"
TextConvert = "A"
Case "11"
TextConvert = "B"
Case "12"
TextConvert = "C"
Case "13"
TextConvert = "D"
Case "14"
TextConvert = "E"
Case "15"
TextConvert = "F"
Case Else
TextConvert = TextConvert
End Select
TextResult = TextConvert & TextResult
Dec = Dec \ 16
Loop
Return TextResult
End Function
วิธีที่ 2 วิธีสมัยใหม่
Function แปลงเลขฐาน 10 เป็น ฐาน 16
Private Function DecimalToHex(ByVal Dec As Long) As String
Dim TextResult As String = Convert.ToString(Dec, 16)
Return TextResult
End Function
Date :
2010-12-10 23:23:46
By :
Gunboy
No. 4
Guest
เอ้า 16 เป็น 10 นี้หว่าครับ โทษที เอาใหม่
วิธีที่ 1 วิธีโบราณ
'Function แปลงเลขฐาน 16 เป็น ฐาน 10
Private Function HexToDecimal(ByVal Hex As String) As String
Dim TextResult As String = ""
Dim CountText As Integer = Len(Hex)
Dim CalCulate As Long
Dim Dec As Byte
Dim i As Integer
For i = 1 To CountText
TextResult = Hex.Substring(CountText - i, 1)
Select Case TextResult
Case "A"
Dec = 10
Case "B"
Dec = 11
Case "C"
Dec = 12
Case "D"
Dec = 13
Case "E"
Dec = 14
Case "F"
Dec = 15
Case Else
Dec = Val(TextResult)
End Select
CalCulate = CalCulate + (Dec * (16 ^ (i - 1)))
Next i
TextResult = CalCulate.ToString
Return TextResult
End Function
วิธีที่ 2 วิธีสมัยใหม่
Function แปลงเลขฐาน 16 เป็น ฐาน 10
Private Function DecimalToHex(ByVal Hex As String) As String
dim Num as Long = Convert.ToInt64(InputText, 16)
Dim TextResult As String = Convert.ToString(Num, 10)
Return TextResult
End Function
Date :
2010-12-10 23:30:04
By :
Gunboy
No. 5
Guest
' Function ตัด String ตัวหน้าทิ้ง ครับ
Private Function CutFirstString(ByVal FirstString As String) As String
Dim CountString as Integer = Len(FirstString)
Dim SecString as String = FirstString.Substring(1,CountString-1)