Natty_Dreed

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



Re: Visual Basic Express Edition [Help] Convert ASCII To Hex

Tall Dude

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






Re: Visual Basic Express Edition [Help] Convert ASCII To Hex

Natty_Dreed

Yeah Like What I Want
but I can Convert Only One character
how Can I Convert more Than one character
And By The Way Thank You... Wink





Re: Visual Basic Express Edition [Help] Convert ASCII To Hex

ReneeC

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.4

TextBox1.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.






Re: Visual Basic Express Edition [Help] Convert ASCII To Hex

Natty_Dreed

Ok

I will explain what I want

I have Two TextBox's [ TextBox1 , TextBox2 ] And Button1

In TextBox1 The user Will Give Me An Decimal number He Want it Back To TextBox2 As String

For Example

TextBox1 = 65 -> Click On Button1 -> TextBox2 = A



----------------------

I Can Do That And Thank's To [ Tall Dude ]

But The Problem is When The User Give Me More Than One Char it's not Convert It

TextBox1 = 6567 -> Click On Button1 -> TextBox2 = A


Why This Happen. I don't know ;(




Re: Visual Basic Express Edition [Help] Convert ASCII To Hex

Solitaire

That's because ASCII values are 8-bit numbers and only go up to 255. You will have to validate the input in the textbox to restrict the entry to within that range.






Re: Visual Basic Express Edition [Help] Convert ASCII To Hex

ReneeC

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.






Re: Visual Basic Express Edition [Help] Convert ASCII To Hex

Tall Dude

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






Re: Visual Basic Express Edition [Help] Convert ASCII To Hex

ReneeC

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.






Re: Visual Basic Express Edition [Help] Convert ASCII To Hex

Natty_Dreed

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

I don't Know How To Thank You, and every body here
But I'm wondering Can I Do The Same With
ASCII To Decimal & ASCII To Hex

PS: Don't Bother Your Self with me.





Re: Visual Basic Express Edition [Help] Convert ASCII To Hex

ReneeC

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.Click

TextBox1.Text = DecHex(TextBox1.Text.Trim)

End Sub

Public Function DecHex(ByVal Instr As String) As String

Try

Return Hex(CInt(Instr))

Catch ex As Exception

Return ex.Message

End Try

End Function






Re: Visual Basic Express Edition [Help] Convert ASCII To Hex

Natty_Dreed

Thank's But I have already In My code
What I want now Is
ASCII To Hex
ASCII To Decimal
And Thank You Again





Re: Visual Basic Express Edition [Help] Convert ASCII To Hex

ReneeC

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.






Re: Visual Basic Express Edition [Help] Convert ASCII To Hex

Natty_Dreed

I'm Sorry Embarrassed
I'm Meaning String
Like What You Said
AB = 41 42 <- In Hex
AB = 65 66 <- In Decimal
Is there Way Can Make It Done





Re: Visual Basic Express Edition [Help] Convert ASCII To Hex

ReneeC

Pretty easily. This is the point I have been trying to make since the beginning of the thread. ....soon.