[問題] Encoding of binary data
我要照一個文件所述的需求,完成一個功能,如下:
我實在愚笨 看了好幾天仍不能了解它encode,decode規則是如何...orz
這是用在websocket send/recievd binary data時要求的:
Where binary data is returned from this API,
it is encoded as follows:
It is converted to a string by converting each byte in turn using these rules:
a. Bytes 0x20 to 0x24 inclusive are mapped to the corresponding characters
U+0020 to U+0024 inclusive.
b. Bytes 0x26 to 0x7E inclusive are mapped to the corresponding characters
U+0026 to U+007E inclusive.
c. Other bytes are mapped to the three character sequence consisting of a percent character followed by two uppercase hex characters,
e.g. byte 0 maps to “%00” and byte 0xAB maps to “%AB”.
Where binary data is passed into this API,
it is passed as a string which is decoded as follows:
a. Characters U+0020 to U+0024 inclusive are mapped to the corresponding
bytes 0x20 to 0x24 inclusive.
b. Characters U+0026 to U+007E inclusive are mapped to the corresponding
bytes 0x26 to 0x7E inclusive.
c. The three character sequence consisting of a percent character followed
by two uppercase hex characters maps to the corresponding byte,
e.g. “%00” maps to byte 0 and “%AB” maps to byte 0xAB.
d. A percent character that is not followed by two uppercase hex characters
means the string is malformed.
e. If the string contains a character outside the range U+0020 to U+007E
inclusive then the string is malformed
可否各位強者能給我一些意見, 上網找了一些編碼資料仍一頭霧水...
Thanks
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 114.32.220.130
※ 文章網址: https://www.ptt.cc/bbs/Python/M.1481008293.A.827.html
推
12/06 15:16, , 1F
12/06 15:16, 1F
→
12/06 15:18, , 2F
12/06 15:18, 2F
→
12/06 15:19, , 3F
12/06 15:19, 3F
→
12/06 15:19, , 4F
12/06 15:19, 4F
→
12/06 15:19, , 5F
12/06 15:19, 5F
→
12/06 15:20, , 6F
12/06 15:20, 6F
→
12/06 16:02, , 7F
12/06 16:02, 7F