• Welcome to Valhalla Legends Archive.
 

Simple StrToHex class

Started by Eli_1, April 19, 2004, 02:57 PM

Previous topic - Next topic

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.


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


And here's my usage:

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


Output:
Quote
3120322033203420

Why's it doing this?

Adron

#1
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.

Eli_1

Quote from: Adron on April 19, 2004, 03:24 PM
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.
I'm very new to this, can you explain a little more what you mean? For one, why is it getting overwritten?

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:


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


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


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


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


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


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:


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



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.

Eli_1


Maddox

You can, however, do this:


char *func() { return "Some string"; }
asdf.

K

Quote from: Maddox on April 19, 2004, 07:58 PM
You can, however, do this:


char *func() { return "Some string"; }


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.

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.
Eibro of Yeti Lovers.

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.

Eli_1

#9
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.

Adron

For the purpose of encapsulating functions, you could just use a namespace in C++. Might be better.