有多少种方法可以将整数转换为字符串 [英] how many ways to convert a integer to a string

查看:70
本文介绍了有多少种方法可以将整数转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尽可能多。

int i = 333;

char * s;

如何将i转换为s;

解决方案


ap *********** @ gmail.com 写道:

尽可能多。
int i = 333;
char * s;
如何将我转换为s;




你的问题不够明确(应该在
$ b中表示) $ b帖子的主体也是)。你想使用标准函数

(参见`sprintf`),还是在寻找不同/算法/方法?


< blockquote> ap*********** @ gmail.com 写道:< blockquote class =post_quotes>
尽可能多。
int i = 333;
char * s;
如何将i转换为s;




#include< stdlib.h>

#include< limits.h>

#include< stdio.h>


s = malloc((size_t)((sizeof(int)* CHAR_BIT - 1)/ 3.3)+ 3);

if(s!= NULL ){

sprintf(s,%d,i);

}


-

pete



ap ***********@gmail.com ??????

尽可能多。
int i = 333;
char * s;
如何将i转换为s;




另一种方法是获取每个数字从整数然后将

difit转换为它的相应char。继续将整数

除以10,直到结果为0.每个除法的剩余部分是整数最左边的数字。


as many as possible.
int i = 333;
char* s;
how to convert i to s;

解决方案


ap***********@gmail.com wrote:

as many as possible.
int i = 333;
char* s;
how to convert i to s;



Your question is not clear enough (and should have been repreated in
the body of the post as well). Do you want to use standard functions
(see `sprintf`), or are you looking for different /algorithmic/ ways?


ap***********@gmail.com wrote:


as many as possible.
int i = 333;
char* s;
how to convert i to s;



#include <stdlib.h>
#include <limits.h>
#include <stdio.h>

s = malloc((size_t)((sizeof(int) * CHAR_BIT - 1) / 3.3) + 3);
if (s != NULL) {
sprintf(s, "%d", i);
}

--
pete



ap***********@gmail.com ???é?????

as many as possible.
int i = 333;
char* s;
how to convert i to s;



Another way is to get every digit from the integer and then convert the
difit to it''s corresponding char. Just continue to divide the integer
by 10 until the result is 0. The remainder of each division is the
leftmost digit of the integer.


这篇关于有多少种方法可以将整数转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆