array<Byte>^ unsigned char* char* 之间的转换

6 篇文章 0 订阅

问题来源:C++调用C#的dll时候,需要响应的类型转换

array<unsigned char>^ GetBYTECLI(unsigned char *pszName,int nStrLen) 
{  
	array<unsigned char>^ bs = gcnew array<unsigned char>(nStrLen); 
	for(int i = 0;i<nStrLen;i++)
	{
		bs[i] = pszName[i];
	}
	return bs; 
} 
unsigned char* GetBYTE(array<unsigned char>^ pszName) 
{  
	unsigned char *b2 = new unsigned char(pszName->Length);
	//pin_ptr<unsigned char> pb1 = &pszName[0];
	for(int i=0;i<pszName->Length;i++)
	{
		//b2[i] = *(pb1+i);
		b2[i] = pszName[i];
	}
	//memcpy(b2,pb1,pszName->Length);
	return b2;
}

unsigned char *pszName = (unsigned char*)charArray;
unsigned char *b2 = new unsigned char(pszName->Length);
参考:http://stackoverflow.com/questions/15078638/can-i-turn-unsigned-char-into-char-and-vice-versa


#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、付费专栏及课程。

余额充值