Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
194
rated 0 times [  199] [ 5]  / answers: 1 / hits: 33488  / 12 Years ago, thu, june 7, 2012, 12:00:00

I have an array containing strings with special unicode characters:



var a = [
[a, 33],
[hu016B, 44],
[su00EF, 51],
...
];


When I loop over this array:



for (i=0;i<a.length;i++) {
document.write(a[i][0] + <br />);
}


It prints characters with accents:



a


...


and I want:



a
hu016B
su00EF
...


How can I achieve this in Javascript?


More From » unicode

 Answers
74

Something like this?



/* Creates a uppercase hex number with at least length digits from a given number */
function fixedHex(number, length){
var str = number.toString(16).toUpperCase();
while(str.length < length)
str = 0 + str;
return str;
}

/* Creates a unicode literal based on the string */
function unicodeLiteral(str){
var i;
var result = ;
for( i = 0; i < str.length; ++i){
/* You should probably replace this by an isASCII test */
if(str.charCodeAt(i) > 126 || str.charCodeAt(i) < 32)
result += \u + fixedHex(str.charCodeAt(i),4);
else
result += str[i];
}

return result;
}

var a = [
[a, 33],
[hu016B, 44],
[su00EF, 51]
];

var i;
for (i=0;i<a.length;i++) {
document.write(unicodeLiteral(a[i][0]) + <br />);
}


Result



a
hu016B
su00EF


JSFiddle


[#85079] Wednesday, June 6, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
mira

Total Points: 460
Total Questions: 108
Total Answers: 99

Location: American Samoa
Member since Fri, Aug 26, 2022
2 Years ago
mira questions
;