hi, every one
I'm Coding Program In VB .net Can Convert The Base :-)
I Want Some Help In Converting
1 - Decimal To ASCII
2 - ASCII To Decimal
3 - ASCII To Hex
Please Can any one Give me I Simple Code
Thanks |
hi, every one
I'm Coding Program In VB .net Can Convert The Base :-)
I Want Some Help In Converting
1 - Decimal To ASCII
2 - ASCII To Decimal
3 - ASCII To Hex
Please Can any one Give me I Simple Code
Thanks |
I think this is what you are asking for:
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
MsgBox(Chr(65)) ' decimal 65 is character 'A'
MsgBox(Asc("A").ToString) ' 'A' is number 65 decimal in ASCII
MsgBox(Hex(Asc("A"))) ' decimal 65 is hex 41
End Sub
It's funny Natty, i had a different reading than tall dude of your question. i think i was right.
I don't think you are really looking for ascii output What you mean is a string say to a textbox
Like this
Dim
a As Decimal = 123.4TextBox1.Text = a.ToString
Dim s As Decimal = CDec("123.4")
Textbox1.text = hex(cint("1234"))
The reason that there is ambiguity in responses is that although it appears that your question is clear, it doesn't specify nearly as much as a programmer needs to know about what you are asking for.
But textbox outputs are strings.
In validation one can spot a decimal but "1234" could be either hex or integer and he has not specified a way for the user to specify that.
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim OutPutString As String = ""
If Trim(TextBox1.Text).Length = 0 Then
MsgBox("No Numbers to translate!")
Exit Sub
End If
Dim InputChars() As String = Split(TextBox1.Text, " ")
For I As Integer = 0 To InputChars.Length - 1
If InputChars(I) <> "" Then
If IsNumeric(InputChars(I)) AndAlso _
Val(InputChars(I)) < 256 _
AndAlso Val(InputChars(I)) > 0 Then
OutPutString += ChrW(Val(InputChars(I)))
Else
MsgBox("All input should be numeric, between 1-255," _
& " with values separated by a space.")
Exit Sub
End If
End If
Next
TextBox2.Text = OutPutString
End Sub
End Class
There are problems in what the OP has proposed and in Tall dudes solution.
As far as the OP is concerned in his request, he has provided for no way to programmatically discern the users intent and there is no way for the software to distinguish between the string "1234" if the intended radix is base 10 or base16.
A decimal number can be discerned by
If StringName.contains(".") then ... do whatever. There is no need for all that looping.
What he is asking for tall dude is freeform input
He wants radix conversion but has no specified a way for the user to say what the input radix is leaving serveral cases of ambiguity.
You can definitely guess that 12a2 is hex input and concert that to decimal.
You can say that 12J2 is not valid input.
But software will have no idea what to do with "1234" because the intended radix could be either hex or decimal.
Tall Dude wrote:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim OutPutString As String = ""
If Trim(TextBox1.Text).Length = 0 Then
MsgBox("No Numbers to translate!")
Exit Sub
End If
Dim InputChars() As String = Split(TextBox1.Text, " ")
For I As Integer = 0 To InputChars.Length - 1
If InputChars(I) <> "" Then
If IsNumeric(InputChars(I)) AndAlso _
Val(InputChars(I)) < 256 _
AndAlso Val(InputChars(I)) > 0 Then
OutPutString += ChrW(Val(InputChars(I)))
Else
MsgBox("All input should be numeric, between 1-255," _
& " with values separated by a space.")
Exit Sub
End If
End If
Next
TextBox2.Text = OutPutString
End Sub
End Class
This will convert a string in hex to integer
Private Sub cbGo_Click(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles cbGo.Click
TextBox1.Text = HexCon(TextBox1.Text)
End Sub
Public Function HexCon(ByVal Instr As String) As String
If Instr.Length = 0 Then Return "No Input"
Dim str As String = Instr.ToUpper().Trim
Dim Power As ULong = CLng(str.Length) - 1
Dim BinNum As Long = 0
Dim Num As Long = 0
Dim Mul As ULong
Dim ptr As Byte
For I As Integer = CInt(Power) To -1 + 1 Step -1
Try
Num = System.Uri.FromHex(str.Substring(ptr, 1))
Catch
Return "Illegal input " + TextBox1.Text.Substring(ptr, 1)
End Try
Mul = CLng(System.Math.Pow(CDbl(16), CDbl(Power)))
BinNum += CLng((Num * Mul))
If Power > 0 Then Power -= 1
ptr += 1
Next
Return CStr(BinNum)
End Function
This will convert integer numbers into hex
Private
Sub cbGo_Click(ByVal sender As Object, ByVal e As System.EventArgs) _Handles
cbGo.ClickTextBox1.Text = DecHex(TextBox1.Text.Trim)
End SubPublic Function DecHex(ByVal Instr As String) As String
Try Return Hex(CInt(Instr)) Catch ex As Exception Return ex.Message End Try End Function
Please define your understanding of how you are using ascii. Do you means ascii byte strings
Textboxes are unicode and are not ascii at all. Do you want unicode string "AB" converted into a string that is "41 42"
The unicode string can certainly be broken into ascii and that the hex value can certainly displayed.
Pretty easily. This is the point I have been trying to make since the beginning of the thread. ....soon.