0

I am new to c and just learning, I am trying to find a way of reading a hex value from a string and store in a byte array but I am getting a stack around variable failure.

char hexstring[] = "78E2401A417231D2", *position = hexstring;
BYTE hex[8];
size_t count = 0;

for (count = 0; count < sizeof(hex) / sizeof(hex[0]); count++) {
    sscanf(position , "%2hhx", &hex[count]);
    position += 2;
}

Error

Run-Time Check Failure #2 - Stack around the variable 'hex' was corrupted.

Jimbo Jones
  • 983
  • 4
  • 13
  • 48
  • 2
    The problem may not be in the code you have shown. Please provide a [mcve] that reproduces the problem. (Also, I assume `count2` is meant be `count`). – kaylum Dec 08 '16 at 22:46
  • 1
    Hmmm: using `"%2hhx"` makes sense when using a compiler that understands `"hh"`. OTOH `BYTE` looks like some pre-C99 macro. What happens with `#include ` and `uint8_t hex[8];`? What C compiler is used? – chux - Reinstate Monica Dec 08 '16 at 22:55
  • You might want to read this question about format specifiers: http://stackoverflow.com/questions/21782244/what-is-the-need-of-hh-and-h-format-specifiers. Also, %hhx might not be doing what you think. – bruceg Dec 08 '16 at 22:56
  • Using visual studio and win32 – Jimbo Jones Dec 08 '16 at 22:57
  • Try `sscanf(position , "%2hhx", &hex[count]);` --> `unsigned x; sscanf(position , "%2x", &x); hex[count] = x;` to cope with pre-C99 compilers. – chux - Reinstate Monica Dec 08 '16 at 22:58
  • MSVC works perfectly when wrapped in a function and `BYTE` is `unsigned char`. Printing the `hex` array values matches source string. – Weather Vane Dec 08 '16 at 23:00
  • This may help http://stackoverflow.com/a/2231285/2410359 if compiler does not understated `"hh"`. – chux - Reinstate Monica Dec 08 '16 at 23:02
  • @WeatherVane How does printing the `hex` array apply here? Certainly OP has a `sscanf()` problem. – chux - Reinstate Monica Dec 08 '16 at 23:05
  • @chux it showed that the `sscanf` worked. My (separate) test loop iterated `printf("%02X ", hex[count]);` – Weather Vane Dec 08 '16 at 23:05
  • @WeatherVane I doubt it "worked". I suspect OP's compiler ignored the "hh" and then saved the scans as `unsigned`, corrupting the memory after `hex[]` --> UB and "Run-Time Check Failure #2". If code survived, printing `hex[]` could still show the hoped for result - yet other memory is corrupted. – chux - Reinstate Monica Dec 08 '16 at 23:08
  • @chux perhaps from your other comments OP is using an old version of VS. – Weather Vane Dec 08 '16 at 23:09
  • @JimboJones What version of visual studio? – chux - Reinstate Monica Dec 08 '16 at 23:10
  • @chux I extended the array to `hex[16]`, initilialised it all to `{0}`, changed the loop to specifically `8`, and there was no corruption beyond `hex[7]`. – Weather Vane Dec 08 '16 at 23:13
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/130157/discussion-between-chux-and-weather-vane). – chux - Reinstate Monica Dec 08 '16 at 23:14
  • VS 2013, My code is to big to paste basically, its in a method and when that method comes to an end I get the error, but that var it is only used in the first 3 lines of my method. – Jimbo Jones Dec 08 '16 at 23:20

0 Answers0