小春网

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

[IT 交流] 【小春首发】把Unicode转换为ASCIICode

[复制链接]
发表于 2011-8-18 21:54:59 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 憨吃迷糊睡 于 2011-8-18 21:56 编辑


// ConvertUnicodeToASCIICode.cpp : Convert Unicode to ASCIICode.

// #include "StdAfx.h"

#include <windows.h>
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
        ////// Program_Begin //////
        // Define the locale : Chinese (Simplified)_People's Republic of China.936
        #pragma setlocale("chs")
        char * return_value_of_setlocale = setlocale( LC_ALL, "chs" );
        ////// Program_Begin //////

        // Variables Declarations_Start //
        wchar_t        UnicodeText  [2];                                                // The Unicode string to convert ( INPUT  )
        char        ASCIICodeText[3];                                                // The converted string          ( OUTPUT )

        memset( UnicodeText  ,0,sizeof(UnicodeText  ));        // ZeroMemory
        memset( ASCIICodeText,0,sizeof(ASCIICodeText));        // ZeroMemory

        UnicodeText[0] = atoi( argv[argc-1] );                        // The Unicode to be convert     ( = the first parameter of the program )
        UnicodeText[1] = '\0';                                                        // The terminating null character( = \0 )
        // Variables Declarations_End //

        // WideCharToMultiByte Variables Declarations_Start //
        UINT        WideCharToMultiByte_CodePage;                        // Code page to use in performing the conversion
        DWORD        WideCharToMultiByte_dwFlags;                        // Flags indicating the conversion type
        LPCWSTR        WideCharToMultiByte_lpWideCharStr;                // Pointer to the Unicode string to convert
        int                WideCharToMultiByte_cchWideChar;                // Size, in characters, of the string indicated by lpWideCharStr
        LPSTR        WideCharToMultiByte_lpMultiByteStr;                // Pointer to a buffer that receives the converted string
        int                WideCharToMultiByte_cbMultiByte;                // Size, in bytes, of the buffer indicated by lpMultiByteStr
        LPCSTR        WideCharToMultiByte_lpDefaultChar;                // Pointer to the character to use if a character cannot be represented in the specified code page
        LPBOOL        WideCharToMultiByte_lpUsedDefaultChar;        // Pointer to a flag that indicates if the function has used a default character in the conversion
        // WideCharToMultiByte Variables Declarations_End //

        // Maps a UTF-16 (wide character) string to a new character string
        // 1) Get the required buffer size
        // 2) Then Convert UnicodeText to ASCIICodeText

        // 1) Get the required buffer size -> WideCharToMultiByte_cbMultiByte = 0x03(FIX VALUE)
        /*WideCharToMultiByte_CodePage                        = CP_ACP;                // The system default Windows ANSI code page
        WideCharToMultiByte_dwFlags                                = 0;                        // The conversion type is 0
        WideCharToMultiByte_lpWideCharStr                = UnicodeText;        // Pointer to the Unicode string to convert
        WideCharToMultiByte_cchWideChar                        = -1;                        // Processes the entire input string, including the terminating null character
        WideCharToMultiByte_lpMultiByteStr                = NULL;                        // Pointer to a buffer that receives the converted string
        WideCharToMultiByte_cbMultiByte                        = 0;                        // The required buffer size for lpMultiByteStr and makes no use of the output parameter itself
        WideCharToMultiByte_lpDefaultChar                = NULL;                        // Optional
        WideCharToMultiByte_lpUsedDefaultChar        = NULL;                        // Optional
        WideCharToMultiByte_cbMultiByte = WideCharToMultiByte(
                WideCharToMultiByte_CodePage,                                                // Code page to use in performing the conversion
                WideCharToMultiByte_dwFlags,                                                // Flags indicating the conversion type
                WideCharToMultiByte_lpWideCharStr,                                        // Pointer to the Unicode string to convert
                WideCharToMultiByte_cchWideChar,                                        // Size, in characters, of the string indicated by lpWideCharStr
                WideCharToMultiByte_lpMultiByteStr,                                        // Pointer to a buffer that receives the converted string
                WideCharToMultiByte_cbMultiByte,                                        // Size, in bytes, of the buffer indicated by lpMultiByteStr
                WideCharToMultiByte_lpDefaultChar,                                        // Pointer to the character to use if a character cannot be represented in the specified code page
                WideCharToMultiByte_lpUsedDefaultChar                                // Pointer to a flag that indicates if the function has used a default character in the conversion
                );*/

        // 2) Then Convert UnicodeText to ASCIICodeText
        WideCharToMultiByte_CodePage                        = CP_ACP;                // The system default Windows ANSI code page
        WideCharToMultiByte_dwFlags                                = 0;                        // The conversion type is 0
        WideCharToMultiByte_lpWideCharStr                = UnicodeText;        // Pointer to the Unicode string to convert
        WideCharToMultiByte_cchWideChar                        = -1;                        // Processes the entire input string, including the terminating null character
        WideCharToMultiByte_lpMultiByteStr                = ASCIICodeText;//*Pointer to a buffer that receives the converted string
        WideCharToMultiByte_cbMultiByte                        = 0x03;                        //*The required buffer size(FIX VALUE)
        WideCharToMultiByte_lpDefaultChar                = NULL;                        // Optional
        WideCharToMultiByte_lpUsedDefaultChar        = NULL;                        // Optional
        WideCharToMultiByte_cbMultiByte = WideCharToMultiByte (
                WideCharToMultiByte_CodePage,                                                // Code page to use in performing the conversion
                WideCharToMultiByte_dwFlags,                                                // Flags indicating the conversion type
                WideCharToMultiByte_lpWideCharStr,                                        // Pointer to the Unicode string to convert
                WideCharToMultiByte_cchWideChar,                                        // Size, in characters, of the string indicated by lpWideCharStr
                WideCharToMultiByte_lpMultiByteStr,                                        // Pointer to a buffer that receives the converted string
                WideCharToMultiByte_cbMultiByte,                                        // Size, in bytes, of the buffer indicated by lpMultiByteStr
                WideCharToMultiByte_lpDefaultChar,                                        // Pointer to the character to use if a character cannot be represented in the specified code page
                WideCharToMultiByte_lpUsedDefaultChar                                // Pointer to a flag that indicates if the function has used a default character in the conversion
                );

        ////// Program_End //////
        if((UnicodeText[0]&0X0FFF)==UnicodeText[0])
        {
                if((UnicodeText[0]&0XF0FF)==UnicodeText[0])
                {
                        if((UnicodeText[0]&0XFF0F)==UnicodeText[0])
                        {
                                if((UnicodeText[0]&0XFFF0)==UnicodeText[0])
                                {
                                        printf("0000"                                                );//0x0000
                                }
                                else
                                {
                                        printf("000%X",UnicodeText[0]&0XFFFF);//0x000X
                                }                       
                        }
                        else
                        {
                                        printf("00%X", UnicodeText[0]&0XFFFF);//0x00XX
                        }
                }
                else
                {
                                        printf("0%X",  UnicodeText[0]&0XFFFF);//0x0XXX
                }
        }
        else
        {
                                        printf("%X",   UnicodeText[0]&0XFFFF);//0xXXXX
        }

        printf(",");

        if((ASCIICodeText[0]&0X0F)==(ASCIICodeText[0]&0XFF))
        {
                if((ASCIICodeText[0]&0XF0)==(ASCIICodeText[0]&0XFF))
                {
                        printf("00"                                                  );//0x00
                }
                else
                {
                        printf("0%X",ASCIICodeText[0]&0XFF);//0x0X
                }
        }
        else
        {
                        printf("%X", ASCIICodeText[0]&0XFF);//0xXX
        }

        if((ASCIICodeText[1]&0X0F)==(ASCIICodeText[1]&0XFF))
        {
                if((ASCIICodeText[1]&0XF0)==(ASCIICodeText[1]&0XFF))
                {
                        printf("00"                                                  );//0x00
                }
                else
                {
                        printf("0%X",ASCIICodeText[1]&0XFF);//0x0X
                }
        }
        else
        {
                        printf("%X", ASCIICodeText[1]&0XFF);//0xXX
        }

        printf("\n");

        return 0;
        ////// Program_End //////
}


补充内容 (2011-8-28 14:13):
转载请标明出处:小春论坛http://www.incnjp.com/thread-1638144-1-1.html
 楼主| 发表于 2011-8-18 22:01:38 | 显示全部楼层
编译方法:cl ConvertUnicodeToASCIICode.cpp
生成ConvertUnicodeToASCIICode.exe

执行例子:ConvertUnicodeToASCIICode 38376
解释:38376是“门”这个汉字的Unicode的10进制数值
执行结果:95E8,C3C5(前面是Unicode的16进制数值,后面是“门”这个汉字的ASCII码)

做一个批处理文件(或者简单改动代码,追加上循环功能)
ConvertUnicodeToASCIICode        1        >>ConvertUnicodeToASCIICode.csv
ConvertUnicodeToASCIICode        2        >>ConvertUnicodeToASCIICode.csv
ConvertUnicodeToASCIICode        3        >>ConvertUnicodeToASCIICode.csv
...
ConvertUnicodeToASCIICode        65535        >>ConvertUnicodeToASCIICode.csv

将得到所有汉字的Unicode和ASCII码的对应关系(可以用Excel查看ConvertUnicodeToASCIICode.csv)

回复

使用道具 举报

 楼主| 发表于 2011-8-18 22:03:30 | 显示全部楼层
// Unicode2ASCIICode.cpp : The DLL Project
//

#include "stdafx.h"

#ifdef __cplusplus
#define EXPORT extern "C" __declspec (dllexport)
#else
#define EXPORT            __declspec (dllexport)
#endif // __cplusplus

EXPORT unsigned char* CALLBACK ConvertUnicodeToASCIICode(int        UnicodeValue);

static unsigned char ASCIITable[65536][2] ={
0x00,0x00,
0x01,0x00,
0x02,0x00,
0x03,0x00,
...
0x3F,0x00,
};

EXPORT unsigned char* CALLBACK ConvertUnicodeToASCIICode(int        UnicodeValue)
{
        return &ASCIITable[UnicodeValue][0];
}
回复

使用道具 举报

 楼主| 发表于 2011-8-18 22:04:35 | 显示全部楼层
// Unicode2ASCIICodeEXE.cpp : The EXE Project

#include <stdio.h>
#include <stdlib.h>

#include "C:\Work\Unicode2ASCIICode\stdafx.h"

#pragma comment(lib,"C:\\Work\\Unicode2ASCIICode\\Debug\\Unicode2ASCIICode.lib")

int main(int argc, char *argv[])
{
        char        ASCIICodeText[3];                                                // The converted string          ( OUTPUT )
        memset( ASCIICodeText,0,sizeof(ASCIICodeText));        // ZeroMemory
        ASCIICodeText[2]='\0';
       
        memcpy(ASCIICodeText,ConvertUnicodeToASCIICode(atoi( argv[argc-1] )),2);
       
        if((ASCIICodeText[0]&0X0F)==(ASCIICodeText[0]&0XFF))
        {
                if((ASCIICodeText[0]&0XF0)==(ASCIICodeText[0]&0XFF))
                {
                        printf("00"                                                  );//0x00
                }
                else
                {
                        printf("0%X",ASCIICodeText[0]&0XFF);//0x0X
                }
        }
        else
        {
                        printf("%X", ASCIICodeText[0]&0XFF);//0xXX
        }

        if((ASCIICodeText[1]&0X0F)==(ASCIICodeText[1]&0XFF))
        {
                if((ASCIICodeText[1]&0XF0)==(ASCIICodeText[1]&0XFF))
                {
                        printf("00"                                                  );//0x00
                }
                else
                {
                        printf("0%X",ASCIICodeText[1]&0XFF);//0x0X
                }
        }
        else
        {
                        printf("%X", ASCIICodeText[1]&0XFF);//0xXX
        }
       
        return 0;
}
回复

使用道具 举报

 楼主| 发表于 2011-8-18 22:11:18 | 显示全部楼层
把2楼生成的CSV文件,简单编辑一下,可以做成头文件供其他程序使用。

也可以参考3楼和4楼的代码把处理DLL化之后,供其他EXE使用。

微软提供的WideCharToMultiByte这个API可以根据当前的代码页进行转换,
但是不能跨越各种语言的操作系统(比如在中文系统下得到的是GB,在日文系统下得到的是JIS)

本示例在中文操作系统下,通过调用WideCharToMultiByte API取得所有文字的GB码之后,
将相应信息保存到DLL文件中,从而实现了在日文系统下可以得到一个文字GB码的功能。

之前在网上找类似的头文件,貌似还需要些银子才能获得。
夏休时闲来无事做了几个小程序贴上来,大家有啥金砖美玉尽管拍拍
回复

使用道具 举报

 楼主| 发表于 2011-8-18 22:16:11 | 显示全部楼层
贴完帖子百度一下,小春论坛的检索结果排在百度第一名。
嘻嘻,希望饱受Unicode、GB码、JIS码折磨的童鞋早日脱离苦海……

把Unicode转换为ASCIICode.jpg
回复

使用道具 举报

头像被屏蔽
发表于 2011-8-19 08:16:12 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽
回复

使用道具 举报

 楼主| 发表于 2011-8-19 14:11:18 | 显示全部楼层
a13352355521 发表于 2011-8-19 08:16
看不懂啊

自己试着写一个就OK了

在任何语言的Windows系统下,实现Unicode对GB码的转换,仅此而已……
回复

使用道具 举报

头像被屏蔽
发表于 2011-8-19 16:29:03 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽
回复

使用道具 举报

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

本版积分规则

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