PDA

View Full Version : Convert ASCII code back to text with StrConv + vbUnicode (VBA)



stephfan
02-01-2023, 03:35 PM
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

arnelgp
02-01-2023, 05:46 PM
' 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).

Paul_Hossler
02-01-2023, 08:24 PM
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

stephfan
02-02-2023, 09:35 AM
thank you very much for your help :-)