字符数组到单个整数
Array of chars to a single integer
我需要一些关于将字符转换为单个整数值的指导。我不应该使用任何转换它们的库函数。我理解如何使用char - '0' = int将单个字符转换为数组内的int,我的问题是,如果有一种方法可以将整数数组变为单个整数。我正在使用iostream, string和cctype。
谢谢
看一下这段代码:
#include <string.h>
#include <iostream>
int main(void) {
char str[20] = {'1', '2', '3', '4', '5', '6', ' '}; // char string;
int i, len;
long long number = 0; // number which stores the value
len = strlen(str); // calculate the length of string
i = 0;
for(i=0; i<len; i++) {
number = 10*number + (str[i] - '0');
}
std::cout << number << std::endl; // number
return 0;
}
注意:它可以处理从0到9,223,372,036,854,775,807的所有+ive值。我们甚至可以将它扩展到-ive值,只需检查字符串的第一个字符是否为负号(-)。
相关文章:
- 如何反转整数参数包
- enum是C++中的宏变量还是整数变量
- 使用可变参数函数将整数和/或整数数组放入单个 int 数组中
- 编写一个程序,提示用户输入一个整数,然后输出数字的单个数字和数字的总和
- 将C字符串转换为单个整数
- 如何使用位操作在单个整数中编码和解码两个数字
- 如何以相反顺序C 打印整数的单个数字
- 任何可以在单个 CPU 指令中在 0 和 1 之间翻转位/整数/布尔值的可能代码
- 是否可以使整数仅收到一个单个数字而不是两个接收输入
- 如何将单个字符转换为为基本密码哈希器设置整数,C++
- 如何将字符串和整数组合成单个字符串 (C++)
- 如何将整数存储在字符数组的单个索引中
- 读取整数的单个数字
- 运算符在c++中重载,我需要存储单个字母的整数
- 大数(长长以上)和单个整数之间的除法
- 在C++中读取单个输入行中的整数列表
- 字符数组到单个整数
- 将单个整数传递给c++中的多线程函数
- 将单个整数集合解析为浮点数
- 如何访问整数的单个字节