Valhalla Legends Forums Archive | C/C++ Programming | Simple StrToHex class

AuthorMessageTime
Eli_1
Making a class that converts from string to hex and visa versa, but for some reason it's only converting the first 8 characters to hex.

[code]
// clsconvert.h class
#include <stdio.h>
#include <string.h>

class clsconvert {
public:
   char *strtohex(char *inbuf);
};

char *clsconvert::strtohex(char *inbuf) {
char buffer[512];

char hexTable[][3] = { "00", "01" ... "FF" }; // The hex values.

   for (int i=0; i<strlen(inbuf); i++)
      strcat(buffer, hexTable[inbuf[i]]);
   
   return buffer;
}
[/code]

And here's my usage:
[code]
// main.cpp
#include <clsconvert.h>
#include <stdio.h>

int main() {
clsconvert CConvert;

   printf("%s\n", CConvert.strtohex("1 2 3 4 5 6 7 8 9 10"));
return 0;
}
[/code]

Output:
[quote]
3120322033203420
[/quote]

Why's it doing this?
April 19, 2004, 7:57 PM
Adron
Your buffer is on the stack and gets overwritten. You need to return something better, perhaps a std::string or a pointer to a static / thread-local variable.

Edit:
Actually, given your design, I'd probably add a char* or std::string class member and store the string there.
April 19, 2004, 8:24 PM
Eli_1
[quote author=Adron link=board=30;threadid=6381;start=0#msg55853 date=1082406258]
Your buffer is on the stack and gets overwritten. You need to return something better, perhaps a std::string or a pointer to a static / thread-local variable.

Edit:
Actually, given your design, I'd probably add a char* or std::string class member and store the string there.
[/quote]
I'm very new to this, can you explain a little more what you mean? For one, why is it getting overwritten?
April 19, 2004, 8:30 PM
Adron
OK, I'll try as simple an explanation as I can.

There's something called stack. This is a memory area. When you call functions, the stack is used to store data for them. Local variables that you declare in a function are stored on the stack. The stack typically builds downwards. Return addresses and function arguments are also typically stored on the stack.

To keep track of how much of the stack is in use, there's a stack pointer. The stack pointer marks the boundary between "in use" and "free".

Example:

[code]
void func3(int c)
{

}

void func2(int b)
{
func3(b * 2);
}

void func1(int a)
{
int x = a * 2;
func2(x * 3);
}

int main(int ac, char **av)
{
func1(14);
}
[/code]

When func1 is called, the stack will look like this:

[code]
...
av
ac
<the address that called main>
a (14)
<an address in main>
x <-- stack pointer points here
...
[/code]

When func3 is called, the stack will look like this:

[code]
...
av
ac
<the address that called main>
a (14)
<an address in main>
x (28)
b (84)
<an address in func1>
c (168)
<an address in func2> <-- stack pointer points here
...
[/code]

As the functions return, the stack pointer is moved back step by step to point to its original position. When you're back in main, the values are still where they used to be, but they will be overwritten as the stack is used by other function calls:

[code]
...
av
ac
<the address that called main> <-- stack pointer points here
a (14) <---- this and anything below it will be overwritten any time soon
<an address in main>
x (28)
b (84)
<an address in func1>
c (168)
<an address in func2>
...
[/code]


What you are doing is return a pointer that points into the area on the stack that is "free", available for printf to use while printing out the string.
April 19, 2004, 8:50 PM
Eli_1
Thanks, Adron ;D.
April 19, 2004, 9:03 PM
Maddox
You can, however, do this:

[code]
char *func() { return "Some string"; }
[/code]
April 20, 2004, 12:58 AM
K
[quote author=Maddox link=board=30;threadid=6381;start=0#msg55883 date=1082422686]
You can, however, do this:

[code]
char *func() { return "Some string"; }
[/code]
[/quote]

I would return that as a const char*. since by default on most compilers, characer arrays created like that are placed in the read only section of the program, and writing to it could be bad.
April 20, 2004, 1:43 AM
Eibro
From experience, I've found it best to either stick with std::string, and return an object of that type, or std::auto_ptr< char > and return a block of dynamically allocated mem.
April 20, 2004, 2:17 AM
Moonshine
Additionally, if you're just using one function, it might not even be worth putting it in a class -- you might want to just use a function. Unless of course you're just putting it in a class for practise.
April 20, 2004, 3:26 PM
Eli_1
This is the first class I'v ever made. So it is for learning, because I use classes a lot in VB and I want to be able to do the same w/ C++.

The class isn't specificly for StrToHex. In VB I have a class called clsConvert, which has all my conversion functions. Including StrToHex, StrReverse, StrToBin, HexOutput, all those types of functions. I was just trying to convert it to C++ for practice -- StrToHex just happened to be first.
April 20, 2004, 6:23 PM
Adron
For the purpose of encapsulating functions, you could just use a namespace in C++. Might be better.
April 20, 2004, 8:51 PM

Search