tags:

views:

115

answers:

4

I'm writing a script for IRC and sometimes I might need to use a color. Normally I do it this way

my $C = chr(3);

$C is the control code used for color but I've seen some other script that escapes it something like "\x\v...". How do I get the correct encoded version of that? I tried Data::Dumper but I didn't find it. I hope this question makes sense.

+5  A: 

Characters with codes on the range 0 .. 255 can be expressed in a number of ways. These example all print the character A:

print chr(65);
print "\101";     # octal notation
print "\x41";     # hexidecimal notation (and hexadecimal notation)
printf "%c",65;


or for your particular problem:

print chr(3);
print "\003";
print "\3";
print "\x03";     # hexidecimal notation (and hexadecimal notation)
printf "%c",3;
print "\cc";      # from Sinan's answer
print "\cC";
mobrule
Ah, I see. So I think it's the hexadecimal notation that I've seen in the other scripts. So how would I go about converting chr(3) to a hexadecimal notation?
somebody
@mobrule I think you have covered all the bases ;-)
Sinan Ünür
+3  A: 

You can print hex-encoded (or even octal- or binary-encoded) characters through a number of mechanisms (and this is not a comprehensive list by any means):

# generate strings from hex:
my $space_char = sprintf("%x", 0x20);
my $space_char2 = "\x20";
my $space_char3 = 0x20;
my $space_char4 = pack("L", 0x20);
my $space_char5 = chr(0x20);

You can read about these functions at perldoc perlfunc or individually via perldoc -f sprintf, perldoc -f pack, perldoc -f chr etc.

For more about hexadecimal, octal and binary numbers in general, see "Scalar value constructors" under perldoc perldata.

Ether
Can you give me an example of how to get the hex version of chr(3) not the other way round?
somebody
I found the answer. Thanks everyone
somebody
@Wireless What the heck does "I found the answer" mean? There are some very good answers here such as the ones given by @Ether and @mobrule. **You should check one of them as the answer to the question you asked here.**
Sinan Ünür
Sorry, that was just a follow up to the previous comment about the example "chr(3)".
somebody
+1  A: 

See the sections Escape sequences and Character Classes and other Special Escapes in perldoc perlre.

mctylr
+4  A: 

The way to specify chr(3) with a hexadecimal escape code is to use:

print "\x03\n";

or, in octal:

print "\003\n";

or, as a control code:

print "\cC\n";

See perldoc perlop:

The following escape sequences are available in constructs that interpolate and in transliterations.

  1. \t tab (HT, TAB)
  2. \n newline (NL)
  3. \r return (CR)
  4. \f form feed (FF)
  5. \b backspace (BS)
  6. \a alarm (bell) (BEL)
  7. \e escape (ESC)
  8. \033 octal char (example: ESC)
  9. \x1b hex char (example: ESC)
  10. \x{263a} wide hex char (example: SMILEY)
  11. \c[ control char (example: ESC)
  12. \N{name} named Unicode character
Sinan Ünür
perlop! I checked perlsyn, perldata, and perlintro looking for this.
mobrule
@mobrule: I cheated: http://www.google.com/search?q=character+escape+sequence+site%3Aperldoc.perl.org
Sinan Ünür