Valhalla Legends Archive

Programming => General Programming => C/C++ Programming => Topic started by: Eli_1 on March 03, 2004, 03:48 PM

Title: Can't figure out why this isn't working
Post by: Eli_1 on March 03, 2004, 03:48 PM
This is just a little program I wanted to try to make because I don't know how to do StrToHex conversion in C/C++...


#include <stdio.h>
#include <string.h>

int main(int argc, char **argv) {

   if (argc == 1) {
      printf("Argument needed");
      return -1;
   }

   for (int i = 0; i < argc; i++) {
      char buffer[2048];
      if (i == 0)
         continue; //Ghetto :(
      
      if (argv[i] == NULL)
         continue;
      
      char *argument = argv[i];
      for (int i2; i2 < sizeof(argument); i2++)
         sprintf(buffer, "%x", argument[i2]);
      
      printf("Hex Output: %s\r\n", buffer);
      delete [] buffer;
   }
   
   return 0;
}


Command:

Hex.exe test1 test2 test3


Output:

Hex Output:
Hex Output:
Hex Output:


Any ideas?
Title: Re:Can't figure out why this isn't working
Post by: iago on March 03, 2004, 05:36 PM
What is the inner loop (i2) supposed to do?
Title: Re:Can't figure out why this isn't working
Post by: Adron on March 03, 2004, 05:42 PM
Quote from: iago on March 03, 2004, 05:36 PM
What is the inner loop (i2) supposed to do?

Studying the logic flow of the program makes me believe it's intended to go through the characters in the argument, adding the hexadecimal value for each to the buffer.
Title: Re:Can't figure out why this isn't working
Post by: Arta on March 03, 2004, 07:39 PM
You shouldn't be calling delete on buffer. Only call delete on memory allocated with new.  Also, every time the inner loop iterates (other than the first) it'll overwrite whatever was in buffer.
Title: Re:Can't figure out why this isn't working
Post by: Eli_1 on March 03, 2004, 09:28 PM
Thanks guys, I got it fixed
the main problem was with my inner loop

for (int i2; i2 < sizeof(argument); i2++)


the above was never working because I didn't have it as int i2 = 0; ect...

for anymore who wants to see, here's the working code


#include <stdio.h>
#include <string.h>

int main(int argc, char **argv) {

   if (argc == 1) {
      printf("Argument needed");
      return -1;
   }

   for (int i = 0; i < argc; i++) {
      char buffer[2048];
      if (i == 0)
         continue; //Ghetto :(
      
      if (argv[i] == NULL)
         continue;
      
      char *argument = argv[i], tmp[4];
      for (int i2 = 0; i2 < strlen(argument); i2++) {
         sprintf(tmp, "%x", argument[i2]);
         strcat(buffer, tmp);   
      }
      
      printf("Hex Output: %s\r\n", buffer);
   }
   
   return 0;
}
Title: Re:Can't figure out why this isn't working
Post by: Skywing on March 03, 2004, 10:22 PM
Why don't you use for(i = 1 ....)...?
Title: Re:Can't figure out why this isn't working
Post by: Eli_1 on March 04, 2004, 05:31 AM
Quote from: Skywing on March 03, 2004, 10:22 PM
Why don't you use for(i = 1 ....)...?
don't know...  :P
Title: Re:Can't figure out why this isn't working
Post by: Kp on March 04, 2004, 03:02 PM
hmm, I think that will behave badly if you use an upper-ASCII character in an argument.  Assuming your architecture considers char to be signed, it will sign extend the character to pass it to sprintf.  Since you're using the horrible sprintf instead of snprintf, it can overwrite your array if you pass a too-large value.  Sign extending (char)-1 produces 0xffffffff, which is going to be 8 bytes of output (you aren't asking sprintf to do the 0x).  8 > 4, so you'll overflow the tmp buffer.  Bad things result.