[問題] hex to string跑迴圈有問題

作者: filiaslayers (司馬雲)   2019-02-18 10:43:06
開發平台(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
作者: Lipraxde (Lipraxde)   2019-02-18 11:10:00
%2hhx-Wall,%x的目標是給uint的,s變數被排在a後面的話可能會不小心蓋到

Links booklink

Contact Us: admin [ a t ] ucptt.com