TOP C++ Visual C++ VARIANT Kanji

Visual C++

String^ to wchar_t *

#pragma warning ( push )
#pragma warning ( disable: 4365 )
#include <vcclr.h>  // PtrToStringChars
#pragma warning ( pop )
pin_ptr<const wchar_t> fname = PtrToStringChars(text_box0->Text);

sjis2UTF16

  1. locale設定必須デフォルトは"C"なので"Japanese"に変える。忘れると単にバイトの間に0x00が入る。
  2. 変換後のバッファサイズをバッファ長0であらかじめ求める
  3. 最後の0が付かないので2バイト多めにmalloc
  4. fgetwsで書き出す場合、バイナリーでオープンしないと、マルチバイトに戻る。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <locale.h>
#include <wchar.h>
wchar_t * char2wchar_t(char * orig){
    size_t orig_size = strlen(orig) + 1;
    size_t new_size;
    setlocale(LC_ALL, "Japanese");
    mbstowcs_s(&new_size, NULL,0,orig, 0);
    new_size++;
    size_t converted_chars = 0;
    wchar_t * wcstring = (wchar_t*) malloc(new_size * sizeof(wchar_t));
    mbstowcs_s(&converted_chars, wcstring, new_size, orig, _TRUNCATE);
//    printf(orig);
//    wprintf(wcstring);
    return wcstring;
}
#define LINE_BUF_SIZE 1000    
void test(char* fin, char * fount){
    char * p;
    char cline[LINE_BUF_SIZE];
    fopen_s(&fpr,fin,"r");
    fopen_s(&fpw,"converted.txt","wb");
    do {
        p = fgets(cline, LINE_BUF_SIZE, fpr);
        if(p != NULL){
            wchar_t* wp=char2wchar_t(cline);
            fputws(wp,fpw);
            free(wp);
        }
    } while (p != NULL);
    fclose(fpr);
    fclose(fpw);
}

UTF16->sjis

  1. UTF16のオープンはバイナリーで行う。
  2. stdinの場合も
  3. wcstombs_sの返す文字数はchar単位で合ってるので2倍する必要なし。CRLFも入っている。
  4. ただし、終端の\0は入っていないので、+1する
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <locale.h>
#include <wchar.h>
char * utf16le2sjis(wchar_t * orig){
	setlocale(LC_ALL, "Japanese");
	size_t orig_size;
	size_t new_size;
	wcstombs_s(&new_size, NULL, 0, orig, 0);
	orig_size = wcslen(orig) + 1;
	size_t converted_chars = 0;
	//new_size*=2; // not necessary!!!
	// printf("%d %d",orig_size,new_size);
	new_size++; // for terminating \0
	char * nstring = (char *) malloc(new_size);
	wcstombs_s(&converted_chars, nstring, new_size, orig, _TRUNCATE);
	return nstring;
}
#include <io.h>		// setmode()
#include <fcntl.h> // O_BINARY
void test() {
	FILE * fpr=NULL;
	FILE * fpw=NULL;

	char * p;
	wchar_t * wp;
#define LINE_BUF_SIZE 1000
	char cline[LINE_BUF_SIZE];
	wchar_t wline[LINE_BUF_SIZE];

	if (fname==NULL){
		fpr=stdin;
		if( _setmode ( _fileno ( stdin ), O_BINARY ) == -1 ){
			perror ( "Cannot set stdin to binary mode" );
			exit(1);
		}
	} else {
		fopen_s(&fpr,fname,"rb");
		if(fpr == NULL) exit(1);
	}
	fprintf(stderr,"UTF16LE->sjis\n");
	fopen_s(&fpw,"converted.sjis","w");
	do {
		wp = fgetws(wline, LINE_BUF_SIZE, fpr);
		if(wp != NULL){
			char* cp=utf16le2sjis(wline);
			printf("%s",cp);
			fputs(cp,fpw);
			free(cp);
		}
	} while (wp != NULL);

	if(fpr != stdin){
		fclose(fpr); fpr = NULL;
	}
	if(fpw != NULL) fclose(fpw); fpw=NULL;
}


char*->BSTR

BSTR _bstr_t
#include <comutil.h> // _bstr_t
//#pragma comment(lib, "comsupp.lib") // _bstr_t
#pragma comment(lib, "comsuppw.lib") // _bstr_t

char msg[]="Hello";
_bstr_t temp(msg);
//temp.GetBSTR(); // BSTR
BSTR b=temp.copy(true);

SampleCode

The Basics of UTF-8 utf8->utf16/32,utf16/32->utf8

管理人/副管理人のみ編集できます