C語言 位元組陣列轉為字串

2021-08-16 03:35:22 字數 739 閱讀 3501

#include int main(void)

;

printf("111 test1: %s\n",test1);

printf("222 test1: %.2x %.2x %.2x %.2x %.2x\n", test1[0],test1[1],test1[2],test1[3],test1[4]);

return 0;

}

編譯命令:gcc test.c -o test

編譯後執行結果:

我們發現自己陣列test1無法用%s方式列印,如果想把位元組陣列**為字串可以用%s方式輸出,利用如下**即可。

話不多說貼**:

int main(void)

;char s[13] = ; //注意字串申請的空間,以防越界,無論使用實體陣列還是動態申請malloc方式,空間應該為位元組陣列長度*2+1

int i;

char c1, c2;

for (i = 0; i < 6; i++)

printf("!! %s\n",s);

printf("len %d\n",strlen(s));

return 0;

}//1200567890ab

C 字串到位元組陣列,位元組陣列轉整型

int num 12345 string num1 convert.tostring 12345,16 byte bytes bitconverter.getbytes num 將int32轉換為位元組陣列 num bitconverter.toint32 bytes,0 將位元組陣列內容再轉成in...

字串 位元組陣列互轉

將byte轉換為string 或者將string轉換為byte author administrator public class byteorstringhelper param str 源字串轉換成位元組陣列的字串 return public static byte stringtobyte s...

C 字串與位元組陣列互轉

定義string變數為str,記憶體流變數為ms,位元陣列為bt 1.字串轉位元陣列 1 byte bt system.text.encoding.default.getbytes 字串 2 byte bt convert.frombase64string 字串 2.字串轉流 1 memorystr...