【发布时间】:2020-08-06 09:49:20
【问题描述】:
我有 C++ CUDA 项目,但出现错误“标识符“sprintf”在设备代码中未定义”)。 sprintf() 在主机代码中使用,那么如何在 CUDA 内核中将十六进制转换为字符?
C++ 代码:
//md5Hash - its unsigned char hexdecimal array with length = 32
char str[16][2];
for (int j = 0; j < 16; ++j) {
sprintf(str[j], "%02x", md5Hash[j]);//convert by 2 symbols
}
//convert from array str[16][2] to array new_word[32]
char* new_word = (char*) malloc(sizeof(char)*32);
for (int i = 0; i < 16; i++) {
new_word[2 * i] = str[i][0];
new_word[2 * i + 1] = str[i][1];
}
string_to_hex(我需要一个类似的 hex_to_string)
void string_to_hex(unsigned char* output, size_t out_size, char* input, size_t in_size)
{
//example: string_to_hex(md5Hash, 16, "1c0d894f6f6ab511099a568f6e876c2f", 32);
memset(output, '\0', out_size);
for (int i = 0; i < in_size; i += 2)
{
unsigned char msb = (input[i + 0] <= '9' ? input[i + 0] - '0' : (input[i + 0] & 0x5F) - 'A' + 10);
unsigned char lsb = (input[i + 1] <= '9' ? input[i + 1] - '0' : (input[i + 1] & 0x5F) - 'A' + 10);
output[i / 2] = (msb << 4) | lsb;
}
}
数组 md5Hash[32] 和 str[16][2] 中的第一个符号(str 数组中的所有符号将 = "1c0d894f6f6ab511099a568f6e876c2f")。我想将 uchar md5Hash 十六进制数组转换为 char 数组 without sprintf 和其他命名空间 std 函数。我需要从 CUDA device
调用它【问题讨论】:
-
使用
itoa函数和radix等于16,这里示例mkssoftware.com/docs/man3/itoa.3.asp -
@lupaulus 标识符“itoa”在设备代码中未定义
-
@0andriy 我不能包含 kernel.h
-
geeksforgeeks.org/implement-itoa自己的itoa的实现