Last Comment

Calendar

July 2010
SunMonTueWedThuFriSat
 << < > >>
    123
45678910
11121314151617
18192021222324
25262728293031

Who's Online?

Member: 0
Visitor: 1

Announce

rss Syndication

03 Dec 2007 - 06:53:24 am
Working with UTF8 characters...
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 usefull functions for that situations...

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]);
                    sb.Append(uniChar);
                }
                else
                    sb.Append(Encoding.UTF8.GetString(btOne));
            }
            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;
                        arrIndex++;
                    }
                    else
                    {
                        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;
            }
            else
                return new byte[0];
        }
justchirag · 117 views · 0 comments
Categories: Functions
Tags: Tags ASCII Tags encoding Tags UTF8

Permanent link to full entry

http://patelslab.yourliveblog.com/Patel-s-Lab-b1/Working-with-UTF8-characters-b1-p15.htm

Comments

No Comment for this post yet...


Leave a comment

New feedback status: Awaiting moderation





Your URL will be displayed.

 
Please enter the code written in the picture.


Comment text

Options
   (Set cookies for name, email and url)