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

Hi, I have a hexidecimal character in a string returned by my DB and I would like to remove it with the substitution method. My questions are:

1) How do I determine which hex digit it is?

2) How do I remove the character?

Thank you

Replies are listed 'Best First'.
Re: hexadecimal character
by graff (Chancellor) on Mar 06, 2007 at 02:00 UTC
    Allow me to propose the following, as a sort of experiment for you to perform. You can go ahead and fold this snippet into your perl script (it is a perl script you're talking about, isn't it?), and let us know what sort of output you see on your terminal as a result of running the code with this snippet.
    # Let's pretend you have the troublesome string returned by your # DB, and that string is stored in a variable called "$db_string". # (if it's actually stored in a variable called "$x", you # could adapt it to this snippet by adding the line: # my $db_string = $x; my @db_chars = (); for my $db_char ( split //, $db_string ) { push @db_chars, sprintf( "%02x", ord( $db_char )); } for my $db_char ( @db_chars ) { printf "== %s == %s ==\n", $db_char, chr(hex($db_char)); }
    That will show you the actual hex codes of each (single-byte) character in the string from the database, along with the way that single-byte character appears on your screen when printed in its "raw" form. If the database string is really long, you can limit the second loop to just enough iterations to clarify what's happening -- e.g. if 15 characters is enough:
    for my $db_char ( @db_chars[0..14] ) { ...
    Once you've done that, try to learn something about what is actually in the database. What language is it (Greek? Russian? Chinese?), what encoding is it in (ISO-8859?, utf8? GBK?), what is it supposed to look like when it's displayed correctly (and do you have the font needed to do that)?
Re: hexadecimal character
by GrandFather (Saint) on Mar 06, 2007 at 00:52 UTC
    1. It's that one right there
    2. use this handy eraser

    Alternatively you could show us the string you are having trouble with. We don't know what your DB may be returning unless you show us.


    DWIM is Perl's answer to Gödel
      I meant - which hex number is it returning? Is it the number 2, the number 9? How do I figure it out? When the string is printed out, it has a bunch of 'boxes' if you know what I mean. thanks
        A reply falls below the community's threshold of quality. You may see it by logging in.

        Show us some code. Sounds like you are dealing with either control characters (are you managing line ends correctly?), binary data or possibly Unicode. There is no way we can solve your problem without knowing what you are actually trying to do.

        Show use some code. Show us some sample data. Keep the whole lot under 10 lines. (read I know what I mean. Why don't you? first perhaps.)


        DWIM is Perl's answer to Gödel
Re: hexadecimal character
by Moron (Curate) on Mar 06, 2007 at 17:14 UTC
    If it's just a question of removing unprintable characters...
    s/[:^print:]//g;

    -M

    Free your mind