Jump to content

  • Log In with Google      Sign In   
  • Create Account


Long to string


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
9 replies to this topic

#1 Matthew123   Members   -  Reputation: 157

Like
Likes
Like

Posted 05 January 2002 - 11:17 AM

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

Sponsor:

#2 Minion   Members   -  Reputation: 118

Like
Likes
Like

Posted 05 January 2002 - 11:47 AM

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

#3 matrix2113   Members   -  Reputation: 122

Like
Likes
Like

Posted 05 January 2002 - 11:48 AM

  
...
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))

#4 Beer Hunter   Members   -  Reputation: 712

Like
Likes
Like

Posted 05 January 2002 - 12:09 PM

You could do it with sprintf, but it''s probably better to use itoa.

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

#5 Null and Void   Moderators   -  Reputation: 1087

Like
Likes
Like

Posted 05 January 2002 - 12:56 PM

The itoa function is not ANSI. It''s easy to write your own though.

[Resist Windows XP''s Invasive Production Activation Technology!]

#6 Beer Hunter   Members   -  Reputation: 712

Like
Likes
Like

Posted 06 January 2002 - 11:02 AM

hmmm... you''re right. If you need portability, write your own itoa clone


#7 merlin9x9   Members   -  Reputation: 174

Like
Likes
Like

Posted 06 January 2002 - 11:09 AM

...using sprintf internally.

#8 Beer Hunter   Members   -  Reputation: 712

Like
Likes
Like

Posted 08 January 2002 - 10:37 AM

That couldn''t possibly work, you know... :D

#9 Palidine   Members   -  Reputation: 1279

Like
Likes
Like

Posted 08 January 2002 - 11:03 AM

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

#10 Beer Hunter   Members   -  Reputation: 712

Like
Likes
Like

Posted 09 January 2002 - 11:50 AM

...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;
}





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS