求一段小程序
A0B3883EC4E9上面的字符串是unicode的编码
我想把它还原成ascii,怎么实现?
3x --------------------编程问答-------------------- 程序在FressBSD/Linux上调试通过,Windows 用户只要修改几个变量类型和头文件即可
说明:函数名称
int strUnicode2GB(char *strSourcer, char *strDest,int n)
转换Unicde字符串到GB码,返回汉字数
输入( Unicode 源串,GB2312/ASCII混合编码串,Unicode字节数 必须是偶数!!!)
[code:1:0a0b72ed53]
#include <sys/types.h>
const unsigned short int Unicode_GB2312[][2] =
{
/*Unicode ,GB||ASCII ,MEMO*/
0x0000,0x0040,/* 0 COMMERCIAL AT @ */
0x0001,0x00A3,/* 1 POUND SIGN £ */
0x0002,0x0024,/* 2 DOLLAR SIGN $ */
0x0003,0x00A5,/* 3 YEN SIGN ¥ */
0x0004,0x00E8,/* 4 LATIN SMALL LETTER E WITH GRAVE è */
.............
.............
码表太长了 请尽量通过 Google 查询.
0xFF5C,0xA3FC,/* '|' -> 65372 */
0xFF5D,0xA3FD,/* '}' -> 65373 */
0xFF5E,0xA1AB,/* '~' -> 65374 */
0xFFE0,0xA1E9,/* '¢' -> 65504 */
0xFFE1,0xA1EA,/* '£' -> 65505 */
0xFFE3,0xA3FE,/* ' ̄' -> 65507 */
0xFFE5,0xA3A4,/* '¥' -> 65509 */
};
u_int16_t Unicode2GBcode(u_int16_t iUnicode)
{
int i,j,n;
switch (iUnicode){
case 0x0002:
return 0x24;
break;
case 0x000a:
return 0xa;
break;
case 0x000d:
return 0xd;
break;
case 0x0040:
return 0xA1;
break;
}
if ((iUnicode>=0x20&&iUnicode<=0x5a)||(iUnicode>=0x61&&iUnicode<=0x7a)) return iUnicode;
for (i=0,j=0,n=sizeof(Unicode_GB2312)/sizeof(Unicode_GB2312[0])-1;n>0;n>>=1,++j){
if(Unicode_GB2312[i][0]==iUnicode) return Unicode_GB2312[i][1];
if (j>1){
if(Unicode_GB2312[i-1][0]==iUnicode) return Unicode_GB2312[i-1][1];
if(Unicode_GB2312[i+1][0]==iUnicode) return Unicode_GB2312[i+1][1];
}
if (Unicode_GB2312[i][0]<iUnicode) i=i+n;
else i=i-n;
}
if(Unicode_GB2312[i][0]==iUnicode) return Unicode_GB2312[i][1];
if(Unicode_GB2312[i-1][0]==iUnicode) return Unicode_GB2312[i-1][1];
if(Unicode_GB2312[i+1][0]==iUnicode) return Unicode_GB2312[i+1][1];
return 0; //转换不成功
}
/*转换Unicde字符串到GB码,返回汉字数*/
int strUnicode2GB(const char *strSourcer,const char *strDest,int n)
{
char cTmp;
u_int16_t hz,tmphz;
char *pSrc;
char *pDest;
int i;
for (i=0,pSrc=strSourcer,pDest=strDest;n>0;n-=2,pSrc+=2,++i,++pDest){
hz=0;
hz=*pSrc<<8|(*(pSrc+1)&0x00FF);
tmphz=Unicode2GBcode易做图;
if (!tmphz||tmphz>0x7F&&tmphz<0xFF){
*pDest='.';
continue;
}
else if (tmphz>0x00&&tmphz<=0x7F){
cTmp=tmphz;
*pDest=cTmp;
}
else{
cTmp=tmphz;
*pDest=(tmphz>>=8);
++pDest;
*pDest=cTmp;
}
}
*pDest='\0';
return i;
}
--------------------编程问答-------------------- 以上为c++实现。 --------------------编程问答-------------------- byte[] bts = { 0xA0, 0xB3, 0x88, 0x3E, 0xC4, 0xE9 };
?? --------------------编程问答--------------------
using System;--------------------编程问答-------------------- C# 现成的方法就可以用 --------------------编程问答-------------------- C#下有 Encoding 函数使用 --------------------编程问答-------------------- 谢谢谢谢,等我过年的时候 测试以下,再结帖:)
using System.Text;
namespace ConvertExample
{
class ConvertExampleClass
{
static void Main()
{
string unicodeString = "This string contains the unicode character Pi(\u03a0)";
// Create two different encodings.
Encoding ascii = Encoding.ASCII;
Encoding unicode = Encoding.Unicode;
// Convert the string into a byte[].
byte[] unicodeBytes = unicode.GetBytes(unicodeString);
// Perform the conversion from one encoding to the other.
byte[] asciiBytes = Encoding.Convert(unicode, ascii, unicodeBytes);
// Convert the new byte[] into a char[] and then into a string.
// This is a slightly different approach to converting to illustrate
// the use of GetCharCount/GetChars.
char[] asciiChars = new char[ascii.GetCharCount(asciiBytes, 0, asciiBytes.Length)];
ascii.GetChars(asciiBytes, 0, asciiBytes.Length, asciiChars, 0);
string asciiString = new string(asciiChars);
// Display the strings created before and after the conversion.
Console.WriteLine("Original string: {0}", unicodeString);
Console.WriteLine("Ascii converted string: {0}", asciiString);
}
}
}
补充:.NET技术 , C#