#include const char *ch_table[] = { /*160*/ "nbsp", "iexp", "cent", "pound", "curren", "yen", "brvar", "sect", "uml", "copy", /*170*/ "ordf", "laquo", "not", "shy", "reg", "macr", "deg", "plusmn", "sup2", "sup3", /*180*/ "acute", "micro", "para", "middot", "cedol", "sup1", "ordm", "raquo", "frac14", "frac12", /*190*/ "frac34", "iquest", "Agrave", "Aacute", "Acirc", "Atilde", "Auml", "Aring", "AElig", "Ccedil", /*200*/ "Egrave", "Eacute", "Ecirc", "Euml", "Igrave", "Iacute", "Icirc", "Iuml", "ETH", "Ntilde", /*210*/ "Ograve", "Oacute", "Ocirc", "Otilde", "Ouml", "times", "Oslash", "Ugrave", "Uacute", "Ucirc", /*220*/ "Uuml", "Yacute", "THORN", "szlig", "agrave", "aacute", "acirc", "atilde", "auml", "aring", /*230*/ "aelig", "ccedil", "egrave", "eacute", "ecirc", "euml", "igrave", "iacute", "icirc", "iuml", /*240*/ "eth", "ntilde", "ograve", "oacute", "ocirc", "otilde", "ouml", 0, "oslash", "ugrave", /*250*/ "uacute", "ucirc", "uuml", "yacute", "thorn", "yuml", "aring", "Eth", "icirc", "Thorn", }; #define NR_CH_TABLE (sizeof(ch_table)/sizeof(char *)) int main(int argc, char *argv[]) { unsigned char ch = fgetc(stdin); while (!feof(stdin)) { int unicode = 0; bool okay = true; if (ch < 0x80) { unicode = ch; ch = fgetc(stdin); } else { if ((ch & 0xe0) == 0xc0 && (ch & 0x1e) != 0) { unicode = ch & 0x1f; ch = fgetc(stdin); } else { if ((ch & 0xf0) == 0xe0) { unicode = ch & 0x0f; ch = fgetc(stdin); } else { if ((ch & 0xf8) == 0xf0) { unicode = ch & 0x07; ch = fgetc(stdin); } else okay = false; if ((ch & 0xc0) != 0x80) okay = false; if (okay) { unicode = (unicode << 6) | (ch & 0x3f); ch = fgetc(stdin); } } if ((ch & 0xc0) != 0x80) okay = false; if (okay) { unicode = (unicode << 6) | (ch & 0x3f); ch = fgetc(stdin); } } if ((ch & 0xc0) != 0x80) okay = false; if (okay) { unicode = (unicode << 6) | (ch & 0x3f); ch = fgetc(stdin); } } if (okay) { if (unicode < 127) printf("%c", unicode); else if (unicode >= 160 && unicode <= 255 && ch_table[unicode-160] != 0) printf("&%s;", ch_table[unicode-160]); else printf("&#x%X;", unicode); } else fprintf(stderr, "Illegale Unicode value %d\n", unicode); } return 0; }