Converting a hex string to a byte array

后端 未结 19 2130
时光取名叫无心
时光取名叫无心 2020-11-22 12:10

What is the best way to convert a variable length hex string e.g. \"01A1\" to a byte array containing that data.

i.e converting this:

st         


        
19条回答
  •  时光取名叫无心
    2020-11-22 13:09

    You said "variable length." Just how variable do you mean?

    For hex strings that fit into an unsigned long I have always liked the C function strtoul. To make it convert hex pass 16 as the radix value.

    Code might look like:

    #include 
    std::string str = "01a1";
    unsigned long val = strtoul(str.c_str(), 0, 16);
    

提交回复
热议问题