Last Update: Nov.14,2006

C Source Library

「LSI C-86 Ver.3.30c 試食版」、「Borland C++ 5.5」でコンパイル出来ます。

■ Macintosh → Windows 機種依存文字変換

/*  Macintosh → Windows 機種依存文字変換
 */

#include <stdio.h>

#define iskanji(c)  ((c)>=0x81&&(c)<=0x9f)||((c)>=0xe0&&(c)<=0xfc)
#define iskanji2(c) ((c)>=0x40&&(c)<=0x7e)||((c)>=0x80&&(c)<=0xfc)

/*  Macintosh -> Windows のテーブル
 *
 *      Tue Feb 25 09:51:39 2003
 */

typedef struct {
    unsigned short maccode;
    unsigned short wincode;
} MAC2WIN_TABLE;

const MAC2WIN_TABLE m2w[] = {
    {0x005c,0x0000},{0x0080,0x005c},{0x00a0,0x0000},{0x00fd,0x0000}, /* ・ \・・ */
    {0x00fe,0x0000},{0x815c,0x0000},{0x8160,0x0000},{0x8161,0x0000}, /* ・・・・ */
    {0x817c,0x0000},{0x8191,0x0000},{0x8192,0x0000},{0x81ca,0x0000}, /* ・・・・ */
    {0x8540,0x8740},{0x8541,0x8741},{0x8542,0x8742},{0x8543,0x8743}, /* ①②③④ */
    {0x8544,0x8744},{0x8545,0x8745},{0x8546,0x8746},{0x8547,0x8747}, /* ⑤⑥⑦⑧ */
    {0x8548,0x8748},{0x8549,0x8749},{0x854a,0x874a},{0x854b,0x874b}, /* ⑨⑩⑪⑫ */
    {0x854c,0x874c},{0x854d,0x874d},{0x854e,0x874e},{0x854f,0x874f}, /* ⑬⑭⑮⑯ */
    {0x8550,0x8750},{0x8551,0x8751},{0x8552,0x8752},{0x8553,0x8753}, /* ⑰⑱⑲⑳ */
    {0x855e,0x0000},{0x855f,0x0000},{0x8560,0x0000},{0x8561,0x0000}, /* ・・・・ */
    {0x8562,0x0000},{0x8563,0x0000},{0x8564,0x0000},{0x8565,0x0000}, /* ・・・・ */
    {0x8566,0x0000},{0x8567,0x0000},{0x8568,0x0000},{0x8569,0x0000}, /* ・・・・ */
    {0x856a,0x0000},{0x856b,0x0000},{0x856c,0x0000},{0x856d,0x0000}, /* ・・・・ */
    {0x856e,0x0000},{0x856f,0x0000},{0x8570,0x0000},{0x8571,0x0000}, /* ・・・・ */
    {0x857c,0x0000},{0x857d,0x0000},{0x857e,0x0000},{0x8580,0x0000}, /* ・・・・ */
    {0x8581,0x0000},{0x8582,0x0000},{0x8583,0x0000},{0x8584,0x0000}, /* ・・・・ */
    {0x8585,0x0000},{0x8592,0x0000},{0x8593,0x0000},{0x8594,0x0000}, /* ・・・・ */
    {0x8595,0x0000},{0x8596,0x0000},{0x8597,0x0000},{0x8598,0x0000}, /* ・・・・ */
    {0x8599,0x0000},{0x859a,0x0000},{0x859f,0x8754},{0x85a0,0x8755}, /* ・・ⅠⅡ */
    {0x85a1,0x8756},{0x85a2,0x8757},{0x85a3,0x8758},{0x85a4,0x8759}, /* ⅢⅣⅤⅥ */
    {0x85a5,0x875a},{0x85a6,0x875b},{0x85a7,0x875c},{0x85a8,0x875d}, /* ⅦⅧⅨⅩ */
    {0x85a9,0x0000},{0x85aa,0x0000},{0x85b3,0xeeef},{0x85b4,0xeef0}, /* ・・ⅰⅱ */
    {0x85b5,0xeef1},{0x85b6,0xeef2},{0x85b7,0xeef3},{0x85b8,0xeef4}, /* ⅲⅳⅴⅵ */
    {0x85b9,0xeef5},{0x85ba,0xeef6},{0x85bb,0xeef7},{0x85bc,0xeef8}, /* ⅶⅷⅸⅹ */
    {0x85bd,0x0000},{0x85be,0x0000},{0x85db,0x0000},{0x85dc,0x0000}, /* ・・・・ */
    {0x85dd,0x0000},{0x85de,0x0000},{0x85df,0x0000},{0x85e0,0x0000}, /* ・・・・ */
    {0x85e1,0x0000},{0x85e2,0x0000},{0x85e3,0x0000},{0x85e4,0x0000}, /* ・・・・ */
    {0x85e5,0x0000},{0x85e6,0x0000},{0x85e7,0x0000},{0x85e8,0x0000}, /* ・・・・ */
    {0x85e9,0x0000},{0x85ea,0x0000},{0x85eb,0x0000},{0x85ec,0x0000}, /* ・・・・ */
    {0x85ed,0x0000},{0x85ee,0x0000},{0x85ef,0x0000},{0x85f0,0x0000}, /* ・・・・ */
    {0x85f1,0x0000},{0x85f2,0x0000},{0x85f3,0x0000},{0x85f4,0x0000}, /* ・・・・ */
    {0x8640,0x876f},{0x8641,0x0000},{0x8642,0x8770},{0x8643,0x0000}, /* ㎜・㎝・ */
    {0x8644,0x0000},{0x8646,0x8775},{0x8647,0x0000},{0x8648,0x8771}, /* ・㎡・㎞ */
    {0x8649,0x0000},{0x864a,0x8772},{0x864c,0x8773},{0x864d,0x8774}, /* ・㎎㎏㏄ */
    {0x864e,0x0000},{0x864f,0x0000},{0x8650,0x0000},{0x8651,0x0000}, /* ・・・・ */
    {0x8652,0x0000},{0x8653,0x0000},{0x8654,0x0000},{0x8655,0x0000}, /* ・・・・ */
    {0x8656,0x0000},{0x8657,0x0000},{0x8658,0x0000},{0x8659,0x0000}, /* ・・・・ */
    {0x865a,0x0000},{0x865b,0x0000},{0x865c,0x0000},{0x869b,0x8782}, /* ・・・№ */
    {0x869c,0x8783},{0x869d,0x8784},{0x869f,0x0000},{0x86a0,0x0000}, /* ㏍℡・・ */
    {0x86a1,0x0000},{0x86a2,0x0000},{0x86a3,0x0000},{0x86a4,0x0000}, /* ・・・・ */
    {0x86a5,0x0000},{0x86a6,0x0000},{0x86b3,0x0000},{0x86b4,0x0000}, /* ・・・・ */
    {0x86b5,0x0000},{0x86c7,0x0000},{0x86c8,0x0000},{0x86c9,0x0000}, /* ・・・・ */
    {0x86ca,0x0000},{0x86cb,0x0000},{0x86cc,0x0000},{0x86cd,0x0000}, /* ・・・・ */
    {0x86cf,0x0000},{0x86d0,0x0000},{0x86d1,0x0000},{0x86d2,0x0000}, /* ・・・・ */
    {0x8740,0x0000},{0x8741,0x0000},{0x8742,0x0000},{0x8743,0x0000}, /* ・・・・ */
    {0x8744,0x0000},{0x8745,0x0000},{0x8746,0x0000},{0x8747,0x0000}, /* ・・・・ */
    {0x8748,0x0000},{0x8749,0x0000},{0x874a,0x0000},{0x874b,0x878c}, /* ・・・㈹ */
    {0x874c,0x0000},{0x874d,0x878a},{0x874e,0x0000},{0x874f,0x0000}, /* ・㈱・・ */
    {0x8750,0x878b},{0x8751,0x0000},{0x8752,0x0000},{0x8753,0x0000}, /* ㈲・・・ */
    {0x8754,0x0000},{0x8755,0x0000},{0x8756,0x0000},{0x8757,0x0000}, /* ・・・・ */
    {0x8758,0x0000},{0x8793,0x8785},{0x8794,0x8786},{0x8795,0x8787}, /* ・㊤㊥㊦ */
    {0x8796,0x8788},{0x8797,0x8789},{0x8798,0x0000},{0x8799,0x0000}, /* ㊧㊨・・ */
    {0x879a,0x0000},{0x879b,0x0000},{0x879c,0x0000},{0x879e,0x0000}, /* ・・・・ */
    {0x879f,0x875f},{0x87a0,0x8761},{0x87a1,0x8762},{0x87a2,0x8760}, /* ㍉㌢㍍㌔ */
    {0x87a3,0x0000},{0x87a4,0x0000},{0x87a5,0x0000},{0x87a6,0x0000}, /* ・・・・ */
    {0x87a7,0x8765},{0x87a8,0x8766},{0x87a9,0x8763},{0x87aa,0x0000}, /* ㌃㌶㌘・ */
    {0x87ab,0x8764},{0x87ac,0x8767},{0x87ad,0x876d},{0x87ae,0x0000}, /* ㌧㍑㍊・ */
    {0x87af,0x8768},{0x87b0,0x8769},{0x87b1,0x0000},{0x87b2,0x876b}, /* ㍗㌍・㌣ */
    {0x87b3,0x876a},{0x87b4,0x876e},{0x87b5,0x876c},{0x87bd,0x0000}, /* ㌦㌻㌫・ */
    {0x87be,0x0000},{0x87bf,0x0000},{0x87c0,0x0000},{0x87c1,0x0000}, /* ・・・・ */
    {0x87e5,0x878d},{0x87e6,0x878e},{0x87e7,0x878f},{0x87e8,0x877e}, /* ㍾㍽㍼㍻ */
    {0x87fa,0x0000},{0x8840,0x8793},{0x8841,0x8798},{0x8842,0x8799}, /* ・∮∟⊿ */
    {0x8854,0x8780},{0x8855,0x8781},{0x8868,0x0000},{0x886a,0x0000}, /* 〝〟・・ */
    {0x886b,0x0000},{0x886c,0x0000},{0x886d,0x0000},                 /* ・・・   */
};

unsigned short bsearch_wincode(unsigned short maccode)
{
    int left, right, middle;
    left = 0;
    right = sizeof(m2w) / sizeof(m2w[0]) - 1;
    while (left < right) {
        middle = (left + right) / 2;
        if (m2w[middle].maccode < maccode)
            left = middle + 1;
        else
            right = middle;
    }
    if (m2w[left].maccode == maccode)
        return m2w[left].wincode;
    return maccode;
}

void maccode2wincode(void)
{
    unsigned char c;
    unsigned short m, s;
    int ch, mode = 0;
    while ((ch = fgetc(stdin)) != EOF) {
        c = ch;
        switch (mode) {
            case 0:
                if (iskanji(c)) {
                    m = (c << 8) & 0xFF00;
                    mode = 1;
                } else {
                    putchar(c);
                }
                break;
            case 1:
                m |= c;
                if (iskanji2(c)) {
                    s = bsearch_wincode(m);
                    if (s) {
                        putchar((char)(s >> 8));
                        putchar((char)(s & 0xFF));
                    } else {
                        putchar(0x81);
                        putchar(0x45);
                    }
                } else {
                    fprintf(stderr, "不正な文字 >> 0x%04x\n", m);
                }
                mode = 0;
                break;
            default:
                break;
        }
    }
}

int main()
{
    maccode2wincode();
    return 0;
}