[問題] Encoding of binary data

看板Python作者 (aMa)時間7年前 (2016/12/06 15:11), 編輯推噓1(106)
留言7則, 3人參與, 最新討論串1/1
我要照一個文件所述的需求,完成一個功能,如下: 我實在愚笨 看了好幾天仍不能了解它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
a,b就是直接弄成0x的byte就好 中間跳一個U+0025就是%
12/06 15:18, 2F

12/06 15:19, , 3F
%是特殊規則 收到%接著兩個大寫hex就是map到0x多少多少
12/06 15:19, 3F

12/06 15:19, , 4F
不是作業,是工作上project中的一個資料處理需求,它自定義的
12/06 15:19, 4F

12/06 15:19, , 5F
如果收到這之外的 就是api壞了
12/06 15:19, 5F

12/06 15:20, , 6F
規則是什麼, 我看不懂/_\ 如果string是hello world, 是轉成?
12/06 15:20, 6F

12/06 16:02, , 7F
你看一下ascii table對照一下就知道他想幹嘛了
12/06 16:02, 7F
文章代碼(AID): #1OHcIbWd (Python)