Software development, photography, jokes, ....


noyb-logo

Sites by me

 
sds82-logo
tas-logoTransportation Administration System
snoezelkussen-logo-kleinstSnoezelen Pillows for Dementia
ikzoekeenbegeleider-logoBegeleiders voor gehandicapten
Laat uw hond het jaarlijkse vuurwerk overwinnen
Betuweroute en Kunst
logo 50x50Hey Vos! Je eigen naam@vos.net emailadres?
Kunst in huis? Nicole Karrèr maakt echt bijzonder mooie dingen
nettylogo2Kunst in huis? Netty Franssen maakt ook bijzonder mooie dingen

Hosting Favorites

 
ANU Internet Services
Netim
GoDaddy
XEL Media

Blogroll

 
Bomenstichting
Google Translate
PHP
MySQL
jQuery
jQuery UI
YourHead Stacks API
Favicon Generator.
Password Generator.
Check HTTPS problems




Categories

Archives

Marc's Place


 

Photo-blog


Alle getoonde foto's zijn (c) MHE Vos, Nederland.

All shown photo's are (c) MHE Vos, Netherlands.


Below blog is created with the Blogspot.stack using the Categories-filter.

Hex2Str and Str2Hex Javascript Functions

 Permalink
Today I needed some Javascript to convert a HEX-encoded string to an ASCII-encoded string. The reason is that to be able to send all possible strings via AJAX-calls back to the client, without problems with quotes, apostrophes and other non-ASCII7 characters, I decided to encode the AJAX-data to hex, which is a nice and simple ASCII7 string of only the characters 0 through 9 and A through F.

After Googling a bit I found an example on webdeveloper.com and some tips on stackoverflow.com, and both combined with some of my own JS-knowledge, I came up with the following small and handy functions:

function Str2Hex(tmp) {
var str = "";
for (var i=0; i<tmp.length; i++)
str += ("00" + (tmp.charCodeAt(i)).toString(16)).substr(-2);
return(str);
}
function Hex2Str(tmp) {
var str = "";
for (var i=0; i<tmp.length; i+=2)
str += String.fromCharCode(parseInt(tmp.substr(i,2),16));
return(str);
}

For example, after a user made a selection from the autocomplete menu and you return a HEX-encoded string containing multiple values, separated by "+++", to fill other form-fields too:

..., select: function(e,u) {
var x = Hex2Str(u.item.value).split("+++");
$("#field1").val(x[0]).change();
$("#field2").val(x[1]).change();
... etc.
return false; } ....

Or when you return multiple values as a HEX-encoded JSON array:

..., select: function(e,u) {
var x = u.item.value;
$("#field1").val(Hex2Str(x.field1)).change();
$("#field2").val(Hex2Str(x.field2)).change();
... etc.
return false; } ....

Happy coding!

Alle getoonde foto's zijn (c) MHE Vos, Nederland.

All shown photo's are (c) MHE Vos, Netherlands.


© 1997- Marc Vos (and others)   -   Privacy Statement   -    Contact Me

On this website, Google Analytics is used to track visitor statistics. These are anonymised data about the number of visitors, which pages they visit on this site, from which regions they visit, which web browsers they use, etc.. You will also see non-personalised ads via Google AdSense. Cookies from Paddle or Paypal are placed when you click on a 'Buy now!' or 'Donate!' button, and possible cookies from Disqus when you use that system to comment on one or more blogposts.
Privacy Statement