Long to string

Started by
8 comments, last by Matthew123 22 years, 3 months ago
HI How do you convert a unsigned long to a string. Is there a function that i have overlooked or what. Thanks
Advertisement
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
  ...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))
"I've learned something today: It doesn't matter if you're white, or if you're black...the only color that really matters is green"-Peter Griffin
You could do it with sprintf, but it''s probably better to use itoa.

char Str[10];
itoa(Number, Str, 10);
The itoa function is not ANSI. It''s easy to write your own though.

[Resist Windows XP''s Invasive Production Activation Technology!]
hmmm... you''re right. If you need portability, write your own itoa clone
...using sprintf internally.
That couldn''t possibly work, you know... :D
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--) {
//see note below about this line
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
...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;}  

This topic is closed to new replies.

Advertisement