宽字符处理问题

老赵同志QQ 2005-05-14 09:07:04
下面的两个文本框的内容怎么会不一样啊!我感觉太奇怪了。请教各位大侠!
WideString test= "我是中国人";
wchar_t zwb[5] ;
StrCopy( zwb ,test.c_bstr()) ;
USHORT tests[5];
tests[0]='我';
tests[1]='是';
tests[2]='中';
tests[3]='国';
tests[4]='人';
/*
test[1]='我';
test[2]='是';
test[3]='中';
test[4]='国';
test[5]='人';
*/

for ( int i = 1 ;i<test.Length()+1;i++ )
{
Edit1->Text = Edit1->Text +"/" + IntToStr(test[i]) ;
}

// Edit2->Text = test;
Edit2->Text = IntToStr(tests[0])+"/"+IntToStr(tests[1])+"/"+IntToStr(tests[2])+"/"+IntToStr(tests[3])+"/"+IntToStr(tests[4])+"/";
...全文
260 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
老赵同志QQ 2005-05-16
  • 打赏
  • 举报
回复
up
老赵同志QQ 2005-05-16
  • 打赏
  • 举报
回复
目前我要传的值就是GBK值;怎样才能把字符串(WideString)中的字符的GBK值读出来呢!
leonatcs 2005-05-16
  • 打赏
  • 举报
回复
一开始没搞明白你要什么。

AnsiString zhong("中国");

wchar_t wcDest[10];
zhong.WideChar(wcDest,4); //wcDest里面是Unicode
ShowMessage(IntToHex(wcDest[1],4)); //国是56FD

unsigned short de[10];
char* cde = (char *)de;
strcpy(cde,zhong.c_str());//cde里面是两两字节颠倒的GBK
for(int i=0; i<=2/*有两个汉字*/; i+=2) //每个汉字的两个字节互换
{ char temp;
temp = cde[i];
cde[i] = cde[i+1];
cde[i+1] = temp;
}//现在cde里面是GBK了

ShowMessage(IntToHex(de[1],4)); //国是B9FA
leonatcs 2005-05-15
  • 打赏
  • 举报
回复
AnsiString::WideChar

Converts the AnsiString to a wide-character array.

wchar_t* __fastcall WideChar(wchar_t* dest, int destSize) const;

Description

WideChar converts the AnsiString to an array of wide characters, and returns the resulting wide-character array.

dest is a buffer the caller allocates to hold the return value. Use the WideCharBufSize method to determine the size needed to hold the entire string value.

destSize is the size, in bytes, of dest.

WideChar returns a pointer to the wide-character array that is written to dest.
leonatcs 2005-05-15
  • 打赏
  • 举报
回复
wchar_t wcDest[5];
AnsiString("就用这个").WideChar(wcDest,8);
MessageBoxW(Handle,wcDest,L"title",MB_OK);

僵哥 2005-05-15
  • 打赏
  • 举报
回复
另外楼主在使用strcpy时,对于全汉字没有什么问题,但是如果中间存在ASCII码值小于127的,那么在WideChar里面的两个字节就会成为如L'D',那么其值就相于'D','\0'这样子两个字节构成,在使用strcpy里面就会在拷贝到'\0'的时候,就认为该字符串已经结束了,例:
WideString s=L"ABCDEFG";
wchar_t c[8];
memset(c,0,8*sizeof(wchar_t));
strcpy(c,s.c_bstr());
结果:
c[0] : L'A'
c[1] : L'\0'
c[2] : L'\0'
...
c[7] : L'\0'

在这里对WideChar进行操作,那么这个strcpy就应该当改为wcscpy,或者最好的办法是使用memcpy。
僵哥 2005-05-15
  • 打赏
  • 举报
回复
USHORT tests[5];
tests[0]='我';
tests[1]='是';
tests[2]='中';
tests[3]='国';
tests[4]='人';
这样子赋值,那么传递过去的是AnsiString,比如'中',它是GBK编码值为0xD6D0,那么传递给tests[2]的值就是0xD6D0(即54992),而相对的L"中"(WideChar),则是Unicode编码值为0x4E2D(即20013),所以楼主输出之后得到了两个不同的值。
老赵同志QQ 2005-05-14
  • 打赏
  • 举报
回复
怎么没有朋友肯帮我啊!!!!!up
老赵同志QQ 2005-05-14
  • 打赏
  • 举报
回复
我在调用别人的DLL;要求我传的参数是一个USHORT型的数组;
USHORT[]={'我','是','中','国','人'} --->这样是可以的;但我的内容(我是中国人)是从一个String变量中得到;我想了很多办法都没得实现!请各位高手帮帮忙!!!!!

1,317

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder 网络及通讯开发
社区管理员
  • 网络及通讯开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧