Important alert: (current site time 7/25/2014 11:37:15 AM EDT)
 

winzip icon

UTF-8 Encoder/Decoder

Email
Submitted on: 4/7/2003 2:32:23 PM
By: Divide By Zero 
Level: Beginner
User Rating: By 22 Users
Compatibility: VB 6.0
Views: 32801
author picture
 
     Encodes/Decodes text with a given codepage such as UTF-8/UTF-7 etc. Can be easily altered to be used in some notepad application.

 

Windows API/Global Declarations:

Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
'**************************************
'Windows API/Global Declarations for :UTF-8 Encoder/Decoder
'**************************************
Private Declare Function GetACP Lib "Kernel32" () As Long
Private Declare Function GetLocaleInfoA Lib "Kernel32" (ByVal Locale As Long, ByVal LCType As Long, ByVal lpLCData As String, ByVal cchData As Long) As Long
Private Declare Function GetSystemDefaultLCID Lib "Kernel32" () As Long
Private Declare Function IsWindowUnicode Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function TranslateCharsetInfo Lib "gdi32" (lpSrc As Long, lpcs As CHARSETINFO, ByVal dwFlags As Long) As Long
Private Declare Function MultiByteToWideChar Lib "Kernel32" (ByVal CodePage As Long, ByVal dwFlags As Long, ByVal lpMultiByteStr As Long, ByVal cchMultiByte As Long, ByVal lpWideCharStr As Long, ByVal cchWideChar As Long) As Long
Private Declare Function WideCharToMultiByte Lib "Kernel32" (ByVal CodePage As Long, ByVal dwFlags As Long, ByVal lpWideCharStr As Long, ByVal cchWideChar As Long, ByVal lpMultiByteStr As Long, ByVal cchMultiByte As Long, ByVal lpDefaultChar As Long, lpUsedDefaultChar As Long) As Long
Private Declare Function MsoCpgFromLid Lib "c:\program files\common files\microsoft shared\vba\mso97rt.dll" Alias "#307" (ByVal lid As Long) As Long
Private Declare Function MsoMultiByteToWideChar Lib "c:\program files\common files\microsoft shared\vba\mso97rt.dll" Alias "#778" (ByVal CodePage As Long, ByVal dwFlags As Long, ByVal lpMultiByteStr As Long, ByVal cchMultiByte As Long, ByVal lpWideCharStr As Long, ByVal cchWideChar As Long) As Long
Private Declare Function MsoWideCharToMultiByte Lib "c:\program files\common files\microsoft shared\vba\mso97rt.dll" Alias "#915" (ByVal CodePage As Long, ByVal dwFlags As Long, ByVal lpWideCharStr As Long, ByVal cchWideChar As Long, ByVal lpMultiByteStr As Long, ByVal cchMultiByte As Long, ByVal lpDefaultChar As Long, lpUsedDefaultChar As Long) As Long
winzip iconDownload code

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. Afterdownloading it, you will need a program like Winzip to decompress it.Virus note:All files are scanned once-a-day by Planet Source Code for viruses, but new viruses come out every day, so no prevention program can catch 100% of them. For your own safety, please:
  1. Re-scan downloaded files using your personal virus checker before using it.
  2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.
  3. Scan the source code with Minnow's Project Scanner

If you don't have a virus scanner, you can get one at many places on the net including:McAfee.com

 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.


Other 1 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

4/7/2003 7:28:47 PM

if this works, 5 globes
(If this comment was disrespectful, please report it.)

 
4/7/2003 7:57:41 PM

oh my god. I love you man!!!!!!!!!!!!! 5 globes!!!!!
(If this comment was disrespectful, please report it.)

 
4/8/2003 10:07:05 AMMrEnigma

I have been looking for this for a very very long time
(If this comment was disrespectful, please report it.)

 
4/8/2003 11:32:41 AM

I'm glad it was of some use to someone else! I made this because I'm making an IRC Client for MSN Chat and their servers send users nicknames encoded in utf-8, and it needed to be *fast* and is why I did it using API's.
(If this comment was disrespectful, please report it.)

 
6/18/2003 4:49:11 PMPaw Jershauge

D.a.m.n. greate job... im working on a multi MSN an like u said it uses UTF-8. right now im having 18 diffrent messenger logons running on the same computer.. lol i was wondering if you have an resept on the MD5 crypting... because im working with an dll right now, but i want to have it in my own program... ;o)
(If this comment was disrespectful, please report it.)

 
6/18/2003 4:51:06 PMPaw Jershauge

ohh and by the way 5 stars from me ;o)
(If this comment was disrespectful, please report it.)

 
8/4/2003 3:54:09 AM

I have a trouble , How can I save a UTF-8 file ? becauce when I encode a Chinese character file for XML by Binary model to creative the text file , XML can't decode it , why?
(If this comment was disrespectful, please report it.)

 
10/21/2003 4:35:26 AM

I have tested this application,suddenly,I found that when i entered a string like this in chinese:"ͼ1",encode it with UTF-8 standard,then the result is "机调?"
and i tried to decode it,but the decode result is "?" in chinese.what's the matter?then.Will you be so kind to help me find out the reason?
(If this comment was disrespectful, please report it.)

 
10/21/2003 5:06:13 AM

I have use your code to write text file in UTF-8 encoding standard,but I still find there's difference between your method and the WriteUTF() method in JAVA,I really appreciate you of your good kindness,I think, there's somewhere still need to be improved,
DO YOU THINK SO?IF YOU HAVE TIME,CONTACT ME AS SOON.THANKS
ziliujiang@163.com

(If this comment was disrespectful, please report it.)

 
10/24/2003 2:43:48 PMDivide By Zero

This was just a quick project for when I was testing out basic utf-8 functionality. People that have any problems using this with files make sure you check the BOM [Byte Order Mark](first 3 characters of the file) for the encoding type.. You don't necessarily need this information so you can just ignore/skip the first 3 bytes of the file, unless you create the source to auto-detect and change the encoding/decoding automatically.

ziliujiang, This will most likely be the problem you have found.. just write the first 3 bytes of the encoding type first and it should be fine. I didn't do this as I said it was just a little tester program to see if I could get utf-8 working through windows api's.
You will find out what this Byte Order Marking is if you load up a utf-8 encoded file into a hex editor or something... the first 3 characters should be the BOM for the type of encoding used for that particular file.
(If this comment was disrespectful, please report it.)

 
5/16/2004 6:55:04 AMMahesh

5 G's for this.
How can we automate the pasting.
I am trying to use your code to convert from rtf (Arabic) to Unicode-8. It works fine when I copy from the richtextbox and paste into your textbox. But how to automate it?
(If this comment was disrespectful, please report it.)

 
5/18/2004 5:47:34 PMDivide By Zero

I no longer use any version of visual basic, I'm sorry to those I cannot help but I don't have as much time as I used too.

Again, appologies.
(If this comment was disrespectful, please report it.)

 
5/18/2004 5:48:37 PMDivide By Zero

Also, thanks for the votes and I'm glad this little app was of 'some' use to people :)
(If this comment was disrespectful, please report it.)

 
11/4/2004 7:33:25 PM

I dont see where to download it or how to use it...
(If this comment was disrespectful, please report it.)

 
11/8/2004 12:46:47 AMDivide By Zero

Clicking the little winzip icon might work my friend ;)
(If this comment was disrespectful, please report it.)

 
7/2/2005 1:05:58 AM

Any ideas for show the decode in richtexbox it no show all character some just show ??? any help ty
(If this comment was disrespectful, please report it.)

 
8/23/2005 3:41:52 AMAurlien

A really very good code ! congratulation !
Just a little PB, when u try to translat into a language not installed on Windows, u'r code go in error...this is a little thing, but it's easy to intercept this error and put a message...or, better, find the good API in windows to can laught the languages installer ;o)
but it's a good job !
(If this comment was disrespectful, please report it.)

 
9/15/2005 1:54:56 PMJames

Where can I download this code? I clicked on the zip icon and it moves the page over (???), no code starts downloading.
Thanks
(If this comment was disrespectful, please report it.)

 
11/4/2005 6:09:26 PMNaji A. Alnimr

Will
Good Job But I Need Help TO convert some tables were made in foxpro 2.5 in MS-DOS used the MA20 to write in arabic it is converting
and we can PAY you for your help you can mail me for that on
najixp@yahoo.com
(If this comment was disrespectful, please report it.)

 
4/6/2007 10:06:04 AMDoug Purinton

Hey, I'm trying to make a MDI notepad of some sort and but this line of code (Private Declare Function TranslateCharsetInfo Lib "gdi32" (lpSrc As Long, lpcs As CHARSETINFO, ByVal dwFlags As Long) As Long) doesn't pass the compiler. Could you tell me what I need to make my program more like notepad (encoding wise) thanks!
(If this comment was disrespectful, please report it.)

 
1/3/2008 6:27:32 PMAndRAY

Function EncodeUTF8 returns a string which is twice as long as it should be. There's a lot of Chr(0) at the end of line.
Public Function EncodeUTF8(ByVal cnvUni As String) As String
If cnvUni = vbNullString Then Exit Function
Dim ans As String
ans = WToA(cnvUni, CP_UTF8)
EncodeUTF8 = Mid(StrConv(ans, vbUnicode), 1, Len(ans))
End Function
(If this comment was disrespectful, please report it.)

 
7/7/2012 12:36:34 PMBlazer

For anyone with the chr(0) issues at the end replace the EncodeUTF8 with this

Function EncodeUTF8(ByVal cnvUni As String)
If cnvUni = vbNullString Then Exit Function
EncodeUTF8 = (WToB(cnvUni, sCodePage, 0))
EncodeUTF8 = StrConv(EncodeUTF8, vbUnicode)
EncodeUTF8 = Left$(EncodeUTF8, Len(EncodeUTF8) * 0.5)
End Function
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.