[問題] 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