Conversion of char* and unsigned

The conversion between char* and unsigned has not been successful for a long time, and the last mandatory type conversion is successful

char *tem = new char[4];

unsigned* ret =(unsigned*)tem;//It needs to be converted in this way, unsigned ret = unsigned(*tem) is actually wrong, I don’t know who can give me an answer

In addition, in c, there is a difference between strings and character arrays, refer to http://c.biancheng.net/view/337.html

Attach a little experience:

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);

The two parameters of the above append() function are the first and last addresses of unsigned char*, put_val first converts the T type to unsigned char* type, and then adds the characters to buf.
--------------------- 
Author: Juvenile Ignorant 123 
Source: CSDN  Original
: https://blog.csdn.net/sinat_34816302/article/details/84544345Copyright 
Disclaimer: This article is an original article of the blogger, please attach the link to the blog post for reprinting!

Guess you like

Origin blog.csdn.net/sinat_34816302/article/details/84553436