[问题] Encoding of binary data

楼主: aMaa (aMa)   2016-12-06 15:11:31
我要照一个文件所述的需求,完成一个功能,如下:
我实在愚笨 看了好几天仍不能了解它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
作者: tiefblau (tiefblau)   2016-12-06 15:16:00
是作业吗? 一般编码没关系 自定义规则叫你处理而已a,b就是直接弄成0x的byte就好 中间跳一个U+0025就是%%是特殊规则 收到%接着两个大写hex就是map到0x多少多少
楼主: aMaa (aMa)   2016-12-06 15:19:00
不是作业,是工作上project中的一个资料处理需求,它自定义的
作者: tiefblau (tiefblau)   2016-12-06 15:19:00
如果收到这之外的 就是api坏了
楼主: aMaa (aMa)   2016-12-06 15:20:00
规则是什么, 我看不懂/_\ 如果string是hello world, 是转成?
作者: AndCycle (AndCycle)   2016-12-06 16:02:00
你看一下ascii table对照一下就知道他想干嘛了

Links booklink

Contact Us: admin [ a t ] ucptt.com