0
点赞
收藏
分享

微信扫一扫

uint8_t/unsigned char 与string互转

后来的六六 2022-03-24 阅读 60
c++

一、uint8_t转string

#include <iostream>
using namespace std;
int main() {
    typedef uint8_t  U8;
    U8 Data[] = { 0x48, 0x65, 0x6C, 0x6C, 0x6F,0x2E };
        std::string temp;
        for (uint8_t i : Data)
        {
            temp += i;
        }

        std::cout << temp << std::endl;
        std::string tempa="a";
        temp=tempa+temp;
         std::cout << temp << std::endl;
	return 0;
}

 

二、string转char

#include <iostream>
using namespace std;
int main() {
    typedef uint8_t  U8;
    U8 Data[] = { 0x48, 0x65, 0x6C, 0x6C, 0x6F,0x2E };
        std::string temp;
        for (uint8_t i : Data)
        {
            temp += i;
        }

        std::cout << temp << std::endl;
        std::string tempa="a";
        temp=tempa+temp;
        std::cout << temp << std::endl;
        const char* mlog_s=temp.c_str();
        cout<<"a"<<mlog_s<<endl;
        while(* mlog_s!=0)
       {
         printf("%02x ", *mlog_s);
         mlog_s++;
       }
   return 0;
}

 

举报

相关推荐

0 条评论