Mail Archives: djgpp/1998/07/26/16:00:28
On Fri, 24 Jul 1998 17:59:30 -0700, Erik Max Francis <max AT alcyone DOT com>
wrote:
>Nicklas Lindgren wrote:
>
>> I want to store an int inside a string. (It must be a string, not a
>> struct or anything else)
>>
>> I've tried to make a pointer of int type into the string but i can't
>> figure out how to make it work.
>>
>> If anyone can tell me how to, or suggest something better, i'd
>> appreciate it.
>
>What do you mean, "store an int inside a string"? Do you mean have a
>strange which contains the ASCII equivalent of an int? In that case,
>you should sprintf:
>
> int i; /* the int you want "converted" */
> char buffer[32]; /* more than you need */
>
> sprintf(buffer, "%d", i);
>
I'll try that if i can't find a way to make this work:
What i was looking for was a way to store the actual memory
representation of the int in the same memory space as the string. (The
string doesn't have to be printable)
I've tried making a pointer to a char in the string, and then
implicitly converting it to an int. I used something like:
char string[255];
int x = 5;
*(int *)&string[0] = x;
// And for testing that it works:
x = *(int *)&string[0];
cout << x;
The same number comes out in the other end, but it doesn't seem to end
up in the string.
Nicklas Lindgren
- Raw text -