Consulting

Results 1 to 4 of 4

Thread: Convert ASCII code back to text with StrConv + vbUnicode (VBA)

  1. #1
    VBAX Newbie
    Joined
    Feb 2023
    Posts
    2
    Location

    Convert ASCII code back to text with StrConv + vbUnicode (VBA)

    Dear Community,

    I would like to convert the number sequence (ASCII) 115 116 101 112 104 102 97 110 back to text (one word) with StrConv + vbUnicode. Unfortunately, I don't know how to use the For-Next loop in this case.

    I converted the text in the number sequence as follows (found with Google) - but I just can't get it back to text. Can someone please help me or give me some tips. Many thanks and best regards Stephfan

    Sub zuASCII()
    Dim str1 As String, str2 As String
    Dim i As Integer
    Dim x() As Byte
    str1 = Cells(1, 1).Value                 '(in dieser Zelle steht der Text)
    x = StrConv(str1, vbFromUnicode)
    For i = 0 To UBound(x)
      str2 = str2 & " " & x(i)
    Next i
    Cells(1, 2).Value = Trim(str2)
    End Sub
    Last edited by Aussiebear; 02-01-2023 at 03:46 PM. Reason: Added code tags to supplied code

  2. #2
    ' arnelgp
    Sub zuASCII()
        Dim str1 As String, str2 As String
        Dim i As Integer
        Dim x() As Byte
        str1 = Cells(1, 1).Value                 '(in dieser Zelle steht der Text)
        ' assign str1 to x
        x = str1
        ' now X will contain 115,0,116,0,101,0...(ascii seperated with "0")
        '
        ' loop through each element of byte array
        For i = 0 To UBound(x) Step 2
            Debug.Print x(i)
        Next
        '
        ' convert the array of byte again to string
        str2 = x
        Debug.Print str2
    End Sub
    note: the Loop (For i = 0...) is not necessary, it is only there to show the values of x (Array of bytes).
    Last edited by arnelgp; 02-01-2023 at 08:35 PM. Reason: explain further

  3. #3
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    Another way


    Option Explicit
    
    Sub Test()
        Dim sInput As String, sOutput As String
        Dim i As Long
        Dim arySplitInput As Variant
        
        sInput = ActiveSheet.Range("A1")        '   115 116 101 112 104 102 97 110
    
    
        arySplitInput = Split(sInput, " ")
        
        For i = LBound(arySplitInput) To UBound(arySplitInput)
            sOutput = sOutput & ChrW(arySplitInput(i))
        Next i
        
        sOutput = StrConv(sOutput, vbUnicode)
        
        Debug.Print sOutput
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  4. #4
    VBAX Newbie
    Joined
    Feb 2023
    Posts
    2
    Location
    thank you very much for your help :-)

Tags for this Thread

Posting Permissions

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