vs2005 vc consoleアプリ
#include "stdafx.h"
#include "windows.h"
int _tmain(int argc, _TCHAR* argv[])
{
wchar_t s1[6]=_T("10.00"); //配列を5にすると10.00は初期のサイズが足らないとエラー
char s2[6];
printf("s1の文字はunicodeで :");wprintf(s1);printf("\n");
size_t z=wcslen(s1);
printf("s1の文字の長さは:");
printf("%d\n",z);
int y=WideCharToMultiByte(CP_ACP,0,LPCWSTR(s1),-1,NULL,0,NULL,NULL);//必要なバイト数を返す
printf("s1をS2に格納するのに必要なバイト数は:");
printf("%d",y);
printf("\n");
//WideCharToMultiByte(CP_ACP,0,LPCWSTR(s1),wcslen(s1),s2,y*2,"A",NULL);
WideCharToMultiByte(CP_ACP,0,LPCWSTR(s1),wcslen(s1),s2,y,NULL,NULL);//wcslen(s1)がおかしい
printf(s2);
getchar();
return 0;
結果
s1の文字はunicodeで :10.00
s1の文字の長さは:5
s1をS2に格納するのに必要なバイト数は:6
10.00フフフフフフフフフフフ1
コメント(1件)
C言語では、文字列に終端記号(0)が必要なので、配列のサイズは文字列の長さ+1が必要です。