[問題] 請問簡單的bitwise運算
在一份文件上看到,
長度123的句子, hex是7B, 所以長度只要用1byte, 0x7B來表示
長度1234的句子, hex是4D2, binary是0100 1101 0010
所以長度用2bytes, 0x89及0x52來表示.
請問0x89及0x52是怎麼算出來的?
謝謝
文件上這部份內容大概就只寫了這些, 之後接幾個範例
TLV encoded
We are going to use the Tag-Length-Value system to encode the
tags that is used in EMV applications.
Encode the length as 1 byte with the highest bit to 1 if you need
more of 1 byte to encode it, or 0 if not. For instance, for the
length 123 (in hex it's 7B) the length is just 0x7B, but for the
length 1234 (in hex it's 4D2, in binary it's 0100 1101 0010)
it will be encoded as 0x89 in the first byte and 0x52 in the
second byte.
The text in hex :
(hex code) ....
with length 499 (hex 1F3, bin 0001 1111 0011, encoded as 0x81 in the
first byte, 0xF3 in the second byte). The resulting of TLV is
text_id 81 F3 (hex code) ...
實在想不出來1234拆成2bytes來表示是怎麼變成0x89跟0x52,
499拆成2bytes表示變成 0x81 0xF3
解答:
作者回信他是以每7bits來代表一個hex code
1234的binary是0100 1101 0010
因為超過8bits, 故要用兩個byte來表示.
第一個byte最高bit要改成1, 以代表有第二個byte的存在,
因此,
第二個byte為101 0010 = 0101 0010 = (hex)0x52
第一個byte為0100 1, 1 byte裡最高bit要填上1, 變成100 0100 1 = (hex)0x89
個人是第一次看到這種方式
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.110.117.141
推
06/20 13:16, , 1F
06/20 13:16, 1F
※ 編輯: Schatze 來自: 140.110.117.141 (06/20 13:50)
→
06/20 13:51, , 2F
06/20 13:51, 2F
※ 編輯: Schatze 來自: 140.110.117.141 (06/20 13:52)
→
06/20 14:34, , 3F
06/20 14:34, 3F
→
06/20 14:50, , 4F
06/20 14:50, 4F
→
06/20 14:51, , 5F
06/20 14:51, 5F
→
06/20 14:58, , 6F
06/20 14:58, 6F
→
06/20 14:58, , 7F
06/20 14:58, 7F
→
06/20 15:07, , 8F
06/20 15:07, 8F
→
06/20 15:12, , 9F
06/20 15:12, 9F
※ 編輯: Schatze 來自: 140.110.117.141 (06/20 15:28)
→
06/20 15:30, , 10F
06/20 15:30, 10F
討論串 (同標題文章)
以下文章回應了本文:
完整討論串 (本文為第 1 之 2 篇):