Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.

How to insert Hexadecimal characters

by paragkalra (Scribe)
on Dec 03, 2009 at 08:55 UTC ( [id://810785] : perlquestion . print w/replies, xml ) Need Help??

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

Hello All,

If I am not wrong, 'print "\n";' inserts hexadecimal character '0D'.

Now I have a requirement where I need to insert both '0D' & '0A'. Is there a way I can accomplish it through '\n' or do I need to use some other technique.


Replies are listed 'Best First'.
Re: How to insert Hexadecimal characters
by Xilman (Hermit) on Dec 03, 2009 at 09:12 UTC

    The behaviour of "\n" is generally dependent on the operating-system and the output stream to which you are writing. On Unix-like systems under most circumstances it translates to '0A' as you note. The same is true for Windows-like files opened in binary mode.

    When writing to Unix-like terminals in canonical mode, or to Windows-like files in text-mode it is '0D' '0A' as you require. Note, I'm using your terminology for consistency. In Perl these would be more commonly denoted as '\x0D' and '\x0A' respectively.

    The previous sentence should be all the hint you need, but here it is spelled out explicitly. To force the issue regardless of operating system and nature of the output stream replace 'print "\n";' with 'print "\x0D\x0A";' and you will get what you ask for.

    Whether what you ask for is what you want is another matter entirely...


Re: How to insert Hexadecimal characters
by moritz (Cardinal) on Dec 03, 2009 at 10:19 UTC
    I like the :crlf IO layer:
    $ perl -we 'print "\n"' | hexdump -C 00000000 0a |.| $ perl -we 'binmode STDOUT, ":crlf"; print "\n"' | hexdump -C 00000000 0d 0a |..|
Re: How to insert Hexadecimal characters
by dHarry (Abbot) on Dec 03, 2009 at 09:53 UTC

    I produce a lot of ASCII files that need to end in '0D0A'. The script can be run from both Windows and Linux. Normally I do something like:

    my $line_delimiter = ($^O eq "MSWin32")?"\n":"\r\n"; sprintf("%-$line_width" . "s", $line) . $line_delimiter;

    The line-width is typically 78 or 70 (excluding the 2 added by the line-delimiter) for my purpose. Tailor it according to your needs.



      sprintf("%-$line_width" . "s", $line) . $line_delimiter;

      I think all the concatenation in this line is a little confusing. I'd prefer to interpolate everything in the format string. My assumption is that you are using the sprintf as the return from a subroutine or a do block. Obviously, use printf if you expect actual output.

      sprintf qq{%-${line_width}s$line_delimiter}, $line;

      I hope this is of interest.



Re: How to insert Hexadecimal characters
by Marshall (Canon) on Dec 03, 2009 at 11:47 UTC
    No! If you are printing text, at least x'0A' (line feed) will be inserted (not x'0D').

    The question is whether or not a x'0D' (carriage return) will also be inserted before the x'0A'. If you are interested , I refer you to: for the standard ASCII characters (doesn't cover the wide 16 bit ones).

    In ancient days, each line ended with CR,LF,RUBOUT(x'00'). The RUBOUT(NUL) x'00', was needed to keep the fingers on the mechanical tape reader/punch lubricated. Keeping the CR and LF separate allowed pictures to be drawn of a sort. CR (carriage return) just meant go back to the beginning of the line and then more stuff (different characters) could be printed on the same line. I hope you get to see one of these early pictorial masterpieces. I haven't used a printer for a very long time that could reproduce such an image - there isn't any idea that "over-striking" something will make it darker like existed with typewriter ribbon technology.

    As far as Perl goes, Perl print in a text context will do the "right thing" for your platform. For some reason Windows kept this CR,LF idea while Unix moved to just LF.

    I have often moved files between Unix and Windows systems. In the old days, this was a big hassle and I would have to run some sort of "fixit" script on one machine or the other. Now I just use FireFTP (other modern FTP programs work also) and this automatically adjusts this stuff for me when I am in text mode (and it is very smart about figuring that out).

    Perl itself is "smart" about CR/LF. It will ignore this CR if it exists. Perl can read any text file that it created on any platform.

    There are some situations, like hashing to disk with fixed record sizes where this line termination stuff matters a lot. But I'm not hearing that is not the case. I've written programs in both Perl and C using this concept and I'd be happy to show you the code, but I don't think that is what you need.

    In short: don't worry about this in your Perl programs. If this CR needs to be inserted before the LF, use a std FTP program in text mode to make the transfer between the *NIX and Windows box.

Re: How to insert Hexadecimal characters
by JavaFan (Canon) on Dec 03, 2009 at 10:03 UTC
    Actually, you are wrong. print "\n" prints a logical newline - it will output the bytestring which is the appropriate "newline" for the current platform.

    In your case, I would do: print "\x{0D}\x{0A}";

      For the record (and since no one has mentioned it yet), this is documented in perlport.

Re: How to insert Hexadecimal characters
by rovf (Priest) on Dec 03, 2009 at 14:36 UTC
    I have a requirement where I need to insert both '0D' & '0A'
    Unless you want to have this as terminator for every line (in which case, as it was suggested, you should use the :crlf layer), you might also consider the :raw layer, which gives you full control. See perlio.

    Ronald Fischer <>