[問題] 請問簡單的bitwise運算

看板C_and_CPP作者 (吃哈吱)時間13年前 (2011/06/20 10:58), 編輯推噓1(109)
留言10則, 2人參與, 最新討論串1/2 (看更多)
在一份文件上看到, 長度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
hi~您好 我將原文附上去了
06/20 13:51, 2F
※ 編輯: Schatze 來自: 140.110.117.141 (06/20 13:52)

06/20 14:34, , 3F
with the highest bit to 1 if you need.. 似乎漏這句.
06/20 14:34, 3F

06/20 14:50, , 4F
嗯嗯 不過將highest bit設成1的話, 第一個byte應該是0x84
06/20 14:50, 4F

06/20 14:51, , 5F
後面499那個例子倒是沒錯
06/20 14:51, 5F

06/20 14:58, , 6F
寫信問了文件所有者..他說是他的TLV是7bits = 1byte
06/20 14:58, 6F

06/20 14:58, , 7F
第二個例子反而是錯的
06/20 14:58, 7F

06/20 15:07, , 8F
意指, 1234 那個例子是錯的吧 ?
06/20 15:07, 8F

06/20 15:12, , 9F
1234那個是對的~499的是錯的
06/20 15:12, 9F
※ 編輯: Schatze 來自: 140.110.117.141 (06/20 15:28)

06/20 15:30, , 10F
不知是否即為我回文之意
06/20 15:30, 10F
文章代碼(AID): #1D_hT6l8 (C_and_CPP)
文章代碼(AID): #1D_hT6l8 (C_and_CPP)