急!!!unicode如何转换成gb?

porschetttt 2004-11-21 12:39:40
unicode如何转换成gb?我数据库中是unicode编码,但在c++builder中是乱码!怎么使数据库的东西正常显示?
...全文
188 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
porschetttt 2004-11-25
  • 打赏
  • 举报
回复
解决了!!!谢谢
porschetttt 2004-11-24
  • 打赏
  • 举报
回复
有人会吗
lzlbj 2004-11-22
  • 打赏
  • 举报
回复
ji
hy1080 2004-11-22
  • 打赏
  • 举报
回复
来晚了
porschetttt 2004-11-22
  • 打赏
  • 举报
回复
怎样设c++builder客户端字符集呢?
我不懂电脑 2004-11-22
  • 打赏
  • 举报
回复
数据库中是unicode编码,但在c++builder中是乱码

这个应该是把你c++builder客户端的机器字符集和数据库服务器的字符集设置成一致
constantine 2004-11-22
  • 打赏
  • 举报
回复
将unicode(utf-8)编码转换为gb2312编码
提交日期:2004-3-28 Aurhor:tommcat

关键词:unicodeutf-8编码 gb2312编码 编码转换
本人在实际编程中遇到这个问题,后来终于查到了编码的规则,于是写了一个算法来实现unicode->gb2312的转换,现在将程序代码公布如下:
procedure unicode2gb(const unicodestr:string; var GbStr:String);
var SourceLength:integer;
DoneLength:integer;
AscNo:integer;
Byte1,Byte2,Byte3:integer;
begin
GbStr:='';
if Trim(unicodestr)='' then exit;

SourceLength:=Length(UnicodeStr);
DoneLength:=1;
repeat
AscNo:=ord(UnicodeStr[DoneLength]);
case (AscNo and $E0) of
$E0:begin
Byte1:=(AscNo and $0f) shl 12;
Inc(DoneLength);
if DoneLength>SourceLength then break;
AscNo:=ord(UnicodeStr[DoneLength]);
Byte2:=(AscNo and $3f) shl 6;
Inc(DoneLength);
if DoneLength>SourceLength then break;
AscNo:=ord(UnicodeStr[DoneLength]);
Byte3:=AscNo and $3f;
end;
$C0:begin
Byte1:=(AscNo and $1f) shl 6;
Inc(DoneLength);
if DoneLength>SourceLength then break;
AscNo:=ord(UnicodeStr[DoneLength]);
Byte2:=(AscNo and $3f);
Byte3:=0;
end;
0..$bf:begin
Byte1:=AscNo;
Byte2:=0;
Byte3:=0;
end;
end;//case;
GbStr:=GBStr+widechar(Byte1+Byte2+Byte3);
Inc(DoneLength);
if DoneLength>SourceLength then break;
until DoneLength>=SourceLength;
end;

constantine 2004-11-22
  • 打赏
  • 举报
回复
Unicode,GB,GBK转换
提交日期:2003-8-7

关键词:UnicodeGB,GBK
Unicode格式:

我们都知道,Windows2000的记事本,可以保存数据为Unicode的格式,那么在记事本中输入的文字,如何保存到文件里面呢?

原来,Unicode的文本和数据,在开始的时候,会有两个字节的标记:FF FE,如果一个Txt文件,开头两个字节是FF FE,那么记事本就认为它是Unicode的格式!此后,每一个符号数据都是以两个字节来保存的!因此,如果你输入了ab两个字母,那么文件的长度就是:2 + 4=6Byte!

MultiByteToWideChar

WideCharToMultiByte

把一个数字如8bd5变成Unicode字符:

Result:=WideChar($8bd5)

 

function GB2Unicode(GB:string):string;

var

s: string;

i, j, k: integer;

a: array [1..1000] of char;

begin

s:='';

StringToWideChar(GB, @(a[1]), 500);

i:=1;

while ((a<>#0) or (a[i+1]<>#0)) do begin

j:=Integer(a); k:=Integer(a[i+1]);

s:=s+Copy(Format('%X ',[k*$100+j+$10000]) ,2,4);

i:=i+2;

end;

Result:=s;

end;

简体和繁体转换:

请参看MSDN的API

LCMapString

它可以实现GB和big5和Unicode之间的转换,不需要什么对照表。

yhec 2004-11-22
  • 打赏
  • 举报
回复
gz
h98458 2004-11-21
  • 打赏
  • 举报
回复
Unicode和ANSI字符串的转换

前面两个是输入字符串和它的长度(用 strlen/wcslen 获得),
后面两个是输出字符串,和它的缓冲区大小。
返回值是成功转换的字符个数。

如果输出字符串的缓冲区大小为 0,则返回需要的输出字符串缓冲区大小。

如果返回值是0,则表示出错(很可能是输出字符串缓冲区太小),
可以用 GetLastError() 获得进一步错误信息。

#include <winnls.h> //如果需要的话
//---------------------------------------------------------------------------
inline int Unicode2Ansi(wchar_t* wStr, int wcs, char* cStr, int mbs)
{
if(mbs == 0) wcs = -1;
int i = WideCharToMultiByte(CP_ACP, WC_COMPOSITECHECK, wStr, wcs, cStr, mbs, NULL, NULL);
if(mbs != 0) cStr[i] = 0x00;

return i;
}
//---------------------------------------------------------------------------
inline int Ansi2Unicode(char* cStr, int mbs, wchar_t* wStr, int wcs)
{
if(wcs == 0) mbs = -1;
int i = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, cStr, mbs, wStr, wcs);
if(wcs != 0) wStr[i] = 0x0000;

return i;
}
//---------------------------------------------------------------------------

13,825

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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