作者aMaa (aMa)
看板Python
标题[问题] Encoding of binary data
时间Tue Dec 6 15:11:31 2016
我要照一个文件所述的需求,完成一个功能,如下:
我实在愚笨 看了好几天仍不能了解它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://webptt.com/cn.aspx?n=bbs/Python/M.1481008293.A.827.html
1F:推 tiefblau: 是作业吗? 一般编码没关系 自定义规则叫你处理而已 12/06 15:16
2F:→ tiefblau: a,b就是直接弄成0x的byte就好 中间跳一个U+0025就是% 12/06 15:18
3F:→ tiefblau: %是特殊规则 收到%接着两个大写hex就是map到0x多少多少 12/06 15:19
4F:→ aMaa: 不是作业,是工作上project中的一个资料处理需求,它自定义的 12/06 15:19
5F:→ tiefblau: 如果收到这之外的 就是api坏了 12/06 15:19
6F:→ aMaa: 规则是什麽, 我看不懂/_\ 如果string是hello world, 是转成? 12/06 15:20
7F:→ AndCycle: 你看一下ascii table对照一下就知道他想干嘛了 12/06 16:02