-1

例:

// variable fixed
Char specialstring[PATH_MAX];
int integernumber;
int xx = 1 ; 
int yy = 1 ; 
strncopy( specialstring, "1369", ... bla); 


// here we go !! help there below please
integernumber=atoi(specialstring);
mvprintw( yy , xx , "%d" , integernumber );

特殊文字列を整数に変換する方法を教えてください。

ありがとうございました

4

2 に答える 2

2

あなたのコードには、次の 2 つの間違いがあります。

1) strncopyあなたが望む機能ではありませんstrncpy。そのマニュアルページ: char *strncpy(char *restrict s1, const char *restrict s2, size_t n); これs1は宛先文字列でありs2、ソース文字列nです。ソースからコピーする文字の数です。

とても正しい:

strncopy( specialstring, "1369", ... bla); 
     ^                            ^ should be `n` num of chars you wants to 
    strncpy                         copy in `specialstring`

の中へ

 strncpy( specialstring, "1369", 4); 

2)の宣言ではspecialstringCharは間違っています。小さく書く必要があります。c

Char specialstring[PATH_MAX];
^ small letter

char  specialstring[PATH_MAX];

3) atoi() 文字列を int に変換する正しい関数です。変換せずに変換したい場合は、次のような関数atoiを使用できます。sscanf()

sscanf(specialstring,"%d", &integernumber);

これを表示:作業コード

于 2013-03-30T11:10:19.663 に答える
-1

これを使用して、atoiを使用せずに文字列をintに変換できます

int Convert(char * str)
{
    int result =0;
int len=strlen(str);
for(int i=0,j=len-1;i<len;i++,j--)
    {
    result += ((int)str[i] - 48)*pow(10,j);
    }

   return result;
}
于 2013-03-30T11:08:59.173 に答える