admin 管理员组

文章数量: 1086864

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


十六进制的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_FILE
static 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_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):10strlen(pttgj):8
29 59 fffffff0 6c fffffffd 56 45 ffffff96
=========================
)Y餷齎E
=========================char   ttgj[]="天泰国际";
天泰国际sizeof(ttgj):9strlen(pttgj):8
ffffffcc ffffffec ffffffcc ffffffa9 ffffffb9 fffffffa ffffffbc ffffffca
=========================
天泰国际
=========================
Press any key to continue
*/


本文标签: 十六进制的ascii码 u5929u6cf0u56fdu9645 解码成unicode