Search code examples
wxwidgets

wxWidgets wxString


I am using wxWidgets with c++. I have a wxTextCtrl in which the user inputs a set of bytes in hex format separated by space. I want to get them in unsigned char array. How can I convert from a string like "AB D3 4F A A1 0B" to get the resulting array: [171, 211, 79, 10, 161, 11]?


Solution

  • I would use wxStringTokenizer to break up the string into individual hexes, then sscanf to convert them into numeric values.

    Something like this:

    std::vector<unsigned char> vec;
    wxStringTokenizer tkz(thetextCtrl->GetValue(), wxT(" "));
    while ( tkz.HasMoreTokens() )
    {
        wxString token = tkz.GetNextToken();
        unsigned char v;
        sscanf(token.c_str(),"%x",&v)
        vec.push_back( v );
    }