Results 1 to 4 of 4

Thread: Text to Ascii

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2009
    Posts
    0

    Text to Ascii

    Well I just started learning VB, as I started a course at school. So far everything is going good and I'm loving it, and trying out different things.

    I'm looking for any tips on how to convert entire sentences or even whole text files to ASCII, or from ASCII back to text.

    Bits of code for my simple converter/generator:

    Code:
    Private Sub Command1_Click()
    Rem generates an ASCII list of the codes ranging from 65 to 126
    
    Dim n As Integer
    Dim a As Integer
            picError.Cls
            Picture1.Cls
        If a = 0 Then
        For n = 65 To 126                    'start of the iteration / the loop starts
            Picture1.Print n, Chr(n)         'this is the action that repeats
            Next                             'end of iteration (loop)
    
            picError.Print "You have generated ASCII "
            picError.Print "Codes, make sure to click"
            picError.Print "Clear before you generate "
            picError.Print "them again!"
            a = 1
    
        End If
    If chkGenerateError = True Then ' checks whether the hidden OptionButton is set to true or false
        picError.Cls
        Picture1.Cls
        picError.Print "You have not cleared the"           ' if the button is set to true then it prints an error in the picture box
        picError.Print "boxes, please clear the boxes"
        picError.Print "before generating ASCII"
        picError.Print "codes again!"
    End If
        chkGenerateError.Value = True           ' sets the optionbutton to true allowing an error to print next time the ascii button is pressed without clearing
    chkExit = 0
    End Sub
    Code:
    Private Sub txtConverter_KeyPress(KeyAscii As Integer)
    Rem this piece of code is the basis of the key to ascii converter. it can be changed by changing Asc() with Chr(), which will then print the key for the ascii number
            picConvert.Cls
        If KeyAscii = 13 Then
            picConvert.Print Asc(txtConverter)  ' uses Asc(txtConverter) to convert a letter or symbol in the text box to it's corresponding ASCII code
        End If                                  ' use Chr(txtConverter) to swap it around and convert ascii codes to a letter or symbol
                                                ' you will need to change Label3 and Label5's caption value around for it to make sense, but it is not strictly neccessary for it function
    End Sub
    It's not completely finished or tidied up and I've got a heap of comments stuck in there for reminders, but it shows what I've got done. As you can see from the second code box it lets me convert a singular text or ASCII code back and forth, but I can't figure out how to get whole sentences working. If anyone would be kind enough to show me some examples or give some tips it would be very much appreciated!

    Thanks in advance.

  2. #2
    Frenzied Member Jim Davis's Avatar
    Join Date
    Mar 2001
    Location
    Mars base one Username: Jim Davis Password: yCrm33
    Posts
    1,284

    Re: Text to Ascii

    Hello!

    Why your posts counter is zero? However, welcome to the forums.
    There is a quite simple way, to convert a string data into ascii codes, by converting it to a byte array.

    Code:
    Dim aASC() As Byte, sText As String, i As Long
    
    sText = "Let me guess"                          'Test string.
    aASC = StrConv(sText, vbFromUnicode)   'Convert it. It will not be a unicode, but a simple byte array as well.
    
    For i = 0 To UBound(aASC)
      Debug.Print Mid$(sText, i + 1, 1) & " = " & CStr(aASC(i)) 'print out the letter and its corresponding byte value, that is the asc() value as well.
    Next
    
    'and now, you can convert the byte array back to a plain string like this:
    
    sText = StrConv(aASC, vbUnicode)
    Debug.Print sText
    So, strConv is a very usefull method for this kind of job as well. Just use vbFromUnicode to split into byte array, then vbUnicode to 'glue' it into a string.

  3. #3
    Addicted Member
    Join Date
    Jan 2009
    Posts
    233

    Re: Text to Ascii

    try this..

    it will convert the string at text1 to ascii and put the ascii data to text2
    you need 2 textboxes and a command button on a form..

    Code:
    Private Sub Command1_Click()
    
    Dim xstr, ystr As String
    Dim xnum As Integer
    Dim inum As Long
    
    
    xnum = Len(Trim(Text1.Text))
    ystr = Mid(Text1.Text, inum, 1)
     
    
     
    For inum = 1 To xnum
    ystr = Mid(Text1.Text, inum, 1)
    xstr = Asc(ystr)
    Text2.Text = Text2.Text & xstr
    Next
    
    
    End Sub

  4. #4
    Frenzied Member Jim Davis's Avatar
    Join Date
    Mar 2001
    Location
    Mars base one Username: Jim Davis Password: yCrm33
    Posts
    1,284

    Re: Text to Ascii

    Dim xstr, ystr As String
    In this declare the xstr will be a Variant type. I would recommend you to use a long, byte or integer types, instead of variant, because variant is one of the slowest value type.

    Code:
    Dim xstr As Long, ystr As String
    
    
    '...
    
    xstr = Asc(ystr)
    Text2.Text = Text2.Text & CStr(xstr)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width