[an error occurred while processing this directive] [an error occurred while processing this directive]

カタカナひらがな変換

Sjift JIS (CP932, SJIS)版

#include <iostream>
#include <string>
#include <cstdio>

const int SJIS_GANA_XA = 0x829F;
const int SJIS_GANA_MI = 0x82DD;
const int SJIS_GANA_MU = 0x82DE;
const int SJIS_GANA_NN = 0x82F1;

const int SJIS_KANA_XA = 0x8340;
const int SJIS_KANA_MI = 0x837E;
const int SJIS_KANA_MU = 0x8380;
const int SJIS_KANA_NN = 0x8393;

/**
  * Convert kana code to gana code if possible, otherwise returns -1
  * カタカナ 1 文字からひらがな 1 文字に変換します。変換出来ないときは -1 を返します。
  */
int kanaToGanaSJIS(int code){
  if(SJIS_KANA_XA <= code && code <= SJIS_KANA_MI){
    return code - SJIS_KANA_XA + SJIS_GANA_XA;
  } else if(SJIS_KANA_MU <= code && code <= SJIS_KANA_NN){
    return code - SJIS_KANA_NN + SJIS_GANA_NN;
  }
  return -1;
}


/**
  * 文字列での変換です。
  */
void kanasToGanasSJIS(std::string& s0){
  for(int i = 0; i < s0.length()-1; i++){
    int c0 = (unsigned char)s0[i];
    if((0x80 <= c0 && c0 < 0xA0) || 0xE0 <= c0){
      int c1 = (unsigned char)s0[i+1];
      int newCode = kanaToGanaSJIS(c0 << 8 | c1);
      if(newCode >= 0){
        s0[i] = (0xFF00 & newCode) >> 8;
        s0[i+1] = 0xFF & newCode;
      }
      i++;
    }
  }
}


/**
  * こんにちは。これはしふとJISの文字列です。半角カタカナガマジッテモ大丈夫。更新。表示。わをん
  */
int main(void){
  std::string s ="こんにちは。これはシフトJISの文字列です。半角カタカナガマジッテモ大丈夫。\更\新。\表\示。ワヲン";
  std::cout << s << std::endl;
  kanasToGanasSJIS(s);
  std::cout << s << std::endl;
}
[an error occurred while processing this directive]