堆栈损坏使用sscanf [英] Stack corruption using sscanf

查看:585
本文介绍了堆栈损坏使用sscanf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码将2个十六进制数字转换为字节值:

  const char * data =串; 
unsigned char temp ='';
sscanf(data,%2hhx,& temp);

运行时遇到运行时异常,说


堆栈变量temp已损坏


我做错了什么? AFAIK 2hhx 应将两个字节转换为一个...

解决方案

Are你确定你的编译器和标准库支持那个特定的说明符?这是C99中的新功能。



Microsoft根本不支持它:



a href =http://msdn.microsoft.com/en-us/library/xdb9w69d.aspx> http://msdn.microsoft.com/en-us/library/xdb9w69d.aspx p>

这意味着 hh 可能被解释为单个 h ,因此需要一个指向无符号short ,而不是 unsigned char 的指针。


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...

解决方案

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.

这篇关于堆栈损坏使用sscanf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆