/*
    第2種情報処理技術者試験    平成10年度・春期・午後 問7
                                BohYoh Shibata PREPARATION
*/

void CodeConv(char *instr, char *outstr)
{
    static char K2I[] = "\x1b\x24\x42", K2O[] = "\x1b\x28\x42",
                KAI = '\x0e', KAO = '\x0f', KSS = '\x8e';
    int cctype, i, pctype = 1 ;

    while (*instr != '\0') {
        if (*instr == KSS)
            cctype = 2;
        else if ( *instr & 0x80 )
            cctype = 3;
        else
            cctype = 1;
        if ( pctype != cctype ) {
            if (pctype == 3)
                for (i = 0; K2O[i] != '\0'; *outstr++ = K2O[i++]);
            else if (pctype == 2)
                *outstr++ = KAO;
            if (cctype == 3)
                for (i = 0; K2I[i] != '\0'; *outstr++ = K2I[i++]);
            else if (cctype == 2)
                *outstr++ = KAI;
            pctype = cctype;
        }

        if (cctype == 1)
            *outstr++ = *instr++;
        else if (cctype == 3) {
            *outstr++ = *instr++ & 0x7f;
            *outstr++ = *instr++ & 0x7f;
        } else {
            instr++ ;
            *outstr++ = *instr++ & 0x7f;
        }
    }
    if (pctype == 3)
        for (i = 0; K2O[i] != '\0'; *outstr++ = K2O[i++]);
    else if (pctype == 2)
        *outstr++ = KAO;
    *outstr = '\0';
}