我现在正在尝试将整数转换为字符串,但遇到了问题。
我已经完成了大部分代码的编写和工作,但是在传送到下一个地方时它有一个小缺陷。这很难描述,所以我给你举个例子。使用 base 26 和由小写字母组成的字符集:
0 = "a"
1 = "b"
2 = "c"
...
25 = "z"
26 = "ba" (这应该等于 "aa")
在某些情况下似乎会跳过字符集中零位的字符。
令我困惑的是我的代码没有任何问题。我已经在这方面工作了太久了,但我仍然无法弄清楚。
char* charset = (char*)"abcdefghijklmnopqrstuvwxyz";
int charsetLength = strlen(charset);
unsigned long long num = 5678; // Some random number, it doesn't matter
std::string key
do
{
unsigned int remainder = (num % charsetLength);
num /= charsetLength;
key.insert(key.begin(), charset[remainder]);
} while(num);
Run Code Online (Sandbox Code Playgroud)
我有一种感觉,该函数在返回零的模数上绊倒了,但我一直在研究这个,我无法弄清楚它是如何发生的。欢迎任何建议。
编辑:生成的字符串是小端的这一事实与我的应用程序无关。
以下代码以时间格式输出值,即如果是 1:50pm 和 8 秒,则将其输出为 01:50:08
cout << "time remaining: %02d::%02d::%02" << hr << mins << secs;
Run Code Online (Sandbox Code Playgroud)
但我想要做的是(a)将这些整数转换为字符/字符串(b),然后将相同的时间格式添加到其相应的字符/字符串值中。
我已经实现了(a),我只想实现(b)。
例如
char currenthour[10] = { 0 }, currentmins[10] = { 0 }, currentsecs[10] = { 0 };
itoa(hr, currenthour, 10);
itoa(mins, currentmins, 10);
itoa(secs, currentsecs, 10);
Run Code Online (Sandbox Code Playgroud)
现在,如果我输出 'currenthour'、'currentmins' 和 'currentsecs',它将输出与 1:50:8 相同的示例时间,而不是 01:50:08。
想法?
我试图itoa()从K&R练习中重新编写函数,但我没有定义它.我在库中看到了函数的答案,但我无法理解do块中的内容.请向我解释一下.谢谢!
/* itoa: convert n to characters in s */
void itoa(int n, char s[])
{
int i, sign;
if ((sign = n) < 0) /* record sign */
n = -n; /* make n positive */
i = 0;
do { /* generate digits in reverse order */
s[i++] = n % 10 + '0'; /* get next digit */
} while ((n /= 10) > 0); /* delete it */
if (sign < 0)
s[i++] = …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用函数strrev()来反转字符串.我知道strrev返回一个指向反转字符串的指针,因此我只是初始化一个已经分配的字符串,其大小与原始字符串相同,并返回strrev函数.显然,这不是正确的方法,我在该行中得到"不兼容的类型"错误.
这是代码:
int ispalindrome(int n)
{
char s[10], sr[10];
itoa(n, s, 10);
printf("%s", s);
sr = strrev(s);
printf("\nReverse: %s", sr);
if(strcmp(s, sr) == 0)
return 1;
else
return 0;
}
Run Code Online (Sandbox Code Playgroud) 在使用 PIC24FJ128GB204 的 MPLAB X 中学习嵌入式 C。
到目前为止,我主要听说在嵌入式设备上应该尽可能多地使用无符号类型(尤其是?),所以我开始使用 uint8_t 数组来保存字符串。但是,如果我从 stdlib.h 调用 itoa,它需要一个指向有符号字符 (int8_t) 数组的指针:
extern char * itoa(char * buf, int val, int base);
当我在无符号数组上使用 itoa 后尝试编译时,这一点特别清楚:
main.c:317:9: warning: pointer targets in passing argument 1 of 'itoa' differ in signedness
c:\program files (x86)\microchip\xc16\v1.36\bin\bin\../..\include/stdlib.h:131:15: note: expected 'char *' but argument is of type 'unsigned char *'
Run Code Online (Sandbox Code Playgroud)
在其他平台上搜索 itoa 的实现,这似乎是常见的情况。
这是为什么?
(我还注意到大多数实现都需要值/指针/基数,而出于某种原因,来自 Microchip 的 stdlib.h 首先需要指针。我花了一段时间才意识到这一点。)
这很奇怪.itoa();似乎创造了一个无限循环.
for(int i = 0; i < 10; i++)
{
char buffer[1];
itoa(i, buffer, 10);
std::cout << buffer;
}
Run Code Online (Sandbox Code Playgroud)
为什么它会这样做呢?我尝试使用不同的变量i,没有变量的数值(即itoa(1, buffer, 10);),它仍然在无限循环中结束.我试图谷歌没有太大的成功,我在这里找到了一个旧邮件.我使用Windows XP 32位和Code :: Blocks(带GCC)作为编译器.
有谁知道什么是错的?提前致谢.
当我使用itoa()它需要一个char*_DstBuff,这里最好的做法是什么?
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
int num = 100;
// I'm sure here is no memory leak, but it needs to know the length.
char a[10];
// will this causue memory leak? if yes, how to avoid it?
// And why can itoa(num, b, 10); be excuted correctly since b
// has only allocated one char.
char *b = new char;
// What is the difference between char *c and char *b
// both …Run Code Online (Sandbox Code Playgroud) 将整数转换为基数10 char*
std::itoa(ConCounter, ID, 10);
Run Code Online (Sandbox Code Playgroud)
ConCounter是一个整数,ID是char*,10是基数
它说iota不是std的成员,没有std它没有声明.我知道这是一个非标准的功能,但我包含了它的所有库,它仍然没有看到它.
有什么方法可以做到这一点?任何快速的衬垫?我试过以下几点;
std::to_string //it's not declared for me when using mingw, it doesn't exist.
snprintf/sprintf //should work but it gives me the "invalid conversion from 'int' to 'char *'" error
std::stoi //has same problem as iota
Run Code Online (Sandbox Code Playgroud) 我想在单个索引的字符数组中存储一个整数.itoa在这种情况下,该功能不起作用.有人可以帮忙吗?