std::string 转 byte[]

来源:互联网 发布:教师资格考试软件下载 编辑:程序博客网 时间:2024/05/16 15:18
 #include "stdafx.h"
#include <iostream>
#include <string.h>

int _tmain(int argc, _TCHAR* argv[])
{
std::string teststr = "123456abc";

const char* a = teststr.c_str();

char* b = new char[teststr.length() + 1];

memset(b, 0, teststr.length() + 1);

memcpy(b, a, teststr.length());

unsigned char* c = (unsigned char*)b;  //  byte与  unsigned char*相同

for (int i = 0; i < teststr.length(); i++)
{
std::cout << i << ":" << (int)(c[i]) << std::endl;
}

return 0;
}
0 0
原创粉丝点击