Chinese character in csv file with javascript code

Dear Expert,

I get one csv file in which has some chinese character and use following javascript code
to form a table. the table is displayed for number and Engish value is fine but it doesn't
display correct chinese character according to the cells postion in csv file. it display a strange output on
some cells. I heard people said javascript is not able to handle Chinese character or
other country character. Is it True? Is ther any method to overcome this?

Whether I need to convert the chinese character into unicode first and then put it back int  csv file before
any javascript processing ?
http://code.cside.com/3rdpage/us/unicode/converter.html
Please advise

Duncan
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script language="javascript">
	function getData( url ) 
	{ 
		var xmlhttp;
		
		if (window.XMLHttpRequest)
		{// code for IE7+, Firefox, Chrome, Opera, Safari
			xmlhttp=new XMLHttpRequest();
		}
		else
		{// code for IE6, IE5
			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
		
		xmlhttp.onreadystatechange=function()
		{
			if (xmlhttp.readyState==4 && xmlhttp.status==200)
			{
				renderData( xmlhttp.responseText );
			
			}
		}
		xmlhttp.open("GET","postcodes.csv",true);
		xmlhttp.send();
	}

	function renderData( data )
	{
		var dataRows = data.split( "\n" );
		var table = document.createElement("Table");
		table.border = 0.5;
		
		for( i = 0; i < dataRows.length; i++ )
		{
			var tableRow = table.insertRow(i);
			var dataCells = dataRows[i].split(",");
			for( j = 0; j < dataCells.length; j++ )
			{
				var tableCell= tableRow.insertCell(j);      
				tableCell.innerHTML = dataCells[j];
			}
		}
		document.body.appendChild( table );
	} 

	getData();

</script>
</head>
<body>
</body>
</html> 
Toggle HighlightingOpen in New WindowSelect AllAccept Multiple Solutions Accept as Solution

Open in new window

LVL 13
duncanb7Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jimyXCommented:
Please have a look on this article:
http://blogs.sun.com/shankar/entry/how_to_handle_utf_8
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Michel PlungjanIT ExpertCommented:
The file needs to be saved as UTF8 and the page you are showing it in too. Alternatively convert to unicode \uxxxx which JS should be able to show regardless of page encoding
0
duncanb7Author Commented:
I goolge it for whole day, finally get the method to convert csv file into UFT-8 in vba and
find the linke at
http://code.cside.com/3rdpage/us/unicode/converter.htmlSub csvtoUTF8()
the book9.csv(after encode)  is still not matched from note++ to the link conversion above.
What is &#  stand for, it mean UTF code  ?
What is \u  stand for,  it means  classical Unicode ?

Question-1, I use notepad to view the conversion file of boo9.csv with option to UTF8
 the hex code in notepad  is different from hex code with &# and \u , Why ?
QUestion-2   Since by question-1 problem, so I try to convert one character  from
the link, and then use decodeURIComponent( escape(&#xxxx ) ) it works and display right character in javascript
but decodeURIComponent( escape(\uxxxxx ) ) is not displayed the correct character,
What is stand for HTML Unicode and Java Unicode in the link ?
Question-3, SHould I set <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
in my javascript ?


VBA code for book8.csv convert to book9.csv in UTF-8
===========================================
Dim sfol As String
Dim dfol As String
sfol = ActiveWorkbook.path & "\"
dfol = sfol
Call CopyFile2File(sfol, "book8.csv", dfol, "book9.csv")
Call Encode(sfol & "book9.csv", "UTF-8")

End Sub
Sub Encode(ByVal sPath$, Optional SetChar$ = "UTF-8")
With CreateObject("ADODB.Stream")
.Open
.LoadFromFile sPath ' Loads a File
.Charset = SetChar ' sets stream encoding (UTF-8)
.SaveToFile sPath, 2 ' adSaveCreateOverWrite
.Close
End With
End Sub
0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

duncanb7Author Commented:
RE-send it for typing mistake , I goolge it for whole day, finally get the method to convert csv file into UFT-8 in vba and
find the linke at
http://code.cside.com/3rdpage/us/unicode/converter.html
the book9.csv(after encode)  is still not matched from note++ to the link conversion above.
What is &#  stand for, it mean UTF code  ?
What is \u  stand for,  it means  classical Unicode ?

Question-1, I use notepad to view the conversion file of boo9.csv with option to UTF8
 the hex code in notepad  is different from hex code with &# and \u , Why ?
QUestion-2   Since by question-1 problem, so I try to convert one character  from
the link, and then use decodeURIComponent( escape(&#xxxx ) ) it works and display right character in javascript
but decodeURIComponent( escape(\uxxxxx ) ) is not displayed the correct character,
What is stand for HTML Unicode and Java Unicode in the link ?
Question-3, SHould I set <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
in my javascript ?


VBA code for book8.csv convert to book9.csv in UTF-8
===========================================
Sub csvtoUTF8()

Dim sfol As String
Dim dfol As String
sfol = ActiveWorkbook.path & "\"
dfol = sfol
Call CopyFile2File(sfol, "book8.csv", dfol, "book9.csv")
Call Encode(sfol & "book9.csv", "UTF-8")

End Sub
Sub Encode(ByVal sPath$, Optional SetChar$ = "UTF-8")
With CreateObject("ADODB.Stream")
.Open
.LoadFromFile sPath ' Loads a File
.Charset = SetChar ' sets stream encoding (UTF-8)
.SaveToFile sPath, 2 ' adSaveCreateOverWrite
.Close
End With
End Sub
0
duncanb7Author Commented:
I read it before, and now I understand &#aaaa, where a is for decimal code, \ubbbb, where b is for hex code
the question how I convert it to &# or \u  in vba  so, the text file will
be

After new conversion, book9.csv will be like this
=================================
Hello, &#23980;&#21110;&#21340, good, day,
1,2,3,4,4
5,6,7,8

================================

In other words, I use above function of csvtoUTF8() is incorrect. ?
0
duncanb7Author Commented:
But how to convert chinese charter to &#aaaaa in vba ?
0
duncanb7Author Commented:
Finally, get the function to convert the chinese character to Unicode

http://www.blog.highub.com/javascript/javascript-advanced/convert-chinese-characters-to-unicode/
0
duncanb7Author Commented:
Sorry it is in javascript not in vba

Could you help provide a code for Vba
0
Michel PlungjanIT ExpertCommented:
No. Sorry. I added the VBA zone for you
0
duncanb7Author Commented:
Thanks,

I think I could do VBA code accroding the javascript code provide by myself
but others can reply it, it is really welecom and thanks

Duncan
0
duncanb7Author Commented:
Thanks for your reply

If I put the &# Unciode in csv file
like this, it display back all
chinese character by Javascript
Hello,&#23980;&#21110;&#21340
good, day
1,2,3,4,4
5,6,7,8
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.