字符串轉換ASCII碼、Unicode碼

將字符串轉成16進制的ASCii碼的值html

python:
python

使用python 內置函數repr能夠將非ascii碼轉換成\x**的樣式,以下:
linux

>>> a='咱們都是中國人'
>>> print repr(a)
'\xe6\x88\x91\xe4\xbb\xac\xe9\x83\xbd\xe6\x98\xaf\xe4\xb8\xad\xe5\x9b\xbd\xe4\xba\xba'
>>>

好像換成非中文就不行了。shell

對於英文能夠用這個:bash

>>> a=''.join('\\x%02x' % ord(x) for x in 'Hello World!')
>>> print a
\x48\x65\x6c\x6c\x6f\x20\x57\x6f\x72\x6c\x64\x21


bash:
函數

ada@barnabas:~> echo "ABCDE" | uni2ascii -Bsepq
\x0041\x0042\x0043\x0044\x0045
ada@barnabas:~> echo "ABCDE" | uni2ascii -Bsepq | ascii2uni -Bq
ABCDE


C語言:post

void print_hex(const char *s)
{
    while(*s)
        printf("\\x%02x", (unsigned int) *s++);
    printf("\n");
}

Python中文字符串與Unicode編碼相互轉換ui

[admin@admin ~]$ python
>>> u'中文'
u'\u4e2d\u6587'
>>> print u'\u4e2d\u6587'.encode('utf-8')
中文
>>>


參考:編碼

http://www.linuxquestions.org/questions/programming-9/%5Bbash%5D-ascii-to-hex-and-hex-to-ascii-488357/ spa

http://blog.chinaunix.net/uid-12453618-id-2935335.html 

http://blog.yuaz.net/archives/313

PYTHON-進階-編碼處理小結

相關文章
相關標籤/搜索