急求GB2312转unicode的代码(C)?

shenyinhong 2009-12-14 12:08:34
最好不用加库什么的。
...全文
923 12 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
shenyinhong 2009-12-15
  • 打赏
  • 举报
回复
我想要的是,比如汉字“啊”的GB2312编码是0xB0A1,我如何使用FT_Get_Char_Index()这个接口。
也就是怎么把0xB0A1转为“啊”的unicode。
「已注销」 2009-12-15
  • 打赏
  • 举报
回复
如果要转字符串,用mbstowcs
http://msdn.microsoft.com/en-us/library/ms859667.aspx
「已注销」 2009-12-15
  • 打赏
  • 举报
回复
mbtowc
shenyinhong 2009-12-15
  • 打赏
  • 举报
回复
只是在Window上测试,不想用到window API.
shenyinhong 2009-12-15
  • 打赏
  • 举报
回复
对,是Window平台。
因为在freetype库只找到了使用unicode描画汉字的sample,没找到使用GB2312机内码进行汉字描画的实现sample,所以决定先把G2312机内码转化为unicode。
使用unicode描画汉字的sample如下:

#include <stdio.h>
#include <ft2build.h>

#ifdef __cplusplus
#include <freetype.h>
#include <ftglyph.h>
#else
#include FT_FREETYPE_H
#include FTGLYPH_H
#endif

int main()
{
FT_Library pFTLib = NULL;
FT_Face pFTFace = NULL;
FT_Error error = 0 ;
unsigned short pText[]=L"啊";


// Init FreeType Lib to manage memory
error = FT_Init_FreeType( & pFTLib);
if (error)
{
pFTLib = 0 ;
printf( " There is some error when Init Library " );
return - 1 ;
}

// create font face from font file
error = FT_New_Face(pFTLib, "C:\\simkai.ttf" , 0 , & pFTFace);
if ( ! error)
{
FT_Set_Char_Size(pFTFace, 16 << 6 , 16 << 6 , 300 , 300 );
FT_Glyph glyph;
// load glyph
FT_Load_Glyph(pFTFace, FT_Get_Char_Index(pFTFace, *pText), FT_LOAD_DEFAULT);
error = FT_Get_Glyph(pFTFace -> glyph, & glyph);
if ( ! error)
{
// convert glyph to bitmap with 256 gray
FT_Glyph_To_Bitmap( & glyph, ft_render_mode_normal, 0 , 1 );
FT_BitmapGlyph bitmap_glyph = (FT_BitmapGlyph)glyph;
FT_Bitmap & bitmap = bitmap_glyph -> bitmap;
for ( int i = 0 ; i < bitmap.rows; ++ i)
{
for ( int j = 0 ; j < bitmap.width; ++ j)
{
// if it has gray>0 we set show it as 1, o otherwise
printf( "%d" , bitmap.buffer[i * bitmap.width + j] ? 1 : 0 );
}
printf( " \n " );
}
// free glyph
FT_Done_Glyph(glyph);
glyph = NULL;
}
// free face
FT_Done_Face(pFTFace);
pFTFace = NULL;
}

// free FreeType Lib
FT_Done_FreeType(pFTLib);
pFTLib = NULL;
}
shenyinhong 2009-12-15
  • 打赏
  • 举报
回复
谢谢楼上的,就是用数组实现啦,呵呵,一时糊涂了。
shenyinhong 2009-12-15
  • 打赏
  • 举报
回复
已解决。结贴。
shenyinhong 2009-12-15
  • 打赏
  • 举报
回复
利用iconv()函数可以把GB2312汉字转为Unicode,可怎么能使输入为GB2312 机内码呢。
源码如下:

#include "iconv.h"
#include<string.h>
#include <stdio.h>
#define OUTLEN 255
/*代码转换:从一种编码转为另一种编码*/
int code_convert(char *from_charset,char *to_charset,const char *inbuf,unsigned int inlen,
unsigned char *outbuf,unsigned int outlen)
{
iconv_t cd;
int rc;
const char **pin = &inbuf;
unsigned char **pout = &outbuf;
cd = iconv_open(to_charset,from_charset);
if (cd==0) return -1;
memset(outbuf,0,outlen);
if (iconv(cd,pin,&inlen,pout,&outlen)==-1) return -1;
iconv_close(cd);
return 0;
}

/*GB2312码转为utf-8码*/
int g2u(const char *inbuf,unsigned int inlen,unsigned char *outbuf,unsigned int outlen)
{
return code_convert("gb2312","utf-8",inbuf,inlen,outbuf,outlen);
}

void main()
{

[color=#FF0000]const char *in_gb2312 = "啊";
//这怎么修改为‘啊’的区位码0xB0A1呢.[/color]
unsigned char out[OUTLEN];
int rc;
unsigned int length_gb2312;


//gb2312码转为utf8码
length_gb2312= strlen(in_gb2312);
rc = g2u(in_gb2312,length_gb2312,out,OUTLEN);

//utf8转unicode码
unsigned short unicode;
unicode = out[0];
if (unicode >= 0xF0) {
unicode = (unsigned short) (out[0] & 0x07) << 18;
unicode |= (unsigned short) (out[1] & 0x3F) << 12;
unicode |= (unsigned short) (out[2] & 0x3F) << 6;
unicode |= (unsigned short) (out[3] & 0x3F);
} else if (unicode >= 0xE0) {
unicode = (unsigned short) (out[0] & 0x0F) << 12;
unicode |= (unsigned short) (out[1] & 0x3F) << 6;
unicode |= (unsigned short) (out[2] & 0x3F);
} else if (unicode >= 0xC0) {
unicode = (unsigned short) (out[0] & 0x1F) << 6;
unicode |= (unsigned short) (out[1] & 0x3F);
}

printf("gb2312-->utf8 out=%x \n",out);
printf("unicode=%x \n",unicode);
}


-------------
输出为:
gb2312-->utf8 out=12fe7c
unicode=554a
Press any key to continue

如给出正确答案,再加50分。


lijian22500 2009-12-15
  • 打赏
  • 举报
回复
学习!
InOner 2009-12-15
  • 打赏
  • 举报
回复
提供个资源,不用WINAPI也行,wine有相应的函数实现。。。。
ZHENG017 2009-12-14
  • 打赏
  • 举报
回复
windows平台? MultiByteToWideChar...
在Windows 10或Windows 11操作系统中,用户经常会遇到共享打印机时出现的一系列错误代码,这些错误代码可能会阻碍打印机共享功能的正常使用。常见的错误代码包括0x00000057、0x00000709和0x0000011b,这些代码通常指出了不同的问题,比如权限不足、服务未运行或配置错误等。除此之外,还有一些故障提示如“连接失败”或“内存不足”,这些都可能影响到打印机共享的稳定性。 要解决这些故障,首先要确保打印机已经正确地连接到网络,并且在需要共享的电脑上进行了设置。确保打印机驱动程序是最新的,并且在共享设置中没有错误配置。对于权限问题,需要检查网络上的用户账户是否具有足够的权限来访问共享打印机。同时,也要确保打印机服务正在运行,特别是“Print Spooler”服务,因为这是打印机共享服务的核心组件。 在某些情况下,问题可能与操作系统的更新有关,如升级到最新版的Windows 10或Windows 11后可能出现的兼容性问题。这时,可能需要查看微软的官方支持文档来获取特定的解决方案或更新。 对于错误代码0x00000057,这通常是由于没有足够的权限来访问网络打印机或其共享资源,解决方法是确保网络打印机的权限设置正确,包括在组策略中设置相应的访问权限。而0x00000709错误可能是由于打印机驱动问题或打印机端口配置错误,可以尝试重新安装或更新打印机驱动来解决。至于0x0000011b错误,这往往是因为打印机队列服务的问题,检查并重启“Print Spooler”服务通常是解决这类问题的常见手段。 至于“连接失败”或“内存不足”这类故障,通常与客户端和打印机之间的网络连接以及打印机本地资源的使用情况有关。检查网络连接,确保打印机所在的网络段没有故障或中断。同时,如果打印机的打印队列长时间得不到处理,可能会导致内存不足的情况,这时可能需要清理打印队列或增加打印机的内存配置。 为了帮助用户更快速地解决这些问题,市面上出现了各种打印机共享错误修复工具。这些工具往往通过预设的修复程序来自动检测和修正打印机共享中常见的问题。它们可以快速检查打印机驱动、网络连接以及共享设置,并且能够提供一键修复功能,大幅减少了用户自行排查和解决问题的难度。 然而,在使用这些修复工具之前,用户应确保这些工具的来源是安全可靠的,避免因使用不当的修复工具而引发其他系统安全或隐私问题。用户可以到官方平台或者信誉良好的软件提供商处下载这些工具。通过细心检查打印机的共享设置,及时更新驱动程序和服务,以及合理使用修复工具,大多数共享打印机的问题都可以得到有效的解决。

70,023

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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