將一個int數值轉化成字串 – itoa(…) – strlen(…)

來源:互聯網
上載者:User

    寫這段程式的原因是,一位同學問我 winsock 下的 send 函數如何發送一個int數值。我查看了 send 的原型

int send(  SOCKET s,                const char FAR *buf,    int len,                 int flags              );

    其中第二個參數需要是字串指標。於是,我就考慮如何將一個int數值轉化成字串,由此,寫了如下的這段代碼。

 

/* *********************************************************************** * TianYe-2012-5-26                                      * tets.c * One program which contains  itoa(,,); strlen(..); functions. * In order to implement 'int' to 'char *' *********************************************************************** *///#include <stdio.h>#include <stdlib.h>//int main(){int num = 12345;char str[10];int i = 0;/* * TianYe-2012-5-26 * char *itoa( int value, char *string,int radix); * itoa()把一個整數轉換為字串. * 傳回值: 指向string這個字串的指標. */itoa(num, str, 10);printf("int_1 = %d, str = %s\n", num, str);printf("len = %d\n\n", strlen(str));//num = 789;itoa(num, str, 10);printf("int_2 = %d, str = %s\n", num, str);while(str[i] != '\0'){printf("char = %c\n", str[i]);i++;}/* * TianYe-2012-5-26 * strlen(str) - 計算字串 str 的長度。 */printf("len = %d\n", strlen(str));//return 0;}

 

代碼執行結果如:
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.