#include
#include
#include
#include
void HexToStr(unsigned char *pbDest, unsigned char *pbSrc, int nLen)
{
char ddl,ddh;
int i;
for (i=0; i<nLen; i++){
ddh = 48 + (pbSrc[i] >> 4);
ddl = 48 + (pbSrc[i] & 0xf);
if (ddh > 57) ddh = ddh + 7;
if (ddl > 57) ddl = ddl + 7;
pbDest[i * 2] = ddh;
pbDest[i * 2 + 1] = ddl;
}
pbDest[nLen*2] = '\0';
}
void StrToHex(unsigned char* pbDest, unsigned char* pbSrc, int nLen)
{
char h1, h2;
unsigned char s1, s2;
int i;
for (i = 0; i < nLen; i++)
{
h1 = pbSrc[2 * i];
h2 = pbSrc[2 * i + 1];
s1 = toupper(h1) - 0x30; //十六进制 0x30 , dec十进制 48 , 图形 0
if (s1 > 9)
s1 -= 7;
s2 = toupper(h2) - 0x30;
if (s2 > 9)
s2 -= 7;
pbDest[i] = s1 * 16 + s2;
}
}
static void hex_print(const void* pv, size_t len)
{
const unsigned char* p = (const unsigned char*)pv;
if (NULL == pv) {
printf("NULL");
}
else {
size_t i = 0;
for (; i < len; ++i) {
printf("%02x ", *p++);
}
}
printf("\n");
}
int main(int aergv,char *argc){
/*
// 十六进制的数组
unsigned char ArrayHex[16] = {
0x2c, 0x57, 0x8f, 0x79, 0x27, 0xa9, 0x49, 0xd3,
0xb5, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
// 十六进制的字符串
char *strHex = "01aa0304050607083f0add0c0d0e0f00";
*/
char arraystr[16];
char *strHex = "aa0304050607083f0add0c0d0e001f00";
char str[16];
printf("sizeof(strhex):%ld\n",sizeof(strHex)); //指针占8byte
printf("sizeof(str):%ld\n",sizeof(str)); //16个字符占16byte
StrToHex(arraystr,strHex,sizeof(arraystr));
hex_print(arraystr,sizeof(arraystr));
HexToStr(str,arraystr,sizeof(str));
printf("%s\n",str);
return 0;
}
@ubuntu:~/$ ./strtohex
sizeof(strhex):8
sizeof(str):16
aa 03 04 05 06 07 08 3f 0a dd 0c 0d 0e 00 1f 00
AA0304050607083F0ADD0C0D0E001F00
本源码实现的功能:提取十六进制数组的内容,将该内容的数据进行左移<<三位且低位补零的位运算处理,新生成的二进制数转转十六进制数表示。
#include
#include
#include
#include
#include
#include
#include
#include
#include
//十六进制字符串转二进制字符串
void hexstrtobin(char *sDest,const char *sSrc,int len)
{
int i=0;
char Dict[17][5] =
{
"0000", "0001", "0010", "0011",
"0100", "0101", "0110", "0111",
"1000", "1001", "1010", "1011",
"1100", "1101", "1110", "1111",
};
for(i=0;i<len;i++){
//char temp[5]={0};
int n = 16;
if (sSrc[i] >= 'a' && sSrc[i] <= 'f') n = sSrc[i] - 'a' + 10;
if (sSrc[i] >= 'A' && sSrc[i] <= 'F') n = sSrc[i] - 'A' + 10;
if (sSrc[i] >= '0' && sSrc[i] <= '9') n = sSrc[i] - '0';
//sprintf(temp,"%s", Dict[n]);
//memcpy(&dest[i*4],temp,4);
memcpy(&sDest[i*4],Dict[n],4);
}
return;
}
//二进制字符串到十六进制字符串函数
void bintohexstr( char *sDest,const char *sSrc, int nSrcLen){
int times=nSrcLen/4;
char temp[times];
int x=0;
for(int i=0;i<times;i++){
x=8*(sSrc[i*4]-'0');
x+=4*(sSrc[i*4+1]-'0');
x+=2*(sSrc[i*4+2]-'0');
x+=sSrc[i*4+3]-'0';
sprintf(temp+i,"%1x",x);
}
memcpy(sDest,temp,times);
}
void hextostr(char *pbDest, const uint8_t *pbSrc, int nLen)
{
char ddl,ddh;
int i;
for (i=0; i<nLen; i++){
ddh = 48 + (pbSrc[i] >> 4);
ddl = 48 + (pbSrc[i] & 0xF);
if (ddh > 57) ddh = ddh + 7;
if (ddl > 57) ddl = ddl + 7;
pbDest[i * 2] = ddh;
pbDest[i * 2 + 1] = ddl;
}
pbDest[nLen*2] = '\0';
}
void strtohex(unsigned char* pbDest, const unsigned char* pbSrc, int nLen)
{
char h1, h2;
unsigned char s1, s2;
int i;
for (i = 0; i < nLen; i++)
{
h1 = pbSrc[2 * i];
h2 = pbSrc[2 * i + 1];
s1 = toupper(h1) - 0x30; //十六进制 0x30 , dec十进制 48 , 图形 0
if (s1 > 9)
s1 -= 7;
s2 = toupper(h2) - 0x30;
if (s2 > 9)
s2 -= 7;
pbDest[i] = s1 * 16 + s2;
}
}
static void hex_print(const void* pv, size_t len)
{
const unsigned char* p = (const unsigned char*)pv;
if (NULL == pv) {
printf("NULL");
}
else {
size_t i = 0;
for (; i < len; ++i) {
printf("%02x ", *p++);
}
}
printf("\n");
}
int main(int *argc, char *argv[]){
// 测试demo如下:
unsigned char hex_serial[]={0x21,0x22,0x23,0x24,0x60,0x48,0x20,0x20,0x20,0x83,0x4e,0x37,0x30,0x37,0x34,0x44,0x4a,0x52,0x33};
unsigned char serial[16];
for(int i=0;i<16;i++){
serial[i]=hex_serial[i+3];
}
hex_print(serial,sizeof(serial));
unsigned char hex_str[32];
hextostr(hex_str,serial,16);
printf("hex_str: %s\n",hex_str);
char source[128];
hexstrtobin(source,hex_str,strlen(hex_str));
printf("source_bin: %s\n", source);
char dest[256];
for(int i=3;i<128;i++){
dest[i-3]=source[i];
}
for(int i=125;i<128;i++){
dest[i] = '0';
}
dest[128]='\0';
printf("dest_bin: %s\n", dest);
bintohexstr(hex_str,dest,strlen(dest));
printf("strlen:%ld\n",strlen(dest));
printf("hex_str: %s\n",hex_str);
strtohex(serial,hex_str,sizeof(serial));
hex_print(serial,sizeof(serial));
return 0;
}
20 60 48 20 20 20 83 4e 37 30 37 34 44 4a 52 33
hex_str: 206048202020834E37303734444A5233
source_bin: 00100000011000000100100000100000001000000010000010000011010011100011011100110000001101110011010001000100010010100101001000110011
dest_bin: 00000011000000100100000100000001000000010000010000011010011100011011100110000001101110011010001000100010010100101001000110011000
strlen:128
hex_str: 0302410101041a71b981b9a2225291980010000001100000010010000010000000100000001000001000001101001110001101110011000000110111001101000100010001001010010100100011001100000011000000100100000100000001000000010000010000011010011100011011100110000001101110011010001000100010010100101001000110011000
03 02 41 01 01 04 1a 71 b9 81 b9 a2 22 52 91 98
#include
#include
char* substring(char* ch,int pos,int length)
{
//定义字符指针 指向传递进来的ch地址
char* pch=ch;
//通过calloc来分配一个length长度的字符数组,返回的是字符指针。
char* subch=(char*)calloc(sizeof(char),length+1);
int i;
//只有在C99下for循环中才可以声明变量,这里写在外面,提高兼容性。
pch=pch+pos;
//是pch指针指向pos位置。
for(i=0;i<length;i++)
{
subch[i]=*(pch++);
//循环遍历赋值数组。
}
subch[length]='\0';//加上字符串结束符。
return subch; //返回分配的字符数组地址。
}
int main(){
char* result;
char* tstStr = "abcdefg";
result = substring(tstStr,0,2);
printf("结果:%s",result);
}
主函数中定义了一个字符串指针,该字符串指针未初始化,该字符串指针作为实参调用子函数参与计算,子函数计算结束后,发生错误,原因参考如下:
分析:
实参指针p 没有指向任何类容为空,当实参给形参传递值时,传递的实际上是NULL,形参指针p 分配空间,仅仅只是给形参p分配了空间,当函数退出时,实参指针指向的内容还是为空,这时去访问实参p指向的内容,因为指针没有指向确切地址,就会产生段错误,同时因为函数new了空间没有释放,还会造成内存泄漏。
传参问题参考:https://blog.csdn.net/weixin_40026739/article/details/121582395