C 文字列操作 新しいページはコチラ

提供: yonewiki
移動: 案内, 検索
(文字列文字コード変換)
(文字列半角文字全角文字変換)
3,067行: 3,067行:
 
=='''文字列半角文字全角文字変換'''==
 
=='''文字列半角文字全角文字変換'''==
 
これは、標準関数では対応できませんが、ABCD…UVWXYZ@…abc…xyzをABCD…UVWXYZ@…abc…xyzに変換するような作業です。文字コードによっても求める結果や操作が違うので、ICUを使うのが手っ取り早いでしょう。自分で作成できる範囲の変換処理でもあります。
 
これは、標準関数では対応できませんが、ABCD…UVWXYZ@…abc…xyzをABCD…UVWXYZ@…abc…xyzに変換するような作業です。文字コードによっても求める結果や操作が違うので、ICUを使うのが手っ取り早いでしょう。自分で作成できる範囲の変換処理でもあります。
 +
 +
 +
以下はICUを利用した全角→半角変換です。
 +
 +
<syntaxhighlight lang="cpp" line start="1">
 +
#pragma once
 +
 +
#include "stdafx.h"
 +
#include <stdlib.h>
 +
#include <string> //std::string型定義
 +
#include <mbstring.h> //mbs***関数
 +
#include <iostream> //cpp cout etc 一般入出力関数
 +
#include <locale> //Locale関数
 +
#include <tchar.h> //TCHAR型+_tcs***関数
 +
#include "atlstr.h" //CString,CStringA,CStringWおよびLPTSTR,LPWSTR,LPSTR,LPCTSTR,LPCWSTR,LPCSTRの定義プリプロセッサ
 +
#include "atlbase.h" //同上
 +
#include "cstringt.h" //CStringTの定義プリプロセッサ
 +
 +
//_bstr_t型を利用するプリプロセッサ
 +
#include "comutil.h"
 +
#ifdef _DEBUG
 +
#pragma comment(lib, "comsuppwd.lib")
 +
#else
 +
#pragma comment(lib, "comsuppw.lib")
 +
#endif
 +
//_bstr_t
 +
 +
//ICU ucnvプリプロセッサ
 +
#include <unicode/ucnv.h>
 +
#include <unicode/translit.h>
 +
#ifdef _DEBUG
 +
//#pragma comment(lib, "icudt.lib")
 +
#pragma comment(lib, "icuind.lib")//ICU Transliterate関数を使うために必要なライブラリ 正規表現関数も
 +
#pragma comment(lib, "icuucd.lib")//ICU ucnvを使うために必要なライブラリ
 +
//#pragma comment(lib, "icuiod.lib")
 +
#else
 +
#pragma comment(lib, "icudt.lib")
 +
#pragma comment(lib, "icuind.lib")
 +
#pragma comment(lib, "icuuc.lib")
 +
#pragma comment(lib, "icuio.lib")
 +
#endif
 +
 +
 +
#include "UnicodeConverter.h"
 +
 +
 +
using namespace std;
 +
int _tmain(int argc, _TCHAR* argv[])
 +
{
 +
  _tsetlocale(LC_ALL, _T("Japanese"));
 +
 +
 +
  //★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
 +
  // 全角 → 半角変換
 +
  //★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
 +
  UConverter* ucnv;
 +
  UErrorCode errorNum = U_ZERO_ERROR;
 +
  int nStrSize;
 +
 
 +
  //変換元となる全角リテラル
 +
  std::wstring wstringResult = L"あいうえおアイウエオABCDE@;:*+/";
 +
  UnicodeString str(wstringResult.c_str());//変換関数にはUnicodeString型を使うので初期値関数を使って初期化。strは変更可能な変数である必要がある。ココでconstは駄目。
 +
 +
  //正しい変換結果確認用のリテラル
 +
  std::wstring wstringResult2 = L"あいうえおアイウエオABCDE@;:*+/";
 +
 +
  //変換処理の核となる部分
 +
  //変換タイプリテラル
 +
  const UnicodeString convert_type(L"Fullwidth-Halfwidth");
 +
  Transliterator *myTrans = myTrans->createInstance(convert_type, UTRANS_FORWARD, errorNum);//変換方法を設定
 +
  myTrans->transliterate(str);//変換
 +
 +
  ucnv = ucnv_open("shift_jis", &errorNum);
 +
  std::string stringResult(str.length() * ucnv_getMaxCharSize(ucnv),'\0');//shift_jisの最大文字サイズと文字数で仮のメモリ確保
 +
  ucnv_close(ucnv);
 +
 +
  //UnicodeStringクラスを使った変換例。Unicode→任意の文字コード。
 +
  //但しchar型へしか格納できない変数なのでUnicode→Unicodeのような無意味な変換には適さない。やれるけど…
 +
  nStrSize = str.extract(0, str.length(), &stringResult[0], "shift_jis");//stringResultにShift_JISに変換した内容を保管。
 +
  stringResult.resize(nStrSize);//変換によって判明したメモリサイズに再設定
 +
 +
 +
  //UnicodeStringのwchar_w型文字列を格納している先頭アドレスを取得できるメンバ関数。
 +
  printf("★UnicodeString変数の出力サンプル\n");
 +
  wprintf(L"%s:\n",str.getTerminatedBuffer());
 +
  printf("\n");
 +
 +
  //★半角変換+shift_jis変換のバイトコード表示にためにchar型へ代入して出力
 +
  printf("★半角変換+shift_jis変換\n");
 +
  char* pcStr2 = new char[strlen(stringResult.c_str()) + 1];
 +
  strcpy_s(pcStr2,strlen(stringResult.c_str()) + 1,stringResult.c_str());
 +
  printf("%s %d:\n",stringResult.c_str(),strlen(stringResult.c_str()) + 1);
 +
  for(int i = 0;i < (int)strlen(stringResult.c_str()) + 1;i++){
 +
    printf("%02x:",0x000000FF & *(pcStr2 + i));
 +
  }
 +
  printf("\n");
 +
  printf("\n");
 +
 +
 +
  //★無変換のバイトコード表示にためにw_char型へ代入して出力
 +
  printf("★全角無変換\n");
 +
  wchar_t* pcStr = new wchar_t[wcslen(wstringResult.c_str()) + 1];
 +
  wcscpy_s(pcStr,wcslen(wstringResult.c_str()) + 1,wstringResult.c_str());
 +
  wprintf(L"%s %d:\n",wstringResult.c_str(),wcslen(wstringResult.c_str()) + 1);
 +
  for(int i = 0;i < (int)wcslen(wstringResult.c_str()) + 1;i++){
 +
    printf("%02x:",0x0000FFFF & *(pcStr + i));
 +
  }
 +
  printf("\n");
 +
  printf("\n");
 +
 +
 +
  //★変換後確認用の手動変換リテラルのバイトコード表示にためにw_char型へ代入して出力
 +
  printf("★手動変換(確認用)\n");
 +
  wchar_t* pcStr3 = new wchar_t[wcslen(wstringResult2.c_str()) + 1];
 +
  wcscpy_s(pcStr3,wcslen(wstringResult2.c_str()) + 1,wstringResult2.c_str());
 +
  wprintf(L"%s %d:\n",wstringResult2.c_str(),wcslen(wstringResult2.c_str()) + 1);
 +
  for(int i = 0;i < (int)wcslen(wstringResult2.c_str()) + 1;i++){
 +
    printf("%02x:",0x0000FFFF & *(pcStr3 + i));
 +
  }
 +
  printf("\n");
 +
  printf("\n");
 +
 +
  //★変換処理をしたUnicodeのバイトコード表示にためにw_char型へ代入して出力
 +
  printf("★半角変換Unicode版\n");
 +
  wchar_t* pcStr4 = new wchar_t[wcslen(str.getTerminatedBuffer()) + 1];
 +
  wcscpy_s(pcStr4,wcslen(str.getTerminatedBuffer()) + 1,str.getTerminatedBuffer());
 +
  wprintf(L"%s %d:\n",str.getTerminatedBuffer(),wcslen(str.getTerminatedBuffer()) + 1);
 +
  for(int i = 0;i < (int)wcslen(str.getTerminatedBuffer()) + 1;i++){
 +
    printf("%02x:",0x0000FFFF & *(pcStr4 + i));
 +
  }
 +
  printf("\n");
 +
  printf("\n");
 +
 +
 +
}
 +
</syntaxhighlight>
  
 
=='''文字列ファイルパス操作'''==
 
=='''文字列ファイルパス操作'''==

2014年9月30日 (火) 00:00時点における版



個人用ツール
名前空間

変種
操作
案内
ツールボックス