char*和unsigned 的转换弄了好长时间都没有成功,最后用的强制类型转换成功了
char *tem = new char[4];
unsigned* ret =(unsigned*)tem;//需要这样转换,unsigned ret = unsigned(*tem)这样转换出来竟然是错的,不知道哪位能给解答一下
另外,在c中,字符串和字符数组是有区别的,参考http://c.biancheng.net/view/337.html
另附一点心得:
template<class T>
void put_val(std::string& buf, T val) {
buf.append((unsigned char*)&val, (unsigned char*)&val + sizeof(T));
}
std::string b3dm_buf;
b3dm_buf += "b3dm";
cout << "b3dm_buf length:"<<b3dm_buf.length() << endl;
int version = 1;
put_val(b3dm_buf, version);
上面的append()函数的两个参数是unsigned char*的首尾地址,put_val将T类型的首先转换为unsigned char*类型,然后将字符添加到buf中。
---------------------
作者:少年不识123
来源:CSDN
原文:https://blog.csdn.net/sinat_34816302/article/details/84544345
版权声明:本文为博主原创文章,转载请附上博文链接!