在sscanf()

时间:2016-02-19 11:43:01

标签: c++ c

我试图编写一个函数来将十六进制值字符串转换为字节数组。这段代码有什么问题?

在调用free()之后,我得到错误HEAP CORRUPTION DETECTED。如果我评论对sscanf的调用一切正常。 sscanf会写一些超出malloc分配的内存的东西吗?

unsigned char* hextobytes(const string& hex) {
    size_t size = hex.size() / 2;
    unsigned char* bytes = (unsigned char*)malloc(size);
    const string::value_type* pos = hex.c_str();

    for (size_t c = 0; c < size; c++) {
        sscanf((pos + 2 * c), "%2hhx", bytes + c);
    }

    return bytes;
}

int _tmain(int argc, _TCHAR* argv[]) {
    string hex = "FFFF";
    unsigned char* bytes = hextobytes(hex);
    free(bytes);

    return 0;
}

更新:我正在开发Visual Sudio 2013

2 个答案:

答案 0 :(得分:3)

我找到了答案here

Microsoft版本的scanf不支持长度修饰符&#39; hh&#39;指定unsigned char。它支持修饰符&#39; h&#39;它指定一个短整数。

使用short int数组而不是unsigned char可以解决我的问题。

答案 1 :(得分:2)

你的源代码的主要问题是它是C ++,但编程非常C风格。

其他人指出,发布的代码不会显示您声明的错误。

但是,如果可以的话,请允许我展示如何做到这一点C ++风格,没有任何堆腐败的机会因为C ++为我们提供了避免“裸”指针所需的所有工具:

#include <string>
#include <vector>
#include <iostream>

std::vector< unsigned char > hextobytes( const std::string & hex )
{
    std::vector< unsigned char > rc;
    for ( size_t i = 0; i < hex.size(); i += 2 )
    {
        // this may throw std::invalid_argument if no
        // conversion can be performed
        // formally std::out_of_range would be also a
        // possibility, but not with a two-digit hex...
        rc.push_back( static_cast< unsigned char >(
            std::stoul( hex.substr( i, 2 ), 0, 16 ) )
        );
    }
    return rc;
}

int main()
{
    std::string hex( "FFFF" );
    std::vector< unsigned char > bytes = hextobytes( hex );
    for ( auto a : bytes )
    {
        std::cout << static_cast< int >( a ) << "\n";
    }
    return 0;
}
相关问题