ASCII(애스키)란 American Standard Code for Information Interchange의 줄임글로서, 영문자에 기초한 문자 인코딩이다.  이 문자 인코딩에는 C0 제어문자(C0 control character)도 포함되어 있다.  ( 참고:  ASCII - Wikipedia, the free encyclopedia )

다음은  7bit ASCII 코드표를 만들어 보여주는 C# 소스 코드이다. 소스 코드 중에 진법변환에 필요한 메소드

        convertAtoI(string, radix)
        convertItoA(long, radix)

의 구현도 포함되어 있다.

/*
 *  Filename: MakeAsciiTableApp.cs
 *            Make a table of ascii codes.
 *
 *  Compile: csc MakeAsciiTableApp.cs
 *  Execute: MakeAsciiTableApp
 *
 *      Date:  2009/01/19
 *    Author:  PH Kim   [ pkim (AT) scripts.pe.kr ]
 */

using System;

namespace MyTestApplication1 {

    public class MakeAsciiTableApp {
        static void println(String s) {
            Console.WriteLine(s);
        }

        static void print(String s) {
            Console.Write(s);
        }

        static void PrintUsage() {
            println("Usage: MakeAsciiTableApp");
            println("Make a table of ascii codes.");
        }

        static string BASE36 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";

        public static string ConvertItoA(long num, int radix) {
            string tmp;
            string ret;
            string arr;
            long q, r;
            bool isNegative = false;
            if (num < 0L) {
                isNegative = true;
                num = -num;
            }
            arr = "";
            q = num;
            r = 0L;

            while (q >= (long) radix) {
                r = q % (long) radix;
                q = q / (long) radix;
                tmp = BASE36.Substring((int)r, 1);
                arr += tmp;
            }
            tmp = BASE36.Substring((int)q, 1);
            arr += tmp;
            if (isNegative) {
                arr += "-";
             }

             ret = "";
             for (int j = 0; j < arr.Length; j++) {
               ret += arr.Substring(arr.Length - j - 1, 1);
             }
             return ret;
        }

        public static long ConvertAtoI(string s, int radix) {
            long ret = 0L;
            bool isNegative = false;
            int len =s.Length;
            char c;
            int i;
            long val = 0L;

            c = s[0];
            if (c == '-') {
                isNegative = true;
            }
            else if (c >= '0' && c <= '9') {
                ret = (long) (c - '0');
            }
            else if (c >= 'A' && c <= 'Z') {
                ret = (long) (c - 'A') + 10L;
            }
            else if (c >= 'a' && c <= 'z') {
                ret = (long) (c - 'a') + 10L;
            }
            if (ret >= (long) radix) {
                Console.WriteLine("        Invalid character!");
                return ret;
            }

            for (i = 1; i < len; i++) {
                c = s[i];
                ret *= radix;
                if (c >= '0' && c <= '9') {
                    val = (long) (c - '0');
                }
                else if (c >= 'A' && c <= 'Z') {
                    val = (long) (c - 'A') + 10L;
                }
                else if (c >= 'a' && c <= 'z') {
                    val = (long) (c - 'a') + 10L;
                }
                if (val >= (long) radix) {
                    Console.WriteLine("        Invalid character!");
                    return ret;
                }
                ret += val;
            }

            if (isNegative)
             ret = -ret;
            return ret;
        }

       static string[] asc  = new 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", "Spc"
        };

        static string[] control  = new string[] {
            "NUL (null)",
            "SOH (start of heading)",
            "STX (start of text)",
            "ETX (end of text)",
            "EOT (end of transmission)",
            "ENQ (enquiry)",
            "ACK (acknowledge)",
            "BEL (bell)",
            "BS  (backspace)",
            "TAB (horizontal tab)",
            "LF  (line feed, NL new line)",
            "VT  (vertical tab)",
            "FF  (form feed, NP new page)",
            "CR  (carriage return)",
            "SO  (shift out)",
            "SI  (shift in)",
            "DLE (data link escape)",
            "DC1 (device control 1)",
            "DC2 (device control 2)",
            "DC3 (device control 3)",
            "DC4 (device control 4)",
            "NAK (negative acknowledge)",
            "SYN (synchronous idle)",
            "ETB (end of trans. block)",
            "CAN (cancel)",
            "EM  (end of medium)",
            "SUB (substitute, EOF end of file)",
            "ESC (escape)",
            "FS  (file separator)",
            "GS  (group separator)",
            "RS  (record separator)",
            "US  (unit separator)",
        };

        public static void MakeTable() {
            string sbuf = "";
            string abuf = "";
            string tbuf = "";
            int i, j;
            char c;

            print("    ");
            for (i = 0; i < 8; i++) {
                print("+----");
            }
            print("+");
            println("");

            print("    ");
            print("| 0- ");
            print("| 1- ");
            print("| 2- ");
            print("| 3- ");
            print("| 4- ");
            print("| 5- ");
            print("| 6- ");
            print("| 7- ");
            print("|");
            println("");

            print("+---");
            for (i = 0; i < 8; i++) {
                print("+----");
            }
            print("+");
            println("");

            for (i = 0; i < 16; i++) {
                tbuf = "";
                sbuf = ConvertItoA((long) i, 16);
                tbuf += "| " + sbuf + " ";
                for (j = 0; j < 8; j++) {
                    if (j*16 + i <= 32) {
                       abuf = String.Format("| {0, -3}", asc[j*16 + i]);
                    }
                    else if (j*16 + i == 127) {
                        abuf = String.Format("| {0, -3}", "DEL");
                    }
                    else {
                        c = (char) (j*16 + i);
                        abuf = String.Format("| {0, 2} ", c);
                    }
                    tbuf += abuf;
                }
                tbuf += "|";
                println(tbuf);
            }

            print("+---");
            for (i = 0; i < 8; i++) {
                print("+----");
            }
            print("+");
            println("");
            println("");

            for (i = 0; i < 16; i++) {
                sbuf = String.Format("{0, -30}",  control[i]);
                tbuf = String.Format("  {0, -34}",  control[i+16]);
                print(sbuf);
                println(tbuf);
            }
        }

        // Java 언어의 main 메소드에 해당하는 C# 언어의 Main 메소드
        public static void Main(string[] args) {
            if (args.Length > 0 && "-h".Equals(args[0])) {
                PrintUsage();
                Environment.Exit(1);
            }
            MakeTable();
        }
    }
}






컴파일> csc MakAsciiTableApp.cs

실행> MakeAsciiTableApp

    +----+----+----+----+----+----+----+----+
    | 0- | 1- | 2- | 3- | 4- | 5- | 6- | 7- |
+---+----+----+----+----+----+----+----+----+
| 0 | NUL| DLE| Spc|  0 |  @ |  P |  ` |  p |
| 1 | SOH| DC1|  ! |  1 |  A |  Q |  a |  q |
| 2 | STX| DC2|  " |  2 |  B |  R |  b |  r |
| 3 | ETX| DC3|  # |  3 |  C |  S |  c |  s |
| 4 | EOT| DC4|  $ |  4 |  D |  T |  d |  t |
| 5 | ENQ| NAK|  % |  5 |  E |  U |  e |  u |
| 6 | ACK| SYN|  & |  6 |  F |  V |  f |  v |
| 7 | BEL| ETB|  ' |  7 |  G |  W |  g |  w |
| 8 | BS | CAN|  ( |  8 |  H |  X |  h |  x |
| 9 | HT | EM |  ) |  9 |  I |  Y |  i |  y |
| A | LF | SUB|  * |  : |  J |  Z |  j |  z |
| B | VT | ESC|  + |  ; |  K |  [ |  k |  { |
| C | FF | FS |  , |  < |  L |  \ |  l |  | |
| D | CR | GS |  - |  = |  M |  ] |  m |  } |
| E | SO | RS |  . |  > |  N |  ^ |  n |  ~ |
| F | SI | US |  / |  ? |  O |  _ |  o | DEL|
+---+----+----+----+----+----+----+----+----+

NUL (null)                      DLE (data link escape)
SOH (start of heading)          DC1 (device control 1)
STX (start of text)             DC2 (device control 2)
ETX (end of text)               DC3 (device control 3)
EOT (end of transmission)       DC4 (device control 4)
ENQ (enquiry)                   NAK (negative acknowledge)
ACK (acknowledge)               SYN (synchronous idle)
BEL (bell)                      ETB (end of trans. block)
BS  (backspace)                 CAN (cancel)
TAB (horizontal tab)            EM  (end of medium)
LF  (line feed, NL new line)    SUB (substitute, EOF end of file)
VT  (vertical tab)              ESC (escape)
FF  (form feed, NP new page)    FS  (file separator)
CR  (carriage return)           GS  (group separator)
SO  (shift out)                 RS  (record separator)
SI  (shift in)                  US  (unit separator)



Creative Commons License

이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
Posted by Scripter
,