ソフトウェア開発 Win32プログラミング

JISをシフトJISに変換する

戻る


JISをシフトJISに変換するには次のような関数を使うとよいだろう。

#include <windows.h>
#include <mbstring.h>

static INT _JIS2SJIS(LPBYTE pbSrc, INT cchSrc)
{
    INT i, j;
    LPBYTE pbDest;
    WORD w;
 
    pbDest = new BYTE[cchSrc + 1];
    ZeroMemory(pbDest, cchSrc + 1);
    for(i = j = 0; i < cchSrc - 1; i++)
    {
        w = (WORD)_mbcjistojms(MAKEWORD(pbSrc[i + 1], pbSrc[i]));
        if (w != 0)
        {
            pbDest[j] = HIBYTE(w);
            pbDest[j + 1] = LOBYTE(w);
            i++;
            j += 2;
        }
        else
        {
            pbDest[j] = pbSrc[i];
            j++;
        }
    }
    CopyMemory(pbSrc, pbDest, j);
    delete[] pbDest;
    return j;
}
 
#define CODE_ASCII      0
#define CODE_8BITCODE   1
#define CODE_ZENKAKU    2
#define CODE_NULL       3
#define ESC             0x1B

LPSTR JIStoSJIS(LPCSTR pszSrc, INT cchSrc)
{
    INT i, j, ichWork, cchWork, nCode, nOldCode, cchSeq;
    LPBYTE pbDest, pbWork;
    LPSTR pszDest = new CHAR[cchSrc + 1];
    pbDest = (LPBYTE)pszDest;
 
    BOOL bFoundSeq = FALSE;
    nCode = nOldCode = CODE_ASCII;
    for(i = j = ichWork = 0; i < cchSrc; i++)
    {
        if (i + 3 <= cchSrc)
        {
            if (pszSrc[i] == ESC)
            {
                if (pszSrc[i + 1] == '$' &&
                    (pszSrc[i + 2] == 'B' || pszSrc[i + 2] == '@'))
                {
                    bFoundSeq = TRUE;
                    nOldCode = nCode;
                    nCode = CODE_ZENKAKU;
                    cchSeq = 3;
                }
                else if (pszSrc[i + 1] == '(' && pszSrc[i + 2] == 'I')
                {
                    bFoundSeq = TRUE;
                    nOldCode = nCode;
                    nCode = CODE_8BITCODE;
                    cchSeq = 3;
                }
                else if (pszSrc[i + 1] == '(' &&
                    (pszSrc[i + 2] == 'B' || pszSrc[i + 2] == 'J'))
                {
                    bFoundSeq = TRUE;
                    nOldCode = nCode;
                    nCode = CODE_ASCII;
                    cchSeq = 3;
                }
                else if (i + 6 <= cchSrc)
                {
                    if (pszSrc[i + 1] == '&' && pszSrc[i + 2] == '@' &&
                        pszSrc[i + 3] == ESC && pszSrc[i + 4] == '$' &&
                        pszSrc[i + 5] == 'B')
                    {
                        bFoundSeq = TRUE;
                        nOldCode = nCode;
                        nCode = CODE_ZENKAKU;
                        cchSeq = 6;
                    }
                }
            }
        }
 
        if (bFoundSeq)
        {
            if (ichWork < i && CODE_ZENKAKU == nOldCode)
            {
                cchWork = i - ichWork;
                pbWork = new BYTE[cchWork + 1];
                CopyMemory(pbWork, &pszSrc[ichWork], cchWork);
                pbWork[cchWork] = '\0';
                cchWork = _JIS2SJIS((LPBYTE)pbWork, cchWork);
                CopyMemory(&pbDest[j], pbWork, cchWork);
                j += cchWork;
                delete[] pbWork;
            }
            i += cchSeq - 1;
            ichWork = i + 1;
            bFoundSeq = FALSE;
        }
        else if (CODE_ASCII == nCode)
            pbDest[j++] = pszSrc[i];
        else if (CODE_8BITCODE == nCode)
            pbDest[j++] = (BYTE)(0x80 | pszSrc[i]);
    }
 
    pbDest[j] = '\0';
 
    return pszDest;
}
 
#ifdef UNITTEST
#include <stdio.h>
int main(void)
{
    CHAR buf[256];
    LPSTR pszSJIS;
    FILE *fin = fopen("a.htm", "r");
    if (fin != NULL)
    {
        while(fgets(buf, 256, fin) != NULL)
        {
            pszSJIS = JIStoSJIS(buf, lstrlenA(buf));
            printf("%s", pszSJIS);
            delete[] pszSJIS;
        }
        fclose(fin);
    }
    return 0;
}
#endif  // def UNITTEST

ソース: jis2sjis.zip


戻る

©片山博文MZ
katayama.hirofumi.mz@gmail.com

inserted by FC2 system