3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Delphi】(ASCII) コード表を表示するコマンドを作る

Last updated at Posted at 2025-01-28

はじめに

ネットで ASCII コード表を確認するのが面倒になったので、コード表を表示するコマンドを作ろうと思いました。

どんなの?

ASCII の範囲である 0x00..0x7F を表示できればいいのですが、それだけだと芸がないので、8bit のいわゆる ANK も表示し、制御コードとキャレット記法も表示する事にします。

コード

実際のコードです。コンソールアプリケーションとなっています。ちょっと悩みましたが CodePage という名前にしました。

codepage.dpr
program CodePage;

{$APPTYPE CONSOLE}

uses
  System.SysUtils, Winapi.Windows;

const
  CtrlCode: array [0..32] of string = (
    'NUL', 'SOH', 'STX', 'ETX', 'EOT', 'ENQ', 'ACK', 'BEL',
    'BS ', 'HT ', 'LF ', 'VT ', 'FF ', 'CR ', 'SO ', 'SI ',
    'DLE', 'DC1', 'DC2', 'DC3', 'DC4', 'NAK', 'SYN', 'ETB',
    'CAN', 'EM ', 'SUB', 'ESC', 'FS ', 'GS ', 'RS ', 'US ',
    'DEL'
  );
 FmtStr = '  0x%.2x: %s(^%s)';

var
  CP: Integer;
  S: set of byte;

procedure HLine;
const
  LineChar: array [Boolean] of char = ('+', '-');
begin
  for var i := 0 to 34 do
    Write(LineChar[Odd(i)]);
  Writeln;
end;

procedure PutChar(x, y: byte);
begin
  var b := x * 16 + y;
  if not (b in S) then
    b := $20;
  Write(AnsiChar(b), '|');
end;

begin
  CP := GetConsoleCP();
  Writeln('Active code page: ', CP);
  if CP = 932 then
    S := [$20..$7E, $A1..$DF]
  else
    S := [$20..$7E, $80..$FF];
  HLine;
  Write('| |');
  for var c := 0 to 15 do
    Write(Format('%x|', [c]));
  Writeln;
  HLine;
  for var r := 0 to 15 do
  begin
    Write(Format('|%x|', [r]));
    for var c := 0 to 15 do
      PutChar(c, r);
    var Ctrl := '';
    Ctrl := Ctrl + Format(FmtStr, [r + $00, CtrlCode[r + $00], Chr(r + $40)]);
    Ctrl := Ctrl + Format(FmtStr, [r + $10, CtrlCode[r + $10], Chr(r + $50)]);
    if r = 15 then
      Ctrl := Ctrl + Format(FmtStr, [r + $70, CtrlCode[$20], Chr(r + $30)]);
    Writeln(Ctrl);
  end;
  HLine;
end.

実行

実行すると次のような表が表示されます。

image.png

おわりに

「Delphi 12 Athens 使ってるんなら、これでよくね?」

CodePage2.dpr
program CodePage2;

{$APPTYPE CONSOLE}

uses
  Winapi.Windows;

begin
  Writeln('Active code page: ', GetConsoleCP());
  Writeln('''
  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  | |0|1|2|3|4|5|6|7|8|9|A|B|C|D|E|F|
  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  |0| | | |0|@|P|`|p| | | |ー|タ|ミ| | |  0x00: NUL(^@)  0x10: DLE(^P)
  |1| | |!|1|A|Q|a|q| | |。|ア|チ|ム| | |  0x01: SOH(^A)  0x11: DC1(^Q)
  |2| | |"|2|B|R|b|r| | |「|イ|ツ|メ| | |  0x02: STX(^B)  0x12: DC2(^R)
  |3| | |#|3|C|S|c|s| | |」|ウ|テ|モ| | |  0x03: ETX(^C)  0x13: DC3(^S)
  |4| | |$|4|D|T|d|t| | |、|エ|ト|ヤ| | |  0x04: EOT(^D)  0x14: DC4(^T)
  |5| | |%|5|E|U|e|u| | |・|オ|ナ|ユ| | |  0x05: ENQ(^E)  0x15: NAK(^U)
  |6| | |&|6|F|V|f|v| | |ヲ|カ|ニ|ヨ| | |  0x06: ACK(^F)  0x16: SYN(^V)
  |7| | |'|7|G|W|g|w| | ||||| | |  0x07: BEL(^G)  0x17: ETB(^W)
  |8| | |(|8|H|X|h|x| | ||||| | |  0x08: BS (^H)  0x18: CAN(^X)
  |9| | |)|9|I|Y|i|y| | ||||| | |  0x09: HT (^I)  0x19: EM (^Y)
  |A| | |*|:|J|Z|j|z| | ||||| | |  0x0A: LF (^J)  0x1A: SUB(^Z)
  |B| | |+|;|K|[|k|{| | |ォ|サ|ヒ|ロ| | |  0x0B: VT (^K)  0x1B: ESC(^[)
  |C| | |,|<|L|\|l||| | |ャ|シ|フ|ワ| | |  0x0C: FF (^L)  0x1C: FS (^\)
  |D| | |-|=|M|]|m|}| | ||||| | |  0x0D: CR (^M)  0x1D: GS (^])
  |E| | |.|>|N|^|n|~| | ||||| | |  0x0E: SO (^N)  0x1E: RS (^^)
  |F| | |/|?|O|_|o| | | ||ソ||| | |  0x0F: SI (^O)  0x1F: US (^_)  0x7F: DEL(^?)
  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  ''');
end.

「SysUtils 使わないから EXE サイズも小さくなるし」

EXE ファイルサイズ
CodePage.exe 159,744 bytes
CodePage2.exe 60,416 bytes

ぐぬぬ...

See also:

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?