Click here to Skip to main content
13,248,927 members (44,254 online)
Click here to Skip to main content
Add your own
alternative version


7 bookmarked
Posted 11 Mar 2008

Working with UTF8 characters...

, 11 Mar 2008
Rate this:
Please Sign up or sign in to vote.
it is not difficult to work with UTF 8 characters anymore...


Some times situation is like we have to parse the string which has both Unicode and ASCII characters in single string, at that time Encoding functions in .NET will not be help ful,
so i have created two use full functions for that situations...


this article requires some headache with working with UTF 8 characters. just kidding..

you should know the string formate in .NET and how one character is recognize as unicode in 16 bits(2 bytes)

Using the code

These two function will solve our problem fro converting UTF characters to bytes and from bytes to UTF characters.
public static string GetUTF8StringFrombytes(byte[] byteVal)


            byte[] btOne = new byte[1];

            StringBuilder sb = new StringBuilder("");

            char uniChar;

            for (int i = 0; i < byteVal.Length; i++)


                btOne[0] = byteVal[i];

                if (btOne[0] > 127)


                    uniChar = Convert.ToChar(btOne[0]);






            return sb.ToString();


        public static byte[] GetBytesFromUTF8Chars(string strVal)


            if (strVal != string.Empty || strVal != null)


                byte btChar;

                byte[] btArr = new byte[strVal.Length * 2];

                byte[] tempArr;

                int arrIndex = 0;

                for (int i = 0; i < strVal.Length; i++)


                    btChar = (byte)strVal[i];

                    if (btChar > 127 && btChar < 256)


                        btArr[arrIndex] = btChar;





                        tempArr = Encoding.UTF8.GetBytes(strVal[i].ToString());

                        Array.Copy(tempArr, 0, btArr, arrIndex, tempArr.Length);

                        arrIndex += tempArr.Length;

                        tempArr = null;



                byte[] retVal = new byte[arrIndex];

                Array.Copy(btArr, 0, retVal, 0, arrIndex);

                return retVal;



                return new byte[0];


Points of Interest

Just providing some solution which i have found while have problem, so that other will not face them.


If any improvements are suggested then they are welcomes.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer
India India
Chirag Patel, a Programmer Analyst in a well known IT company working on .NET Technologies since last 2 years. He is interested in Pure business logic and fuzzy logic. his area of interest is in C#.NET, VB.NET and MSSQL 2005.

catch me on:

You may also be interested in...

Comments and Discussions

GeneralIncorrect Pin
Jonathan [Darka]11-Mar-08 9:28
memberJonathan [Darka]11-Mar-08 9:28 
GeneralRe: Incorrect Pin
JustChiragPatel11-Mar-08 18:26
memberJustChiragPatel11-Mar-08 18:26 
GeneralDon't use that Pin
TheDarkMan11-Mar-08 5:34
memberTheDarkMan11-Mar-08 5:34 
GeneralRe: Don't use that Pin
JustChiragPatel11-Mar-08 18:30
memberJustChiragPatel11-Mar-08 18:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171114.1 | Last Updated 11 Mar 2008
Article Copyright 2008 by JustChiragPatel
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid