小春网

 找回密码
 注册账号
查看: 1380|回复: 2
收起左侧

[IT 交流] 把Unicode转换为GB18030

[复制链接]
发表于 2011-8-28 13:56:43 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?注册账号

x
本帖最后由 憨吃迷糊睡 于 2011-8-28 14:11 编辑

转载请标明出处:小春论坛http://www.incnjp.com/thread-1651043-1-1.html

// libiconv_test.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

//////
// C:\libiconv_test\libiconv_test.cpp
#pragma comment( lib , "C:/lib_x86/lib/iconv.lib" )
#include <stdlib.h>
#include "C:/lib_x86/include/iconv.h"
#define BUFFER_SIZE 3
//////

//int _tmain(int argc, _TCHAR* argv[])
int main(int argc, char *argv[])
{
        //////
        char fromstring[BUFFER_SIZE];
        char tostring[BUFFER_SIZE];

        fromstring[0] = (char)((atoi( argv[argc-1] ) & 0xFF00) >> 8);
        fromstring[1] = (char) (atoi( argv[argc-1] ) & 0x00FF);
        fromstring[2] = '\0';

        tostring[0] = '\0';
        tostring[1] = '\0';
        tostring[2] = '\0';
        //////

        //////
        iconv_t return_value_of_iconv_open;
        const char* tocode;
        const char* fromcode;

        int return_value_of_iconvctl;
        iconv_t iconvctl_cd;
        int request;
        //void * argument;
        int argument;

        size_t return_value_of_iconv;
        iconv_t iconv_cd;
        const char* inbuf = fromstring;
        size_t inbytesleft = BUFFER_SIZE;
        char* outbuf = tostring;
        size_t outbytesleft= BUFFER_SIZE;

        int return_value_of_iconv_close;
        iconv_t iconv_close_cd;
        //////

        //////
        return_value_of_iconv_open = iconv_open (
                tocode   = "GB18030",
                fromcode = "UTF-16"
                );

        return_value_of_iconvctl = iconvctl (
                iconvctl_cd = return_value_of_iconv_open,
                request = ICONV_GET_DISCARD_ILSEQ,
                &argument
                );

        return_value_of_iconv = iconv (
                iconv_cd = return_value_of_iconv_open,
                &inbuf,
                &inbytesleft,
                &outbuf,
                &outbytesleft
                );

        return_value_of_iconv_close = iconv_close (
                iconv_close_cd = return_value_of_iconv_open
                );
        //////

        //////
        if( (fromstring[0] & 0x0f) == (fromstring[0] & 0xff) )
        {
                if( (fromstring[0] & 0xf0) == (fromstring[0] & 0xff) )
                {
                        printf("00"); //0x00
                }
                else
                {
                        printf("0%X",fromstring[0] & 0xff); //0x0x
                }
        }
        else
        {
                printf("%X",fromstring[0] & 0xff); //0xxx
        }

        if( (fromstring[1] & 0x0f) == (fromstring[1] & 0xff) )
        {
                if( (fromstring[1] & 0xf0) == (fromstring[1] & 0xff) )
                {
                        printf("00"); //0x00
                }
                else
                {
                        printf("0%X",fromstring[1] & 0xff); //0x0x
                }
        }
        else
        {
                printf("%X",fromstring[1] & 0xff); //0xxx
        }

        printf(",");

        if( (tostring[0] & 0x0f) == (tostring[0] & 0xff) )
        {
                if( (tostring[0] & 0xf0) == (tostring[0] & 0xff) )
                {
                        printf("00"); //0x00
                }
                else
                {
                        printf("0%X",tostring[0] & 0xff); //0x0x
                }
        }
        else
        {
                printf("%X",tostring[0] & 0xff); //0xxx
        }

        if( (tostring[1] & 0x0f) == (tostring[1] & 0xff) )
        {
                if( (tostring[1] & 0xf0) == (tostring[1] & 0xff) )
                {
                        printf("00"); //0x00
                }
                else
                {
                        printf("0%X",tostring[1] & 0xff); //0x0x
                }
        }
        else
        {
                printf("%X",tostring[1] & 0xff); //0xxx
        }

        printf("\n");
        //////

        return 0;
}
 楼主| 发表于 2011-8-28 14:07:49 | 显示全部楼层
本帖最后由 憨吃迷糊睡 于 2011-8-28 14:18 编辑

编译方法:

1)先从GNU网站下载libiconv
http://www.gnu.org/software/libiconv/
注意该库遵从LGPL协议,发布DLL时请把该协议的拷贝一起发放
gnu-head-sm.jpg

2)参考下面CSDN博客的文章,生成iconv.h、iconv.lib、iconv.dll
其中头文件和LIB文件是编译时使用的,DLL是运行时使用的
http://blog.csdn.net/leitianjun/article/details/5605410

※迷糊编译时,直接用下面的命令就OK了,没有对下载文件做任何修改
nmake -f Makefile.msvc NO_NLS=1 DLL=1 MFLAGS=-MD PREFIX="c:/lib_x86" IIPREFIX="c:/lib_x86" > nmake.log

3)编译1楼的代码生成libiconv_test.exe

4)用下面的批处理得到libiconv_test.csv【Unicode和GB18030的映射表】
libiconv_test        1        >>libiconv_test.csv
libiconv_test        2        >>libiconv_test.csv
libiconv_test        3        >>libiconv_test.csv
……
libiconv_test        65535        >>libiconv_test.csv

回复

使用道具 举报

发表于 2011-8-29 10:22:23 | 显示全部楼层
有什么用啊这个东西。。。。。。。。。。。。。。。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号

本版积分规则

小春网
常务客服微信
微信订阅号
手机客户端
扫一扫,查看更方便! 快速回复 返回顶部 返回列表