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

来源:互联网 发布:天谕头发颜色数据 编辑:程序博客网 时间:2024/06/05 21:11


十六进制的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 failureif (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0)){// TODO: change error code to suit your needscerr << _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_tHanzi[]=L"中国\n";wprintf(Hanzi);cout <<"========================"<<endl;wchar_t* sz = L"KC is a genuis\n";wprintf(sz);cout <<"========================"<<endl;wchar_tttgj[]=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 charttgj[]=\"天泰国际\"; "<<endl;charttgj[]="天泰国际";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*/


原创粉丝点击