十六进制的ascii码 \u5929\u6cf0\u56fd\u9645 解码成unicode

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

               


十六进制的ascii码 "\u5929\u6cf0\u56fd\u9645" 解码成unicode

转码方法:

C#:           

 string a = "\u5929\u6cf0\u56fd\u9645";
            string b = Encoding.UTF8.GetString(Encoding.UTF8.GetBytes(a.ToCharArray()));


JAVA:

String str2 = new String(str1.getBytes("utf8"));


更正:上述的"\u5929\u6cf0\u56fd\u9645"本身就是unicode的编码,所以并不需要转码,\u开头就代表了是unicode编码,与字面常量'天泰国际'完全等价。


把"59296cf056fd9645"这样的十六进制字符串转换成unicode:

        public string convertUtf8(string code)
        {
            byte[] chars = new byte[code.Length / 2];
            Char[] cs = code.ToCharArray();
            for (int i = 0; i < code.Length / 2; i++)
            {
                if(i%2==0)
                    chars[i+1] = Convert.ToByte(code.Substring(2*i,2),16);
                else
                    chars[i - 1] = Convert.ToByte(code.Substring(2 * i, 2), 16);
                
                
            }
            for (int i = 0; i < chars.Length; i++) Console.WriteLine(chars[i]);

            return Encoding.Unicode.GetString(chars);
        }

注意Unicode的大小端,这里的unicode是小端方式的,所以例如5929在byte数组中29应该在59之前。

// Hanzi.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include "Hanzi.h"#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE[] = __FILE__;#endif/// The one and only application objectCWinApp theApp;using namespace std;int _tmain(int argc, TCHAR* argv[], TCHAR* envp[]){ _tsetlocale(LC_ALL, L"CHS"); int nRetCode = 0// initialize MFC and print and error on failure if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0)) {  // TODO: change error code to suit your needs  cerr << _T("Fatal Error: MFC initialization failed") << endl;  nRetCode = 1; } else {  // TODO: code your application's behavior here.  CString strHello;  strHello.LoadString(IDS_HELLO);  wcout << (LPCTSTR)strHello << endl;  cout <<"========================"<<endl;  //TCHAR  //typedef unsigned short wchar_t;  wchar_t Hanzi[]=L"中国\n";  wprintf(Hanzi);  cout <<"========================"<<endl;  wchar_t* sz = L"KC is a genuis\n";  wprintf(sz);  cout <<"========================"<<endl;    wchar_t ttgj[]=L"天泰国际";  wprintf(ttgj);  cout <<"\n sizeof(ttgj):"<<sizeof(ttgj)<<endl;  char *pttgj=reinterpret_cast<char *>(ttgj);  cout <<"\n strlen(pttgj):"<<strlen(pttgj)<<endl;  for(int i=0;i<strlen(pttgj);i++)   printf("%x ",pttgj[i]);  cout <<"\n========================="<<endl;  for( i=0;i<strlen(pttgj);i++)   putchar(pttgj[i]);  cout <<"\n========================="<<endl;  {    cout <<"\n char ttgj[]=\"天泰国际\"; "<<endl;    char ttgj[]="天泰国际";    printf(ttgj);    cout <<"\n sizeof(ttgj):"<<sizeof(ttgj)<<endl;    char *pttgj=ttgj;    cout <<"\n strlen(pttgj):"<<strlen(pttgj)<<endl;    for(int i=0;i<strlen(pttgj);i++)     printf("%x ",pttgj[i]);    cout <<"\n========================="<<endl;    for( i=0;i<strlen(pttgj);i++)     putchar(pttgj[i]);    cout <<"\n========================="<<endl;  } } return nRetCode;}/*Hello from MFC!========================中国========================KC is a genuis========================天泰国际 sizeof(ttgj):10 strlen(pttgj):829 59 fffffff0 6c fffffffd 56 45 ffffff96=========================)Y餷齎E========================= char   ttgj[]="天泰国际";天泰国际 sizeof(ttgj):9 strlen(pttgj):8ffffffcc ffffffec ffffffcc ffffffa9 ffffffb9 fffffffa ffffffbc ffffffca=========================天泰国际=========================Press any key to continue*/


           

给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow
这里写图片描述
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值