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

shenyinhong 2009-12-14 12:08:34
最好不用加库什么的。
...全文
921 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...
资源下载链接为: https://pan.quark.cn/s/9e7ef05254f8 【久久在线FLASH系统】是一款专为久久在线网站打造的交互式Flash平台,集成了前台展示与后台管理功能,满足内容发布、管理和用户互动的需求。Flash技术曾广泛应用于网页动画和互动内容,尤其在早期互联网时代,在游戏、广告和多媒体教学等领域发挥了重要作用。该系统的核心包括以下几个关键方面: Flash技术:系统利用Flash创建动态图形、动画和交互内容,依赖Adobe Flash Player运行。其编程语言ActionScript支持面向对象开发,便于实现复杂逻辑和交互效果。 后台管理系统:作为系统的控制中心,后台支持内容上传、编辑、分类、权限设置、用户管理及数据分析,确保内容的有序更新与发布。 产品演示模块:用户可在线预览和体验产品功能,无需下载,通过交互式演示了解产品操作流程和优势。 数据库集成:系统与数据库紧密结合,用于存储Flash文件信息、用户数据及访问记录,实现高效的数据管理与检索。 安全性与优化:系统具备防止非法访问和数据泄露的安全机制,并对Flash内容进行优化,提升加载速度与用户体验。 响应式设计:尽管Flash主要用于桌面端,系统仍考虑多设备兼容性,通过响应式设计适配不同屏幕尺寸,提供一致体验。 API接口:系统支持与其他平台或服务通过API进行数据交互,如社交媒体分享、数据分析等,拓展功能边界。 用户体验:界面设计注重交互性与视觉效果,提升用户满意度和停留时间,增强平台吸引力。 版本控制:系统支持内容版本管理,便于追踪更新历史,方便内容维护与回滚。 性能监控:内置性能监控工具,实时跟踪系统负载与资源使用情况,及时发现并解决问题,保障系统稳定运行。 【久久在线FLASH系统】是一个综合性解决方案,融合了前端展示、后台管理、互动体验和数据分析等功能,体现了当时Web

70,023

社区成员

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

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