UNICODE下CString转string

真搞不懂,为毛C++这么多类型转换。。

CString m_str(_T("fuck conversion"));

char *chr=new char[m_str.GetLength()+1];
WideCharToMultiByte(CP_ACP,0,m_str.GetBuffer(),-1,chr,m_str.GetLength()+1,NULL,NULL);
string str=chr;

cout<<str;

 

2、char转string

char ch[10] = {0};

itoa(i, ch, 10);

std::string str = (char *)ch;

 

3、int转byte

public byte[] intToByte(int i) {
        byte[] abyte0 = new byte[4];
        abyte0[0] = (byte) (0xff & i);
        abyte0[1] = (byte) ((0xff00 & i) >> 8);
        abyte0[2] = (byte) ((0xff0000 & i) >> 16);
        abyte0[3] = (byte) ((0xff000000 & i) >> 24);
        return abyte0;
    }
4、byte转int
public  static int bytesToInt(byte[] bytes) {
        int addr = bytes[0] & 0xFF;
        addr |= ((bytes[1] << 8) & 0xFF00);
        addr |= ((bytes[2] << 16) & 0xFF0000);
        addr |= ((bytes[3] << 24) & 0xFF000000);
        return addr;
    }

posted @ 2018-02-05 14:56  yzl050819  阅读(383)  评论(0编辑  收藏  举报