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* pDestunsigned char* pSrc="ABC123";
size_t npSrclen =strlen((char*)pSrc);
unsigned char* pDest=new unsigned char[npSrclen];
memcpy(pDest, pSrc, npSrclen);
//1.static_cast 相近类型#include<iostream>
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<FUNC>(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 //3.去掉后面的字符串unsigned char* pcopy3 = mytrim(pcopy2, Srclen2);size_t copy3len = strlen((char*)pcopy3);
正确的:
//2.new
unsigned char* pSrc2 = (unsigned char*)(s_data_offset_.c_str());
int Srclen2 = strlen((char*)pSrc2);
unsigned char* pcopy2 = new unsigned char[Srclen2]; //长度
memcpy(pcopy2, pSrc2, Srclen2 ); //pcopy2 显示 pSrc中的字符
size_t copy2len = strlen((char*)pcopy2);
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.数据长度 最后一个字符一定要是'\0'
strlen() 只有遇到'\0'才会停止计算,同时计算的长度不包含'\0',否则就是个随机值。
#include <stdio.h>
#include <string.h>//当计算长度时,只有遇到'\0'才会停止计算,同时计算的长度不包含'\0'。
int main()
{char arr[10] = "abcde";int num = strlen(arr);printf("数组arr的长度为:%d\n", num); //数据长度为5return 0;
}int main()
{char arr[] = { 'a','b','c','d','e' };int num = strlen(arr);printf("数组arr的长度:%d\n", num); //数据长度为 19 此处返回的是一个随机值return 0;
}
sizeof
#include <stdio.h>
#include <string.h>//字符串长度默认+1 最后一个 "\0"
int main()
{unsigned char arr1[] = "abcde";int num1 = sizeof(arr1);printf("数组arr1的长度为:%d\n", num1); //数据长度为6unsigned char arr2[] = { 'a','b','c','d','e' };int num2 = sizeof(arr2);printf("数组arr2的长度:%d\n", num2); //数据长度为5 return 0;
}
对于 unsigned char* p 长度用 strlen((char*) p)
对于 unsigned char arr[] 长度用 sizeof(arr)
6.memcpy和strcpy
1.strcpy仅仅只能复制字符串,但是memcpy却可以复制所有的类型。
2.strcpy复制字符串的时候,什么时候结束是依赖于source处字符串的’\0’来决定的。但是memcpy复制的时候,复制多少个字节是采用第三个参数来决定的。
int main()
{char data[256];memcpy(data, "12345", 5);cout << data << endl;strcpy(data, "6789");cout << data << endl;return 0;
}
1. data 初始化为0 memset(data,0,256);
2. memcpy时把最后一个\0 复制过去 memcpy(data,"12345",6);