c ++将_bstr_t转换为int并将int转换为_bstr_t

时间:2013-03-01 08:53:53

标签: c++ int

我遇到了一个问题,这是我的代码,

          _bstr_t bstrTrans=L"2";
    wsprintf(buf,L"insert into inven(ID) values(%s)",(wchar_t *)bstrTrans)//insert into database
//this can work fine, now we want to let bstrTrans add 1, likes this
            int bstrTrans2 = atoi(bstrTrans) + 1;
    wsprintf(buf,L"insert into inven(ID) values(%s)",(wchar_t *)bstrTrans2 )
// now ,it cannot work.

所以任何人都可以帮助我吗?

2 个答案:

答案 0 :(得分:1)

未经测试,但它应该可以正常工作:

const _wchar_t pBuffer[16] = {0};
wsprintf(pBuffer, L"%u", bstrTrans2)
_bstr_t bstrTrans3(pBuffer);
wsprintf(buf,L"insert into inven(ID) values(%s)",(wchar_t *)bstrTrans3 );

答案 1 :(得分:1)

为什么不简单的s - > d?

    _bstr_t bstrTrans=L"2";
    wsprintf(buf,L"insert into inven(ID) values(%s)",(wchar_t *)bstrTrans) 

//insert into database
//this can work fine, now we want to let bstrTrans add 1, likes this
    int bstrTrans2 = atoi(bstrTrans) + 1;
    wsprintf(buf,L"insert into inven(ID) values(%d)",bstrTrans2 )
编辑:嗯,这个工作?

    _bstr_t bstrTrans=L"2";
    wsprintf(buf,L"insert into inven(ID) values(%s)",(wchar_t *)bstrTrans) 

//insert into database
//this can work fine, now we want to let bstrTrans add 1, likes this
    char t[30];
    bstrTrans = itoa ( atoi (bstrTrans) + 1 , t,10);
    wsprintf(buf,L"insert into inven(ID) values(%s)",(wchar_t *)bstrTrans)