1

Is there a uniform method in both PHP and JS to convert unicode characters, so the result would be same on both ends after encoding the unicode string ?

Is there any encoding/ decoding technique on both ends that shares the same mechanism ?

I have been trying with bin2hex() and hex2bin() with PHP and respective manual functions for the same in JS, but it doesn't work for unicode characters ?

hsuk
  • 6,770
  • 13
  • 50
  • 80
  • You could use base64 to encode/decode any binary/text data. It should preserve every single bit. Anyway, could you please explain what's your actual problem? Why can't you just send text data with some metadata about encoding? You could also convert any encoding to UTF-8 before sending. – Crozin Sep 17 '13 at 10:25
  • I tried with base64 JS library as there is no JS function for base64. But, I don't know why the encoded value generated for same string in PHP and via that JS library is different. So, I need sthg different. My actual problem is explained here: http://stackoverflow.com/questions/18786025/mcrypt-js-encryption-value-is-different-than-that-produced-by-php-mcrypt-mcryp – hsuk Sep 17 '13 at 10:36
  • 1
    "Convert Unicode characters" ***to what?*** For what purpose? – deceze Sep 17 '13 at 10:38
  • @deceze : Actually I want to encode those unicode characters before sending it for further encryption process and as the encyption/ decryption process will be on both PHP end and JS end, I am asking for the encoding/ decoding technique for unicode characters as I faced too many issues while feeding the unicode characters directly. – hsuk Sep 17 '13 at 10:43
  • @hsuk: What encryption/decryption are you doing exactly? It would be nice if you could show us the code you are using and where it failed. – Bergi Sep 18 '13 at 04:44
  • 1
    @Bergi : you can find this at http://stackoverflow.com/questions/18786025/mcrypt-js-encryption-value-is-different-than-that-produced-by-php-mcrypt-mcryp – hsuk Sep 18 '13 at 04:46

3 Answers3

2

Yes, it's called utf-8. If you encode all documents as utf-8 and store all data as utf-8, you should be in the clear. The problem is that php, per default, expects strings to be latin1, so you need to change a few things (Like send a Content-Type header etc.)

troelskn
  • 115,121
  • 27
  • 131
  • 155
2

I referenced to this question :

How do I convert special UTF-8 chars to their iso-8859-1 equivalent using javascript?

The following functions helped me:

fixed_string = decodeURIComponent(escape(utf_string));

utf_string = unescape(encodeURIComponent(original_string));

The escape and unescape functions used for encoding and decoding query strings are defined for ISO characters whereas the newer encodeURIComponent and decodeURIComponent which do the same thing, are defined for UTF-8 characters.

Community
  • 1
  • 1
hsuk
  • 6,770
  • 13
  • 50
  • 80
0

This is a hack but it works.

PHP

rawurlencode($theString)

JS

decodeURIComponent(theString)
antzshrek
  • 9,276
  • 5
  • 26
  • 43
Chaoley
  • 1,282
  • 15
  • 21
  • Actually I don't think it works for binary data. I don't know whether it is called binary data, I mean to say the return value of mcrypt encryption. It includes different signs and symbols, unsupported chars. – hsuk Sep 18 '13 at 04:08
  • I've never tried it with binary data but it works fine with UTF8 strings – Chaoley Sep 19 '13 at 05:06