Stack corruption using sscanf

2019-08-13 01:25发布

I have the following code to convert 2 hex digits to a byte value:

const char* data= "Some hex string";
unsigned char temp=' ';
sscanf(data, "%2hhx", &temp);

When running it I get a run time exception, saying

stack around variable temp is corrupted

What am I doing wrong? AFAIK 2hhx should convert two bytes to one...

1条回答
一夜七次
2楼-- · 2019-08-13 02:15

Are you sure your compiler and standard library support that particular specifier? It is new in C99.

Microsoft for example doesn't support it, according to this:

http://msdn.microsoft.com/en-us/library/xdb9w69d.aspx

This means that the hh is probably interpreted as a single h and it therefore expects a pointer to unsigned short, not unsigned char.

查看更多
登录 后发表回答