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

Could you enlighten me on this related issue?

I have a json string from perl (encoded with to_json) which goes to my Javascript code. When I view the json string, it looks like this:
{ "hex_code":"\\x{a5}" }
I want to display the hex_code as an actual symbol in an input field. But what I see in the input field is '\x{a5}' and not the symbol. In Javascript when I set a variable like this:
var hex_code = '\x{a5}'; document.getElementById('some_field').value = hex_code;
The symbol gets displayed in the input field.

What do I need to in the perl code (or in the Javascript) to display the hex code as symbol in the input field?

I'm quite confused by this. Hope you can shed some light :)

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

    What do you mean by "view the JSON string"? Do you mean you print it? Do you print it using Data::Dumper?

    If you want to turn the JSON back into a proper data structure, I suggest you use JSON or JSON::XS to do that. You might need to unescape the string before doing that if it contains doubled backslashes, so that it is proper JSON:

    use JSON 'decode_json'; use Data::Dumper; my $mangled_json = '{ "hex_code":"\\x{a5}" }'; print $mangled_json; my $json = $mangled_json; $json =~ s!\\\\!\\!g; print $json; my $structure = decode_json( $json ); $Data::Dumper::Useqq = 1; print Dumper $structure; binmode STDOUT, ':encoding(UTF-8)'; # well, hopefully, your terminal u +nderstands UTF-8 print $structure->{'hex_code'};
      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.

        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.