strcpy 不能接收 unsigned char [] 吗?

以下是相关的C++源代码:

typedef unsigned char BTUINT8;

//... 省略无关代码 ...

BTUINT8 szDevName[BTSDK_DEVNAME_LEN] = { 0 };
BTUINT8 szTmp[32] = { 0 };

//... 省略无关代码 ...

strcpy(szTmp, szDevName);

error C2664: 'strcpy' : cannot convert parameter 1 from 'unsigned char [32]' to 'char *'

以上代码编译不通过,如果把unsigned char改成char那么这一行可以通过编译,但其他部分代码就要进行很大的修改了!
有没有办法让strcpy接收unsigned char []参数?

或者干脆换其他函数来完成字符串拷贝?


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
#include<stdio.h> #include<string.h> #include<stdlib.h> #include<ctype.h> #include<openssl/hmac.h> char *signature_calculate(char *json_obj, char *key){ unsigned char *key_byte = (unsigned char *)key; char *sorted_json = to_url_query(json_obj); unsigned char *dataddd = (unsigned char *)sorted_json; unsigned char *signature = HMAC(EVP_sha256(), key_byte, strlen(key), dataddd, strlen(dataddd), NULL, NULL); char hex_signature = malloc(2 * EVP_MAX_MD_SIZE + 1); for(int i=0; i<EVP_MAX_MD_SIZE; i++) { sprintf(&hex_signature[i2], "%02x", signature[i]); } return hex_signature; } typedef struct { char key[256]; char value[256]; } KeyValue; int compare(const void a, const void b) { return strcmp(((KeyValue)a)->key, ((KeyValue)b)->key); } char *sort_dict(KeyValue *array, int size) { // 对KeyValue数组按ASCII码升序排序 qsort(array, size, sizeof(KeyValue), compare); char *query_list = malloc(size * 256); int len=0; for(int i=0; i<size; i++) { if(strlen(array[i].value)==0){ // 如果值为空或者空字符串则不拼接 continue; } char *key = array[i].key; char *value = array[i].value; if(isalpha(value[0]) && isalnum(value[1]) && strcmp(value, "true")!=0 && strcmp(value, "false")!=0) { sprintf(&query_list[len], "%s=%s&", key, value); } else { sprintf(&query_list[len], "%s="%s"&", key, value); } len = strlen(query_list); } if(len>0) { query_list[len-1] = 0; } return query_list; } char *to_url_query(char *json, char *prefix){ // 将json字符串转换为URL键值对形式的字符串 int len = strlen(json); KeyValue *array = malloc(len * sizeof(KeyValue)); int i=0; int j=0; int level=0; char *key; // 处理嵌套字典的键名 while(i<len){ if(json[i]=='{' || json[i]=='['){ level++; i++; } else if(json[i]=='}' || json[i]==']'){ level--; i++; } else if(json[i]==','){ array[j].value[i-array[j].key] = 0; i++; j++; } else if(json[i]==':'){ key = array[j].key; array[j].value[0] = 0; i++; } else if(json[i]=='"' && level%2==0){ i++; int k=0; while(json[i]!='"') { array[j].value[k] = json[i]; i++; k++; } array[j].value[k] = 0; i++; } else if(json[i]!=',' && json[i]!=':' && json[i]!=' '){ array[j].key[i-j] = json[i]; i++; } else { i++; } } array[j].value[i-array[j].key] = 0; j++; char *sorted_json = sort_dict(array, j); char *query_list = malloc(strlen(sorted_json)+1); if(strlen(prefix)>0){ sprintf(query_list, "%s.%s", prefix, sorted_json); } else { strcpy(query_list, sorted_json); } free(array); free(sorted_json); return query_list; } 请对上面的代码添加注释
最新发布
06-10

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值