Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to convert Unicode characters and vice versa ? 1

Status
Not open for further replies.

crackoo

Programmer
Feb 17, 2011
132
TN
I have this VBScript that converts the characters in ANSI code.
I want to do the same thing but by converting it to Unicode and vice versa!
-Is there a function that can make this transformation ? I have no idea about this ,
Thank you for your help !
Code:
Dim i,x,a 
i = InputBox("Entrer un caractére ou une phrase pour obtenir son Code ANSI Correspondant !") 
If i <> "" Then 
For x = 1 To Len(i) 
If x <> Len(i) Then 
a = a & "Chr(" & Asc(Mid(i,x,1)) & ")" & "&" 
Else 
a = a & "Chr(" & Asc(Mid(i,x,1)) & ")" 
End if 
Next 
Inputbox "Le Code ANSI Correspondant pour " & qq(i) & " est:",,a 
End If 
Function qq(strIn)
    qq = Chr(34) & strIn & Chr(34)
End Function
 
I'm pretty sure VBScript's InputBox() function already accepts Unicode text, and String values are Unicode anyway.

That so-called "conversion" there is just stripping Unicode to ANSI with Asc() and then giving you back the ANSI converted back to its Unicode equivalent with Chr(), losing fidelity in the process.

Avoid the ANSI functions like Chr(), Asc(), etc. Use ChrW(), AscW(), and so on instead.

It is all in the documentation.
 
Thank you for your helpful Reply !
and the code become after your suggestion like that :
Code:
Dim i,x,a 
i = InputBox("Entrer un caractére ou une phrase pour obtenir son Code Unicode Correspondant !") 
If i <> "" Then 
For x = 1 To Len(i) 
If x <> Len(i) Then 
a = a & "ChrW(" & AscW(Mid(i,x,1)) & ")" & "&" 
Else 
a = a & "ChrW(" & AscW(Mid(i,x,1)) & ")" 
End if 
Next 
Inputbox "Le Code Unicode Correspondant pour " & qq(i) & " est:",,a 
End If 
Function qq(strIn)
    qq = Chr(34) & strIn & Chr(34)
End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top