Valhalla Legends Forums Archive | C/C++ Programming | Can't figure out why this isn't working

AuthorMessageTime
Eli_1
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++...

[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];
      for (int i2; i2 < sizeof(argument); i2++)
         sprintf(buffer, "%x", argument[i2]);
      
      printf("Hex Output: %s\r\n", buffer);
      delete [] buffer;
   }
   
   return 0;
}
[/code]

Command:
[code]
Hex.exe test1 test2 test3
[/code]

Output:
[code]
Hex Output:
Hex Output:
Hex Output:
[/code]

Any ideas?
March 3, 2004, 9:48 PM
iago
What is the inner loop (i2) supposed to do?
March 3, 2004, 11:36 PM
Adron
[quote author=iago link=board=30;threadid=5573;start=0#msg47318 date=1078356961]
What is the inner loop (i2) supposed to do?
[/quote]

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.
March 3, 2004, 11:42 PM
Arta
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.
March 4, 2004, 1:39 AM
Eli_1
Thanks guys, I got it fixed
the main problem was with my inner loop
[code]
for (int i2; i2 < sizeof(argument); i2++)
[/code]

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

[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;
}
[/code]
March 4, 2004, 3:28 AM
Skywing
Why don't you use for(i = 1 ....)...?
March 4, 2004, 4:22 AM
Eli_1
[quote author=Skywing link=board=30;threadid=5573;start=0#msg47381 date=1078374131]
Why don't you use for(i = 1 ....)...?
[/quote]
don't know... :P
March 4, 2004, 11:31 AM
Kp
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.
March 4, 2004, 9:02 PM

Search