/    Sign up×
Community /Pin to ProfileBookmark

convert from text to utf-8 vice versa

Hi guys,

For instance, I enter Chinese character:[B]路[/B] in google search then i will see one of it parameter with the url like this:

[B]q=%E8%B7%AF[/B]

which is i guess is utf-8 based 16.

any anyone know any javascript can convert [B]路 -> %E8%B7%AF[/B] and when this parameter pass to the other page, the page will decode the [B]%E8%B7%AF -> 路[/B] and redisplay on the textbox filed again. Any idea on this? Pls help, Thanks !

Thanks & Regards,
Plant more tree
Cut carbon usage

to post a comment
JavaScript

11 Comments(s)

Copy linkTweet thisAlerts:
@Orc_ScorcherOct 30.2006 — encodeURIComponent("路")
decodeURIComponent("%E8%B7%AF")
Copy linkTweet thisAlerts:
@kmthienauthorOct 30.2006 — Hi Orc Scorcher,

I am looking for the javascript code here. Can you pls kindly post it here? Thanks !
Copy linkTweet thisAlerts:
@Orc_ScorcherOct 30.2006 — There is no code to post, encodeURIComponent and decodeURIComponent are native JS functions. Just use them.
Copy linkTweet thisAlerts:
@kmthienauthorOct 30.2006 — ok Thanks a lot Scorcher, I will try it out !
Copy linkTweet thisAlerts:
@kmthienauthorOct 30.2006 — Hi Scorcher,

I try to do it at http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_encodeURIComponent


<html>

<body>

<script type="text/javascript">

document.write(encodeURIComponent("&#36335;"))

<br>

document.write(decodeURIComponent("%E8%B7%AF"))

</script>

</body>

</html>

but it return like below:

%26%2336335%3B

which is not %E8%B7%AF and the decode print out nothing !

pls help, Thanks !
Copy linkTweet thisAlerts:
@Orc_ScorcherOct 30.2006 — Just tried that page, it works for me? Which browser are you using?

Just one thing: as the name suggests encodeURIComponent encode [b]components[/b], not complete URIs. You'd have to write something like uri = "http://yoursite.com/yourpage.html?" + encodeURIComponent(theText)
Copy linkTweet thisAlerts:
@Orc_ScorcherOct 30.2006 — Seems I found the reason: it depends on the character encoding of the page. Go to that w3schools page and before you press the "Edit the text and click me" button, set the character encoding to UTF-8, then you'll get the desired result.
Copy linkTweet thisAlerts:
@kmthienauthorOct 30.2006 — Hi Orc Scorcher,

Yes after changing to Character Encoding->UTF-8, it display correctly. But the problem is not every user in the world know they need to set this encoding in their browser. If the user don't change to UTF-8 in their browser, my server side will definitely get wrong information. Anyway the solve this issue?

Regards,

Mark

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

[B]Plant more tree

Cut carbon emmission

Just use or take what you need

Conserve for your children[/B]
Copy linkTweet thisAlerts:
@Orc_ScorcherOct 30.2006 — On your own pages that shouldn't be a problem. Configure your server to send your pages with 'Content-Type: text/html; charset=utf-8'. If you can't do that for some reason, include the equivalent '<meta http-equiv="Content-Type" content="text/html; charset=utf-8'">' in the header of your pages.
Copy linkTweet thisAlerts:
@kmthienauthorOct 30.2006 — Many thanks Orc Scorcher !



Regards,

Mark

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

[B]Plant more tree

Cut carbon emmission

Just use or take what you need

Conserve for your children[/B]
Copy linkTweet thisAlerts:
@kmthienauthorNov 02.2006 — Hi Scorcher,

I was trying to ask around but just couldn't get any answer and I really hope that you can help me here. Actually I am using servlet. When the user enter a Chinese character say &#36335; in a text field, and I use encodeURIComponent("&#36335;") and get %E8%B7%AF then pass it as a parameter to next page. The next page will get this parameter once it load and pass it to servlet. Hence in the servlet, how can I convert it back to &#36335; or unicode from %E8%B7%AF then pass it to database to search?

Thanks!
×

Success!

Help @kmthien spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...