1.类型转换:
- //1.赋值一个不知道长度的字符串
-
- unsigned char s[] = "kobe8llJfFwFSPiy"; //1.用一个字符串初始化变量
- unsigned int s_length = strlen((char*)s); //2.获取字符串长度
-
-
- //2.字符串里有双引号"" 需要加\ 长度数据类型:size_t
- const unsigned char sjson[] = "{\"name\":\"MABMEC3F8\", \"value\" : \"W6tvvyYEF7ZkFH8V\" }";
- size_t sjson_length = strlen((char*)sjson);
-
-
- //3.两个 unsigned char*之间的拷贝:unsigned char* pSrc, unsigned char* pDest
-
- unsigned char* pSrc="ABC123";
- size_t npSrclen =strlen((char*)pSrc);
- unsigned char* pDest=new unsigned char[npSrclen];
- memcpy(pDest, pSrc, npSrclen);
- //1.static_cast 相近类型
-
-
- #include
- using namespace std;
-
- int main()
- {
- double d = 12.34;
- int a = static_cast<int>(d);
- return 0;
- }
-
-
- //2.reinterpret_cast 不相关类型
- typedef void(*FUNC) ();
-
- int DoSomething(int i)
- {
- cout << "DoSomething" << endl;
- return 0;
- }
- int main()
- {
- //reinterpret_cast可以让编译器以FUNC的定义方式去看待DoSomething函数
- //所以非常的BUG,下面的转换函数指针的代码时不可移植的,所以不建议使用
- //C++不保证所有的函数指针都被一样使用,所以这样用有时会产生不确定的结果
- FUNC f = reinterpret_cast
(DoSomething); - f();
- system("pause");
- return 0;
- }
-
-
- //3.const_cast 删除变量的const属性
- int main()
- {
- volatile const int a = 2;
- int* p = const_cast<int*>(&a);
- *p = 3;
- cout << a << endl;
- system("pause");
- return 0;
- }
-
- //4.explicit关键字阻止转换构造函数进行的隐式类型转换的发生
- class A
- {
- public:
- explicit A(int a)
- {
- cout << "A(int a)" << endl;
- }
- A(const A* a)
- {
- cout << "const A* a" << endl;
- }
- private:
- int _a;
- };
- int main()
- {
- A a1(1); //隐式转换--A tmp(1); A a2(tmp)
- A a2 = 1;
- system("pause");
- return 0;
- }
-
- int main()
- {
- unsigned char data[] = {0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x57, 0x6F, 0x72, 0x6C, 0x64};
- int len = sizeof(data) / sizeof(unsigned char);
- string str(data, data+len);
- return 0;
- }
3. 拷贝 一定要new
- //1. 不new
- unsigned char* pSrc1 = (unsigned char*)(s_data_offset_.c_str());
- int Srclen1 = strlen((char*)pSrc1);
- unsigned char pcopy1[] = { 0 };
- memcpy(pcopy1, pSrc1, Srclen1 ); //debug 显示字符串中的字符无效
- int copy1len = strlen((char*)pcopy1); //长度 24个 0x18
-
-
- //2.new 长度变了 16->21个 多出的字符为空字符
- unsigned char* pSrc2 = (unsigned char*)(s_data_offset_.c_str());
- int Srclen2 = strlen((char*)pSrc2);
- //int m_sconst = static_cast
(Srclen2); 变量变常量 - unsigned char* pcopy2 = new unsigned char[Srclen2]; //长度 21个 0x15
- memcpy(pcopy2, pSrc2, Srclen2 ); //pcopy2 显示 pSrc中的字符
- size_t copy2len = strlen((char*)pcopy2);
-
-
- //3.去掉后面的字符串
- unsigned char* pcopy3 = mytrim(pcopy2, Srclen2);
- size_t copy3len = strlen((char*)pcopy3);
4.copy 出来的是有空字符串的
去掉空字符串
- //3.去掉 原始的是16-实际的是21 相差5个
- //字符串去空格
- unsigned char* mytrim(unsigned char* pSrc, int pSrcLen)
- {
- unsigned char* copy = (unsigned char*)malloc(pSrcLen);
- memset(copy, 0, pSrcLen);
- unsigned int i = 0, j = 0;
- for (i = 0; i < pSrcLen; i++)
- {
- if (pSrc[i] != ' ')
- {
- copy[j++] = pSrc[i];
-
- }
- }
- copy[j] = '\0';
- int strCopy2len = strlen((char*)copy);
- return copy;
- }
5.数据长度
strlen() 只有遇到'\0'才会停止计算,同时计算的长度不包含'\0',否则就是个随机值。
- #include
- #include
-
- //当计算长度时,只有遇到'\0'才会停止计算,同时计算的长度不包含'\0'。
- int main()
- {
- char arr[10] = "abcde";
- int num = strlen(arr);
- printf("数组arr的长度为:%d\n", num); //数据长度为5
-
- return 0;
- }
-
-
- int main()
- {
- char arr[] = { 'a','b','c','d','e' };
- int num = strlen(arr);
- printf("数组arr的长度:%d\n", num); //数据长度为 19 此处返回的是一个随机值
-
- return 0;
- }
-