- 日志
- 好友
- 卖家信用

- 买家信用

- 注册时间
- 2010-11-23
- 在线时间
- 小时
|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?注册账号
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
|
|