Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > VB Script

Vista - Converting text between various encodings

Reply
 
Old 03-17-2009   #1 (permalink)
Paul Randall


 
 

Converting text between various encodings

Hi,
I'm playing with converting text strings between various encodings like
Unicode and UTF8 and UTF7. One tool I've found that seems to do at least
some of this is the oleprn.olecvt object (on W2K and later I think) which
has ToUnicode and ToUtf8 methods. TLViewer shows that the ToUnicode method
takes two arguments, a string to be converted and a long integer
representing the codepage of the string to be converted (I think), and
returns a Unicode string. It shows that the ToUtf8 method takes one
argument, apparently a little endian Unicode string.

I'm hoping that someone will post a url for some documentation on this
oleprn.olecvt object. TLViewer indicates a lot of functionality, with
coclasses having a lot of printer functionality. But I'm mostly interested
in whether the ToUnicode method can convert only from UTF8 or from any
encoding for which my computer has a codepage number. Maybe this function:
Function fsUTF8ToUnicode(sUTF8String, lCodePage)
should really be called an any-encoding to Unicode routine.

Anyhow, here is a simple script that converts a Unicode character to UTF8
and back to Unicode:

Option Explicit
Dim sUnicode: sUnicode = ChrW(&H2018)
Dim sUTF8, sNewUnicode
sUTF8 = fsUnicodeToUTF8(sUnicode)

MsgBox "Original Unicode = " & sUnicode & _
" (&H" & Hex(ascW(sUnicode)) & ")" & _
vbcrlf & "UTF8: " & sUTF8

sNewUnicode = fsUTF8ToUnicode(sUTF8, 65001)

MsgBox "Original Unicode = " & sUnicode & _
" (&H" & Hex(ascW(sUnicode)) & ")" & _
vbcrlf & "UTF8: " & sUTF8 & vbCrLf & _
"New Unicode = " & sNewUnicode & _
" (&H" & Hex(ascW(sNewUnicode)) & ")"

Function fsUnicodeToUTF8(sUnicodeString)
fsUnicodeToUTF8 = CreateObject("OlePrn.OleCvt")._
ToUtf8(sUnicodeString)
End Function 'fsUnicodeToUTF8(sUnicodeString)

'list of codepage numbers for various encodings.
'http://www.motobit.com/help/scptutl/cl68.htm
Function fsUTF8ToUnicode(sUTF8String, lCodePage)
fsUTF8ToUnicode = CreateObject("OlePrn.OleCvt")._
ToUnicode(sUTF8String, lCodePage)
End Function 'fsUTF8ToUnicode(sUTF8String, lCodePage)

-Paul Randall



My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Howto: Add lines of text from a specific point in a text file.. VB Script
Converting audio into text Vista General
How do I read a text file and sort text by fixed positions? PowerShell
Other encodings in powershell? PowerShell
Cmdlets and Supporting Encodings PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46