C++ 将字符数组转换为单整数?
有人知道如何将字符数组转换为单个int吗C++ 将字符数组转换为单整数?,c++,char,int,C++,Char,Int,有人知道如何将字符数组转换为单个int吗 char hello[5]; hello = "12345"; int myNumber = convert_char_to_int(hello); Printf("My number is: %d", myNumber); 长话短说你必须使用 教育署: 如果您有兴趣这样做: Ascii字符串到整数的转换由atoi()函数完成。使用 /*sscanf示例*/ #包括 int main() { char句子[]=“鲁道夫12岁”; char-str[2
char hello[5];
hello = "12345";
int myNumber = convert_char_to_int(hello);
Printf("My number is: %d", myNumber);
长话短说你必须使用 教育署: 如果您有兴趣这样做:
Ascii字符串到整数的转换由
atoi()
函数完成。使用
/*sscanf示例*/
#包括
int main()
{
char句子[]=“鲁道夫12岁”;
char-str[20];
int i;
sscanf(句子,“%s%*s%d”、str和i);
printf(“%s->%d\n”,str,i);
返回0;
}
将字符串转换为int有多种方法
解决方案1:使用传统C功能
解决方案2:使用词法转换
(最合适和最简单)
如果您使用的是
C++11
,可能应该使用stoi
,因为它可以区分错误和解析“0”
需要注意的是,“1234abc”在被传递到
stoi()
之前是从char[]
到std:string
我将把这个留给那些对没有依赖关系的实现感兴趣的人
inline int
stringLength (char *String)
{
int Count = 0;
while (*String ++) ++ Count;
return Count;
}
inline int
stringToInt (char *String)
{
int Integer = 0;
int Length = stringLength(String);
for (int Caret = Length - 1, Digit = 1; Caret >= 0; -- Caret, Digit *= 10)
{
if (String[Caret] == '-') return Integer * -1;
Integer += (String[Caret] - '0') * Digit;
}
return Integer;
}
可用于负值,但不能处理介于两者之间的非数字字符(但应易于添加)。仅限整数。我使用:
int convertToInt(char a[1000]){
int i = 0;
int num = 0;
while (a[i] != 0)
{
num = (a[i] - '0') + (num * 10);
i++;
}
return num;;
}
例如,“mcc”是一个字符数组,“mcc_int”是您想要得到的整数
char mcc[] = "1234";
int mcc_int;
sscanf(mcc, "%d", &mcc_int);
使用cstring和cmath:
int charsToInt (char* chars) {
int res{ 0 };
int len = strlen(chars);
bool sig = *chars == '-';
if (sig) {
chars++;
len--;
}
for (int i{ 0 }; i < len; i++) {
int dig = *(chars + i) - '0';
res += dig * (pow(10, len - i - 1));
}
res *= sig ? -1 : 1;
return res;
}
int charsToInt(char*chars){
int res{0};
int len=strlen(字符);
bool sig=*字符=='-';
如果(信号){
chars++;
蓝--;
}
对于(int i{0};i
该代码是否按原样编译?不应该,不应该。convert_char_to_int(hello)不是实际的函数。我在问我应该用什么函数/方法来代替我的理论:“convert_char_to_int(hello)”?hello
是一个不可修改的左值,所以hello=“12345”代码>甚至不会编译。好吧,那么,这一个:我如何转换它?你是否完全被C风格的构造所束缚,比如char数组和printf?如果不是,请看字符“Cux> STD::String ,C++输出打印输出(<代码> STD::Cout ),和 Boo::LoCalic OxStudio转换(如已指出的)@ Als:使用<代码> Boo::ListalyOxCase< /Cord>代码>atoi
不安全@纳瓦兹:我想这就是全部的总结:)+1。顺便说一下,您应该将boost::lexical_cast
放在try-catch块中。当强制转换无效时,它会抛出boost::bad_lexical_cast
。atoi
strtol
,在这种情况下更合适-不需要仅为这个简单的操作使用boost…@juanchopanza是一个错误代码,而不是一个异常。如果您正在处理用户输入(以及为什么要转换),错误代码通常比异常更合适。不,您不需要,也不应该在任何新代码中使用strtol
@尼姆我知道这一点,但对于像OP这样的新手来说,我想让他/她使用它是可以的,只是为了理解这个概念。我想这也是为什么其他人也提出了同样的API。@ HARPER在标准C++中没有<代码> ITOA。而且它的传统接口被破坏了,可能导致缓冲区溢出。@雷诺在任何情况下都不应该鼓励初学者养成坏习惯。这是你绝对应该避免的。你如何检查错误?无论如何,不是那样的。例如,切勿使用“%s”
,因为这将导致缓冲区溢出。总之,stdtol
更简单、更安全。stdtol不是吗?为什么strtol
优于atoi
?这通常已经足够好了,但它无法判断字符串是否在零后继续。例如,对于像101
std::string hello("123");
std::stringstream str(hello);
int x;
str >> x;
if (!str)
{
// The conversion failed.
}
try {
int number = std::stoi("1234abc");
} catch (std::exception const &e) {
// This could not be parsed into a number so an exception is thrown.
// atoi() would return 0, which is less helpful if it could be a valid value.
}
inline int
stringLength (char *String)
{
int Count = 0;
while (*String ++) ++ Count;
return Count;
}
inline int
stringToInt (char *String)
{
int Integer = 0;
int Length = stringLength(String);
for (int Caret = Length - 1, Digit = 1; Caret >= 0; -- Caret, Digit *= 10)
{
if (String[Caret] == '-') return Integer * -1;
Integer += (String[Caret] - '0') * Digit;
}
return Integer;
}
int convertToInt(char a[1000]){
int i = 0;
int num = 0;
while (a[i] != 0)
{
num = (a[i] - '0') + (num * 10);
i++;
}
return num;;
}
char mcc[] = "1234";
int mcc_int;
sscanf(mcc, "%d", &mcc_int);
int charsToInt (char* chars) {
int res{ 0 };
int len = strlen(chars);
bool sig = *chars == '-';
if (sig) {
chars++;
len--;
}
for (int i{ 0 }; i < len; i++) {
int dig = *(chars + i) - '0';
res += dig * (pow(10, len - i - 1));
}
res *= sig ? -1 : 1;
return res;
}