Hello, I'm looking for a Win-API-like function to convert a string (i. e. with ANSI char's) into OEM-char's.
If your conversion program is running on Windows, you can get the AnsiToOEM Win32 API function in Perl via the Win32::API module. However, a more portable (and perhaps even simplier, considering the complexity of using Win32::API) solution is to write a function to convert CP1252 (WinLatin1) to CP437 (DOSLatinUS). To do this download the textfiles from those links at Czyborra and write a program like this:
my ($from_name, $to_name, $from_map, $to_map); $from_name = "cp1252.txt"; $to_name = "cp437.txt"; # Load a codepage file into a Perl data structure sub load_cp { my ($filename) = @_; my (@map); open(CP, "<$filename") || die "load_cp: $filename: $!"; while(<CP>) { my ($byte, $unicode) = m/^=(..)\tU[+](....)/; $map[hex $byte] = hex($unicode); } return \@map; } # Map characters in a file from one codepage to another sub map_cp { my ($to, $from, $text) = @_; my ($new_text, $map, %to, %from, @text); # First map codepage "$from" characters to Unicode characters foreach my $char (split //, $text) { if (!defined $from->[ord $char]) { warn "no to=$to_name char for U+$char\n"; } push @text, $from->[ord $char]; } # Now map non-ASCII Unicode characters to codepage "$to" foreach my $char (@text) { if (!defined $to->[$char]) { warn "no from=$from_name char for U+$char\n"; } $new_text .= chr $to->[$char]; } return $new_text; } # Load to and from codepages $from_map = load_cp($from_name); $to_map = load_cp( $to_name); # Replace \x80 with your text print map_cp($from_map, $to_map, "\x80");
Note that CP1252 has characters that CP437 doesn't have, and vice versa. You may want to replace all your ANSI character codes with UTF-8 Unicode characters. It makes things much easier, and then you could use the cp437.txt from Czyborra to generate text capable of being viewed in a DOS environment with the CP437 codepage.

In reply to Re: char2oem by Anonymous Monk
in thread char2oem by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.