#### Archived

This topic is now archived and is closed to further replies.

# Long to string

This topic is 5859 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

HI How do you convert a unsigned long to a string. Is there a function that i have overlooked or what. Thanks

##### Share on other sites
Hello.

I actually anwered an almost identical question a few days ago. The ANSI C way to do this is to use sprintf from stdio.h. Just do this:
#include <stdio.h> // To use sprintf...unsigned long int x;x = 5;char *temp[2];sprintf(temp, "%d\0", x);printf("%s", temp);

You could also use the plain ASCII set and do some calculations but it seems to be just harder work for the exact same result.

Minion

##### Share on other sites
  ...int num = 37;char str = char(num)

If you can''t do that in string class (string.h) (if they haven''t defined a string operator long()(), then you''re out of luck with that, unless you define your own (if you know how))

##### Share on other sites
You could do it with sprintf, but it''s probably better to use itoa.

char Str[10];
itoa(Number, Str, 10);

##### Share on other sites
hmmm... you''re right. If you need portability, write your own itoa clone

##### Share on other sites
...using sprintf internally.

##### Share on other sites
That couldn''t possibly work, you know... :D

##### Share on other sites
you could always do something like this using remainders...

char * getNumberString(unsigned long * pDigitSpan, long number) {
/*
* get the number of digits in the number you''re converting
*/
unsigned long digitSpan = 0;

long tempNum = number;

while (tempNum > 0) {
tempNum /= 10;
digitSpan++;
}

/*
* actually build up the string of number
*/
char * numberString = new char[digitSpan];

tempNum = number;

for (unsigned long i = digitSpan; i > 0; i--) {
numberString[i-1] = tempNum % 10;

tempNum /= 10;
}

//i know you could probably incriment the *pDigitSpan
//instead of doing this assignment down here
//but i ran into some wierd problems doing something similar
//in another program so...
*pDigitSpan = digitSpan;

return numberString;
}

this also gives you the digit length of the number.

i think you need to convert tempNum % 10 from a single digit number into a char. i don''t remember exactly how to do that but i think it might be:

numberString[i-1] = ((char) tempNum % 10) - (char) 0;

damn, been way too long since i converted numerals into chars...

anyway, you can also use this function to convert to a binary number if you change the 10 to a 2 in all the % and /= statements. or even better would be to generecize the function by adding a passed int of the base number system you want to convert to. then use that instead of /= 10; (i.e. /= base

whatever, i know this function works for converting to binary. cause i wrote it for that purpose cept used a return type of bool * to conserve memory space.

-me

##### Share on other sites
...that does not work if number == 0, or if you want to calculate the number in hex. Try this:

  const char char_values[] = "0123456789ABCDEF";char *IntToChar(int num, int base) { int length = 0; int temp_num = num; do { // while(temp_num > 0) ++length; temp_num /= base; } while(temp_num > 0); char *str = (char*)malloc(length + 1); str += length + 1; *str = ''\0''; do { // while(num > 0) --str; *str = char_values[num % base]; num /= base; } while(num > 0); return str;}