View Single Post
Old Dec 4th, 2004, 4:12 PM   #4
Eggbert
Professional Programmer
 
Eggbert's Avatar
 
Join Date: Nov 2004
Posts: 250
Rep Power: 5 Eggbert is on a distinguished road
>but i need to return the hex result as a string not an array, how do i change it to a string.
It is a string. C-style strings are simply arrays of char terminated by '\0'. If you want a C++ string object then take a look at the code I gave you in your other thread.

>I am not printing out the result simply holding it in a varible.
You have two options. If you don't intend to call Convert_to_Hex until after you're done working with the return value then you can simply use a pointer to char:
char *Hex_Num=Convert_To_Hex(Dec - Num);
This works because the string being used in Convert_To_Hex is static and will persist between calls. However, if you intend to do something like this:
char *Hex_Num1=Convert_To_Hex(Dec1 - Num1);
char *Hex_Num2=Convert_To_Hex(Dec2 - Num2);
Then it won't work because there is only one string and it will be overwritten with every call to Convert_To_Hex. In that case, you need to make a copy. Using C-style strings you would do it like this:
#include <cstring>

char Hex_Num[20];

strcpy ( Hex_Num, Convert_To_Hex ( Dec - Num ) );
Using C++ strings you would do this:
#include <string>

string Hex_Num ( Conver_To_Hex ( Dec - Num ) );
Eggbert is offline   Reply With Quote