Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Other Programming > VBScript
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
VBScript For questions and discussions related to VBScript.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VBScript section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old May 9th, 2005, 03:45 PM
Registered User
 
Join Date: May 2005
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Unicode UTf-8? System.Text.UTF8Encoding from VBA?

I'm writing VBA code embedded in an Excel spreadsheet.

I need to convert strings from Unicode UTF-8 to standard string objects.

If you read the MSDN docs, it looks like the way is the object System.Text.UTF8Encoding. But it SEEMS like there is no way to use it from the VBA world. It seems like it might be Visual Basic.NET only, but not exported to the VBA world. Is that true?!?!?

How the heck do I do this?

I can create VBA code that does createobject("System.Text.UTF8Encoding") which runs fine, but then the object that it creates can't take any messages. :-(

  Dim myconverter
  Set myconverter = CreateObject("System.Text.UTF8Encoding") ' no error

  Dim encodedBytes
 encodedBytes = myconverter.GetBytes("sdsf") ' THIS FAILS - no GetBytes

There is some commercial developer who licenses some DLL to do this from VBA/VBscript, but that seems ridiculous, and wouldn't work in my architecture anyway -- i need it all to be contained within the Excel file.

Any ideas?!?!

Thanks!

Reply With Quote
  #2 (permalink)  
Old May 9th, 2005, 03:57 PM
Friend of Wrox
 
Join Date: Nov 2004
Location: Port Orchard, WA, USA.
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

Do you know what the difference is between UTF-8 format and a standard string?

In your snippet, you are using a literal string as the argument. Inasmuch as it is not UTF-8 encoded, I wouldn't expect that to fly, even if the creation of the object "MyConverter" did work.
Reply With Quote
  #3 (permalink)  
Old May 9th, 2005, 04:08 PM
Friend of Wrox
 
Join Date: Nov 2004
Location: Port Orchard, WA, USA.
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

If UTF-8 is unicode, and you want just the bytes of the ASCII characters, you can do this (built in conversion in VBA):
Code:
    Dim bString() As Byte
    Dim strSource As String
    Dim i         As Integer

    strSource = "ABC"          ' strSource contains 65  0  66  0  67  0
    bString() = strSource      ' bString contains   65  66  67

    For i = LBound(bString) To UBound(bString)
        bString(i) = bString(i) + 1
    Next i
    strSource = bString()      ' strSource contains 66  0  67  0  68  0

    Debug.Print strSource      ' Prints "BCD"
Reply With Quote
  #4 (permalink)  
Old May 10th, 2005, 04:34 PM
Registered User
 
Join Date: May 2005
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

If UTF-8 is unicode, and you want just the bytes of the ASCII characters, you can do this (built in conversion in VBA):

UTF-F is is a variable-character-length encoding of Unicode that pays careful attention to not confusing code that looks for chars 1-127 (classic ASCII), thus encoding Unicode chars in standard 8-bit strings, but all bytes with values 1-127 are what they appear to be.

Learn more here:
 http://en.wikipedia.org/wiki/Utf-8

So, the example you posted doesn't do what i need. I need to take a string that I have pulled into a string from a textstream.ReadLine() command and I need to convert it using UTF-8 decoder into a Unicode string decoded from the encoded string. For instance, a 10 byte (10 8-bit bytes) might convert into a 2-10 character Unicode string after variable-length UTF-8 decoding.

this decoding is built into VB, but seems to be offlimits to VBA clients and ASP? Is there no way to do this? BAsically, I need to use the System.Text.UTF8Encoding class from VBA and it seems like it isn't available from the VBA world.

Any ideas?

Reply With Quote
  #5 (permalink)  
Old May 23rd, 2005, 03:44 AM
Registered User
 
Join Date: May 2005
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,

Did you manage to do that? I am looking for a solution for exactly the same issue, but in Word VBA macro. I can't any way to convert UTF-8 encoded string to UNICODE.

Thanks

Reply With Quote
  #6 (permalink)  
Old March 30th, 2006, 01:46 PM
Registered User
 
Join Date: Mar 2006
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Take a look at http://anbedesigns.alojardominio.com. There you can get Utf82Unicode function.

Reply With Quote
  #7 (permalink)  
Old May 20th, 2011, 11:58 AM
Registered User
 
Join Date: May 2011
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default try this code

You try this snippet of code it worked for me....

Code:
Function UTF8_Decode(ByVal sStr As String)
    Dim l As Long, sUTF8 As String, iChar As Integer, iChar2 As Integer
    For l = 1 To Len(sStr)
        iChar = Asc(Mid(sStr, l, 1))
        If iChar > 127 Then
            If Not iChar And 32 Then ' 2 chars
            iChar2 = Asc(Mid(sStr, l + 1, 1))
            sUTF8 = sUTF8 & ChrW$(((31 And iChar) * 64 + (63 And iChar2)))
            l = l + 1
        Else
            Dim iChar3 As Integer
            iChar2 = Asc(Mid(sStr, l + 1, 1))
            iChar3 = Asc(Mid(sStr, l + 2, 1))
            sUTF8 = sUTF8 & ChrW$(((iChar And 15) * 16 * 256) + ((iChar2 And 63) * 64) + (iChar3 And 63))
            l = l + 2
        End If
            Else
            sUTF8 = sUTF8 & Chr$(iChar)
        End If
    Next l
    UTF8_Decode = sUTF8
End Function
Reply With Quote
  #8 (permalink)  
Old May 20th, 2011, 12:29 PM
Registered User
 
Join Date: May 2011
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default trying to read Japanese character set

I am reading some cells in Excel with VBA in Access. One of the cells contains Japanese characters that I need to populate in an Access Table.

If I understand things correctly, I need to read the cell from Excel as Binary then convert to UTF-8 in order to save in my Access table.

If this is correct, could you help me with the code to read the Cell from Excel with VBA as a Binary.

Pretty sure I can then pass this value to your UTF-8 converter to create the valid string.

thank you so much!!!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Retrieve System information by VBA powerz Access VBA 8 August 13th, 2013 03:49 PM
unicode text to image rajuru Pro PHP 0 November 9th, 2006 09:40 AM
System.Text.UTF8Encoding? Unicode convert?!? forum1 Excel VBA 0 May 9th, 2005 03:43 PM
Visual FoxPro, Rich Text control and Unicode Glamdring VS.NET 2002/2003 0 November 19th, 2004 06:15 AM



All times are GMT -4. The time now is 07:00 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.