Help!

Started by
3 comments, last by Adrian99420 14 years, 5 months ago
Hi all, urgent help needed. I faced error of "stack around the variable "hexxal" was corrupted. Any suggestion? UCHAR ucMifareKey[6] = {0}; ULONG ulMifareKeyLen = 0; m_strAuthentKey = "FFFFFFFFFFFF"; CStringToUchar(m_strAuthentKey,ucMifareKey,&ulMifareKeyLen); void CContactlessDemoVCDlg::CStringToUchar(CString str, UCHAR *ucBuffer, ULONG *ulBufferLen) { int Length = 0; int DataLength = 0; char cstr[] =""; char strcstring[512] =""; byte hexval=0x00; int i = 0; Length = str.GetLength(); for (i = 0; i<Length; i++) strcstring = str.GetAt(i); DataLength = Length / 2; for (i = 0; i<DataLength; i++) { cstr[0] = strcstring[2*i]; cstr[1] = strcstring[2*i+1]; sscanf( cstr, "%02x", &hexval ); ucBuffer=hexval; } *ulBufferLen = DataLength; }
Advertisement
sscanf with a format specifier of "%02x" will read in an int, but you gave it a pointer to a single byte. You can try making hexval an int. If what you are reading is in range, that should work fine.
Hi alvaro,

Thanks for the reply, but when i changed the hexval to int, here comes the error of "Stack around the variable 'cstr' was corrupted". Any advice?
UCHAR ucMifareKey[6] = {0};
ULONG ulMifareKeyLen = 0;
m_strAuthentKey = "FFFFFFFFFFFF";
CStringToUchar(m_strAuthentKey,ucMifareKey,&ulMifareKeyLen);

void CContactlessDemoVCDlg::CStringToUchar( const CString& str, UCHAR *ucBuffer, ULONG *ulBufferLen)
{
    int Length = 0;
    int DataLength = 0;
    char cstr[3] = {}; ""; // Previous code allocates only one char for cstr
    char strcstring[512] =""; // Not needed
    byte int hexval=0; 0x00;
    int i = 0; // Moved

    Length = str.GetLength();
    for (i = 0; i<Length; i++)
        strcstring = str.GetAt(i);

    assert( str.GetLength() % 2 == 0 );
    DataLength = Length str.GetLength() / 2;

    for ( int i = 0; i<DataLength; i++)
    {
        cstr[0] = str[2*i]; strcstring[2*i]; // CString provides an operator[]
        cstr[1] = str[2*i+1]; strcstring[2*i+1]; // Buffer overrun here, if str.GetLength() is odd
        sscanf( cstr, "%02x", &hexval );
        ucBuffer= static_cast<UCHAR>(hexval);
    }

    *ulBufferLen = DataLength;
}


<a href=">

[Edited by - _fastcall on November 4, 2009 12:43:25 AM]
Hi all,

I got the things work. Thanks for the help.

This topic is closed to new replies.

Advertisement