A piggy bank of commands, fixes, succinct reviews, some mini articles and technical opinions from a (mostly) Perl developer.

Jump to

Quick reference

Showing posts with label encoding. Show all posts
Showing posts with label encoding. Show all posts

What "Wide character in print" means in Perl (hint: UTF8)

 It means there is a glitch in the matrix - agents are coming!

(further reading)

Prevent spammers from harvesting your email address

When you write your email address on a web page, use @ instead of @, and . instead of the dot.
That way, most email harvesting programs won't recognise it.

Here's a method using Javascript that should stop 100% of email harvesting programs.

However, just using a web contact form would work best.

Find dodgy characters in a file

This will print any characters with values higher than 127, i.e. non-ascii characters that might look like ascii, but really be breaking your processor.

perl -lne'while (my ($c) = $_ =~ m{^(.)}) { print "$c = ".ord($c) if ord($c) > 127; $_ =~ s/^.// }' file_to_check.something


A quick, manual way to confirm some characters really are what you think they are:

cat file_to_check.something | od -bc

URI encoding in Java

Deprecated Apache Commons HttpClient 3.x:
 
import org.apache.commons.httpclient.URIException;
import org.apache.commons.httpclient.util.URIUtil;

String uriTest1(String uri) {
    String encodedUri = null;
    try {
        encodedUri = URIUtil.encodePath(uri,"UTF-8");
    } catch (URIException e) {
        System.err.println("Caught URI Exception");
        e.printStackTrace();
    }
    return encodedUri;
}

URIEncoder encodes to application/x-www-form-urlencoded (i.e. spaces turn to +)

import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;
import java.net.URLEncoder;

String uriTest2(String uri) {
    String encodedUri = null;
    String encoding = "UTF-8";
    try {
        encodedUri = URLEncoder.encode(uri, encoding);
    } catch (UnsupportedEncodingException e) {
        System.err.println("Unsuported encoding: "+encoding);
        e.printStackTrace();
    }
    return encodedUri;
}

java.net.URI requires the whole URI to be input, but you can extract just the URI encoded query part (turns spaces to %20)

import java.net.URI;

private static String uriTest3(String path) {
    String encodedUri = null;
    URI uri = null;
    try {
        uri = new URI("http","bbc.co.uk","/search/news/",path,null);
    } catch (URISyntaxException e) {
        System.err.println("Caught URI Syntax Exception");
        e.printStackTrace();
    }
    encodedUri = uri.getRawQuery();
    return encodedUri;
}
See also.

Convert a string to UTF8

If the string is in iso-8859-1 encoding:

my $string_iso_8859_1 = decode("iso-8859-1", $string);
my $string_utf8 = encode("utf8", $string_iso_8859_1);

How to use encoding

Encoding is necessary only for transmission of data.
Any data stored in files or a database should never be encoded.

Get the transmission right (decode 'automatically' as part of the receiving process, as soon as possible after receiving the data) and you'll never need to worry about decoding issues.