in reply to Re^5: Print unicode strings to pdf
in thread Print unicode strings to pdf

Thanks Corion.

Sorry, the view part is using the inbuilt "alert" function of Javascript. This is what I see of the json string from perl passed to Javascript using "alert":
{ "hex_code":"\\xa5" }
When I display this in an html input field, it displays:
\xa5
After much googling with the right keywords, I found a solution by passing the value "\\xa5" (which originates from perl) to the function below:
//https://stackoverflow.com/questions/4209104/decoding-hex-string-in-j +avascript String.prototype.decodeEscapeSequence = function() { return this.replace(/\\x([0-9A-Fa-f]{4})/g, function() { return String.fromCharCode(parseInt(arguments[1], 16)); }); };
I think it's somewhat similar to your perl code below:
sub unescape { my( $str ) = @_; $str =~ s!\\x\{([0-9a-f]{4})\}!chr(hex $1)!ge; $str };
I am confused because when I have a variable set to a hex value in Javascript like this:
var hex_code = '\xa5';
That gets displayed correctly in the input field as a symbol.

So I thought since "\\xa5" is what I see with Javascript's alert, if I remove the first backslash, it would work. It doesn't whether I remove the first backslash or not. I needed to unescape with the Javascript function found in stackoverflow.

Replies are listed 'Best First'.
Re^7: Print unicode strings to pdf
by Corion (Patriarch) on Mar 27, 2018 at 09:00 UTC

    Maybe a better approach would be to look at what data Perl is sending instead of using Javascript and your browser as an intermediary?

    Also note that with JSON, you still have to parse it in Javascript as well, but I think JSON uses \U... and not \x... escapes, so you will need to think about how you generate your JSON and use a proper way.