[問題] 讀取十六進位制字串再轉成integer
開發平台(Platform): (Ex: VC++, GCC, Linux, ...)
C++
額外使用到的函數庫(Library Used): (Ex: OpenGL, ...)
STL
問題(Question):
從一個二進位制的檔案讀入資料將他轉換成64bit-signed int
內容大概像這樣
char s[9]={'\x00','\x00','\x00','\x6a','\x46','\xd2','\xdf','\x44'};
原本我是自己寫一個函式用位元運算硬轉 但因為是signed integer還有負數
處裡很複雜 所以放棄自己寫一個函式來轉
有翻過cpp reference 似乎可以用stringstream來轉換
但試驗後 似乎必須是*s="0x0000006a46d2df44"這類的形式才能直接用stringstream轉
有想過把'\xff'形式的字元轉成"FF"後餵給stringstream
但似乎又有點多此一舉 不知道是不是有什麼更好的方法可以轉換?
餵入的資料(Input):
char s[9]={'\x00','\x00','\x00','\x6a','\x46','\xd2','\xdf','\x44'};
(一個從檔案讀入的hex字串 00 00 00 6A 46 D2 DF 44 )
預期的正確結果(Expected Output):
456454758212
錯誤結果(Wrong Output):
程式碼(Code):(請善用置底文網頁, 記得排版)
補充說明(Supplement):
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 223.26.95.65
※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1434508982.A.86F.html
→
06/17 10:46, , 1F
06/17 10:46, 1F
→
06/17 10:54, , 2F
06/17 10:54, 2F
→
06/17 11:04, , 3F
06/17 11:04, 3F
→
06/17 11:04, , 4F
06/17 11:04, 4F
→
06/17 11:23, , 5F
06/17 11:23, 5F
→
06/17 11:47, , 6F
06/17 11:47, 6F
→
06/17 11:48, , 7F
06/17 11:48, 7F
→
06/17 11:49, , 8F
06/17 11:49, 8F
→
06/17 11:49, , 9F
06/17 11:49, 9F
→
06/17 11:50, , 10F
06/17 11:50, 10F
→
06/17 11:51, , 11F
06/17 11:51, 11F
→
06/17 14:26, , 12F
06/17 14:26, 12F
→
06/17 15:21, , 13F
06/17 15:21, 13F
→
06/17 15:23, , 14F
06/17 15:23, 14F
→
06/17 15:23, , 15F
06/17 15:23, 15F
→
06/17 15:24, , 16F
06/17 15:24, 16F
討論串 (同標題文章)
以下文章回應了本文:
完整討論串 (本文為第 1 之 2 篇):
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章