简体   繁体   中英

Convert the BSTR hexa decimal string to BYTE * in C++

I was trying to convert the BSTR string to Hex BYTE *

I receive the response from the COM as BSTR

My RAW methods in tlh

 virtual HRESULT __stdcall test(
        /*[in]*/ BSTR domain,
        /*[in]*/ BSTR snap,
        /*[out,retval]*/ BSTR * pRetVal ) = 0;

BSTR aa = L"559EB7F000260044F06BB01A3A000055";

Which i want to convert into BYTE myHex[16]

Sample response {0x55,0x97,0xD6,0x00,0x02,0x60,0x01,0x44,0xF0,0x6B,0xB0,0x1A,0x3A,0x00,0x00,0x55};

One way is to use swscanf . You could do it via a stringstream also.

#include <cstdio>

...

for (int i = 0; i < 16; ++i)
{
    if ( 1 != std::swscanf(aa + 2 * i, L"%02X", myHex + i) )
         throw std::runtime_error("Conversion failure");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM