Stack corruption using sscanf

我是研究僧i 提交于 2019-12-11 06:54:37

问题


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:


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.



来源:https://stackoverflow.com/questions/21988901/stack-corruption-using-sscanf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!