-
April 25th, 2009, 06:52 PM
#1
placing int into char*
ok so i want to be able to take an integer break it up and place it into an char*
example: int x = 10
take 10 make it into '1' and '0' and then place those into char*
now i have a method that does this and it looks like this..........buf is a char*
Code:
std::ostringstream sout;
sout << x;
std::string s(sout.str());
length = s.length(); // Sets length equal to the length of the string
buf = new char[length];
const char *sz = s.c_str(); // Sets const char * equal to the string
buf = (char*) sz; // Casts sz to char* from const char * and sets buf equal
The problem is that iw ould rather now use the string class because unfortunatly im doin an assignment that is basically the string class. So any suggestions through them at me. Thank you
-
April 25th, 2009, 07:31 PM
#2
Re: placing int into char*
Ok so let me clarify..............
sudo code:
Code:
int x = 10;
char* buf = new char[ number of digits ];
take x place into buf like '1' and '0'
so that...
buf[0] = 1
buf[1] = 0
-
April 25th, 2009, 07:36 PM
#3
Re: placing int into char*
I just typed this up on the fly, so there's probably some errors:
Code:
int intPow(const int n, const int p) // Gets n to the power of p
{
int ret = n;
for(int i = 1; i < p; i++)
ret *= n;
return ret;
}
int intLog10(const int n) // Gets log base 10
{
int ret = 0, num = n;
while(num > 10)
{
num /= 10;
ret++;
}
return ret;
}
char* intToStr (const int n) // Call this one with the number and it (maybe) will return it in string form
{
int strLen = intLog10 + 2, num = n, temp, temp2;
char* ret = new char[strLen];
for(int i = 0; i < strLen-1; i++)
{
char[i] = (temp2 = (num % (temp = intPow(10,strLen-i-2)))) + '0';
num -= temp * temp2;
}
ret[strLen-1] = '\0';
return ret;
}
There's also itoa, of course:
http://www.cplusplus.com/reference/c.../cstdlib/itoa/
Last edited by Etherous; April 25th, 2009 at 07:52 PM.
Intel Core Duo Macbook w/ Mac OS 10.5.6
gcc 4.2.1 (i386-apple-darwin9.1.0) and Xcode 3.1.1
-
April 25th, 2009, 08:02 PM
#4
Re: placing int into char*
very nice i will try it and get back her when i figure out what happens
-
April 25th, 2009, 08:08 PM
#5
Re: placing int into char*
Something worth considering if you use itoa or a similar function is the the number with the most digits that could *possibly* be held in an int is -2147483648. So if you pass a fixed-sized char array with at least 12 bytes to itoa, you know you'll have enough space.
-
April 26th, 2009, 12:31 AM
#6
Re: placing int into char*
just out of curiosity what is the #include for iota...... and isnt it a C# function
correct me if im wrong
-
April 26th, 2009, 02:31 AM
#7
Re: placing int into char*
Originally Posted by Lindley
Something worth considering if you use itoa or a similar function is the the number with the most digits that could *possibly* be held in an int is -2147483648.
Though strictly speaking an int could have an even larger range on a given implementation, but such a consideration is not so important for a school assignment.
Originally Posted by ductiletoaster
just out of curiosity what is the #include for iota...... and isnt it a C# function
correct me if im wrong
It is not part of the C++ standard library, but if it is available you can probably #include <cstdlib> to use it.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|