ultranerds has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

This is probably super simple for most of you - but I can't work it out =)

I have the following string:

Segments[4][0] = '\u0069\u006d\u0070\u006f\u0072\u0074\u0061\u006e\u0074';

How would I convert that into a text string? The program I'm working in, encodes it like that (to hide it from the front end users) ... but we are now moving over to a new system, so need to "reverse engineer" it, so work out what the phrases actually say =)

TIA

Andy

Replies are listed 'Best First'.
Re: Convert string into plain text
by choroba (Cardinal) on Nov 30, 2015 at 16:41 UTC
    chr, hex:
    $s = '\u0069\u006d\u0070\u006f\u0072\u0074\u0061\u006e\u0074'; $s =~ s/\\u(....)/chr hex $1/ge; say $s; __END__ important
    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
      Thanks, that did it as well. This is why I love Perl - TMTOWTDI :)
Re: Convert string into plain text
by BrowserUk (Patriarch) on Nov 30, 2015 at 16:39 UTC

    Ta da! :)

    $s = '\u0069\u006d\u0070\u006f\u0072\u0074\u0061\u006e\u0074';; $s =~ s[\\u00][]g;; print pack 'H*', $s;; important

    Of course, if that was just a trivial example and the real stuff is actually Unicode, that won't work.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
    In the absence of evidence, opinion is indistinguishable from prejudice.
      Thank you - that worked a treat :)
Re: Convert string into plain text
by Tux (Canon) on Nov 30, 2015 at 17:46 UTC

    TIMTOWTDI BrowserUK: this is Unicode safe :)

    $ perl -E'say map {chr hex}unpack "(xxA4)*","\\u0069\\u006d\\u0070\\u0 +06f\\u0072\\u0074\\u0061\\u006e\\u0074"' important $ cat test.pl use 5.18.2; use warnings; binmode STDOUT, ":encoding(utf-8)"; while (<DATA>) { chomp; say map { chr hex } unpack "(xxA4)*", $_; } __END__ \u0069\u006d\u0070\u006f\u0072\u0074\u0061\u006e\u0074 $ perl test.pl important $

    Enjoy, Have FUN! H.Merijn