itoa

How to generate a constexpr version string from three integers (or perhaps a git/SVN commit/rev. string)?

橙三吉。 提交于 2019-12-29 04:01:11
问题 Say I have constexpr const std::uint8_t major = 1; constexpr const std::uint8_t minor = 10; constexpr const std::uint8_t bugfix = 0; and I want constexpr const char* version_string(){ ... } to return the equivalent of "1.10.0" in this example, how would I do it? I assume I'll need both of these, in constexpr : integer to string conversion string concatenation The problem is purely academic, and I see little to no use to actually have it constexpr other than "it's possible". I just can't see

itoa function problem

穿精又带淫゛_ 提交于 2019-12-19 14:04:16
问题 I'm working on Eclipse inside Ubuntu environment on my C++ project. I use the itoa function (which works perfectly on Visual Studio) and the compiler complains that itoa is undeclared. I included <stdio.h> , <stdlib.h> , <iostream> which doesn't help. 回答1: www.cplusplus.com says: This function is not defined in ANSI-C and is not part of C++, but is supported by some compilers. Therefore, I'd strongly suggest that you don't use it. However, you can achieve this quite straightforwardly using

itoa function problem

ぃ、小莉子 提交于 2019-12-19 14:03:55
问题 I'm working on Eclipse inside Ubuntu environment on my C++ project. I use the itoa function (which works perfectly on Visual Studio) and the compiler complains that itoa is undeclared. I included <stdio.h> , <stdlib.h> , <iostream> which doesn't help. 回答1: www.cplusplus.com says: This function is not defined in ANSI-C and is not part of C++, but is supported by some compilers. Therefore, I'd strongly suggest that you don't use it. However, you can achieve this quite straightforwardly using

convert int to char* in standard C (without itoa)

拜拜、爱过 提交于 2019-12-13 15:05:55
问题 I have declared and initialized two variables as shown below: int a=5; char* str; str = (char*)calloc(255, sizeof(char)); I want to convert the int to char* in standard C. I cannot use any conversion function from C++ such itoa. I am using Ubuntu 11.10 回答1: First of all, itoa is not a C++ thing. You can simply use sprintf : sprintf(str, "%d", a) In a real application you'll want to use snprintf though to remove the risk of a buffer overflow: str = malloc(16); snprintf(str, 16, "%d", a); And

itoa recursively

不问归期 提交于 2019-12-13 01:42:04
问题 I have been trying to write a recursive version of function itoa , the code is shown below. void itoa(int n, char s[]) { static int i = 0; if(n / 10 != 0) itoa(n/10, s); else if(n < 0) i = 1; /* s[0] is allready taken by - sign */ else i = 0; /* reset i to 0 */ if(n < 0) { s[0] = '-'; } s[i++] = abs(n % 10) + '0'; s[i] = '\0'; } But the code is not ideal. It uses a static variable and probably is not executing as fast as it should be. I am trying to achieve a O(n) algorithm. Could anyone show

Base Conversion Problem

我的梦境 提交于 2019-12-12 10:08:26
问题 I'm trying to convert an integer to a string right now, and I'm having a problem. I've gotten the code written and working for the most part, but it has a small flaw when carrying to the next place. It's hard to describe, so I'll give you an example. Using base 26 with a character set consisting of the lowercase alphabet: 0 = "a" 1 = "b" 2 = "c" ... 25 = "z" 26 = "ba" (This should equal "aa") It seems to skip the character at the zero place in the character set in certain situations. The

atoi is a standard function. But itoa is not. Why?

删除回忆录丶 提交于 2019-12-10 01:50:11
问题 Why this distinction? I've landed up with terrible problems, assuming itoa to be in stdlib.h and finally ending up with linking a custom version of itoa with a different prototype and thus producing some crazy errors. So, why isn't itoa not a standard function? What's wrong with it? And why is the standard partial towards its twin brother atoi ? 回答1: No itoa has ever been standardised so to add it to the standard you would need a compelling reason and a good interface to add it. Most itoa

string类的实现:在VS2010和DEV4.9.9.2之间的差异

折月煮酒 提交于 2019-12-09 11:40:46
以下是string类的代码: string.h代码如下: #include<iostream> using namespace std; #ifndef STRING_H #define STRING_H //------------------------------------------------------------------- class String { private: char* m_pData; public: String(const char* pstr=NULL); String(const String& obj); String& operator=(const String& obj); String& operator=(const char* pstr); String& operator=(int nNum); char& operator[](int nIdx); const char& operator[](int nIdx)const; String& operator[](const String& obj); String& operator+=(const String& obj); String& operator+=(const char* pstr); int operator!=(const String&obj);

ANSI C, integer to string without variadic functions

霸气de小男生 提交于 2019-12-07 10:58:41
问题 I'm currently working with a PLC that supports ANSI C, but uses its own flavour of the GNU compiler, which doesn't compile any variadic functions and things like itoa. So using sprintf & co. isn't an option for converting integers to strings. Can anyone guide me to a site where a robust, sprintf- free implementation of itoa is listed or post a suitable algorithm here? Thanks in advance. 回答1: This is from K&R: void itoa(int n, char s[]) { int i, sign; if ((sign = n) < 0) /* record sign */ n =

converting integer to string C++

浪子不回头ぞ 提交于 2019-12-06 16:38:33
I am trying to convert an integer to char array and I came across this piece of code int i = 5; std::string s; std::stringstream out; out << i; s = out.str(); But when I try to print the value of s it still prints 5. I don't know if its supposed to do that or am I doing something wrong? Besides I would prefer if I could convert the same int to char array. But I would appreciate any help in the matter. Thanks! Code taken from: Alternative to itoa() for converting integer to string C++? Besides I would prefer if I could convert the same int to char array. char *charPtr = new char[ s.length() + 1