String / Hex conversion


/ Published in: JavaScript
Save to your folder(s)



Copy this code and paste it in your HTML
  1. function d2h(d) {
  2. return d.toString(16);
  3. }
  4. function h2d (h) {
  5. return parseInt(h, 16);
  6. }
  7. function stringToHex (tmp) {
  8. var str = '',
  9. i = 0,
  10. tmp_len = tmp.length,
  11. c;
  12.  
  13. for (; i < tmp_len; i += 1) {
  14. c = tmp.charCodeAt(i);
  15. str += d2h(c) + ' ';
  16. }
  17. return str;
  18. }
  19. function hexToString (tmp) {
  20. var arr = tmp.split(' '),
  21. str = '',
  22. i = 0,
  23. arr_len = arr.length,
  24. c;
  25.  
  26. for (; i < arr_len; i += 1) {
  27. c = String.fromCharCode( h2d( arr[i] ) );
  28. str += c;
  29. }
  30.  
  31. return str;
  32. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.