[問題] hex to string跑迴圈有問題
開發平台(Platform): (Ex: Win10, Linux, ...)
linux debian
編譯器(Ex: GCC, clang, VC++...)+目標環境(跟開發平台不同的話需列出)
gcc 6.3
額外使用到的函數庫(Library Used): (Ex: OpenGL, ...)
no
問題(Question):
問題如下詳述
餵入的資料(Input):
int s=5;
char a[7]={0};
char p[]="414243444546";
htos(a,p,s);
printf("%d,%s\n",s,a);
預期的正確結果(Expected Output):
5,ABCDE
錯誤結果(Wrong Output):
0,ABCDE
程式碼(Code):(請善用置底文網頁, 記得排版,禁止使用圖檔)
hex to string的程式碼
void htos(char *dst,const char *src,int len)
{
for (;len>0 && src[0] && src[1] && sscanf(src, "%2X", dst++) ;len--,src += 2);
}
https://gcc.godbolt.org/z/RKlxwH
補充說明(Supplement):
試了幾次,似乎是a的大小跟差值s值小於2就會這樣
像是是s=4 a[6]也會
但s=4 a[7]就會是4,ABCD
看了半天看不出到底為什麼...
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 60.251.130.156
※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1550457789.A.DDC.html
※ 編輯: filiaslayers (60.251.130.156), 02/18/2019 10:53:26
→
02/18 11:10,
6年前
, 1F
02/18 11:10, 1F
It works!看起來是sscanf造成的?但到底是為什麼啊orz?
※ 編輯: filiaslayers (60.251.130.156), 02/18/2019 11:13:17
→
02/18 11:18,
6年前
, 2F
02/18 11:18, 2F
→
02/18 11:18,
6年前
, 3F
02/18 11:18, 3F
看懂了,非常感謝~
※ 編輯: filiaslayers (60.251.130.156), 02/18/2019 11:23:34