note

個人的なスクリプト/プログラムをつらつらっと

EBCDIC to ASCII
 
汎用機データをUnix上で変換するための
EBCDIC to ASCIIのPerl Code

iconv、ddでもOKだけど、こっちはテーブルを
弄れるため顧客が外字登録している場合に役に立つ
#!/usr/bin/perl

# convert EBCDI* to ASCII
# usage:
# ./ convEtoA.pl infile outfile

# Please edit
# --------------------------------Low Value ------------>
# | 0x00 ---------------------------------------- 0x0f
# | |
# | | High Value
# | |
# | 0xf0 0xff
# V
@EBCDIKtoASCII = (
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x20, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0x7e, 0x2e, 0x3c, 0x28, 0x2b, 0x7c,
0x26, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, 0x00, 0xb0, 0x00, 0x21, 0x5c, 0x2a, 0x29, 0x3b, 0x1d,
0x2d, 0x2f, 0x00, 0x00, 0x00, 0x00, 0x7e, 0x00, 0x00, 0x00, 0x5e, 0x2c, 0x25, 0x5f, 0x3e, 0x3f,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3a, 0x23, 0x40, 0x27, 0x3d, 0x22,
0x00, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0x00, 0xbb, 0xbc, 0xbd, 0xbe,
0xbf, 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0x00, 0x12, 0xca, 0xcb, 0xcc,
0x00, 0x7b, 0xcd, 0xce, 0xcf, 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0x7d, 0xd6, 0xd7, 0xd8, 0xd9,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf,
0x5b, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x5d, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50, 0x51, 0x52, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x24, 0x00, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
);

# Don't edit
$CodeMatrix = "0123456789ABCDEF";

# sub process
sub getNumeric(){
my $Code = $_[0];

$Code =~ tr/a-f/A-F/;
my($word1, $word2) = split(//, $Code);
my $num1 = index($CodeMatrix, $word1);
my $num2 = index($CodeMatrix, $word2);
print "num1 = $num1 : num2 = $num2\n";
return $num1 * 16 + $num2;
}

# Start main process
open(IH, "$ARGV[0]") || die("can't open $ARGV[0]");
open(OH, ">$ARGV[1]") || die("can't open $ARGV[1]");
binmode IH;
binmode OH;
while(eof(IH) == 0){
$char = getc(IH);
$index = &getNumeric(unpack('H2', $char));
print OH pack("C*", $EBCDIKtoASCII[$index]);
}
close(IH);
close(OH);

exit;
2010-11-23 (Tue)

sample code2
 
test
test
 test
  てst
2010-11-11 (Thu)

Code Test
 
int main(){
 printf("Hellow\n");
printf("world\n");

return 0;
};
2010-11-11 (Thu)