我有一个C++的CUDA项目,我有一个错误 "标识符 "sprintf "is undefined in device code")。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数组。无冲刺f 和其他命名空间的std函数。我需要从CUDA中调用它 装置
看来,你正好需要两件事来实现你的需求。
第一步可以这样做。
__device__ __host__ void makedigits(unsigned char x, unsigned char (&digits)[2])
{
unsigned char d0 = x / 16;
digits[1] = x - d0 * 16;
unsigned char d1 = d0 / 16;
digits[0] = d0 - d1 * 16;
}
这将返回最重要的16位基数数字 digits[0]
和最小的数字 digits[1]
.
第二步可以通过对两个16位基数的偏移量进行简单的添加,将其转化为正确的ASCII值。
__device__ __host__ void makehex(unsigned char (&digits)[2], char (&hex)[2])
{
for(int i=0; i<2; ++i) {
if (digits[i] < 10) {
hex[i] = '0' + digits[i];
} else {
hex[i] = 'a' + (digits[i] - 10);
}
}
}
一个完整的例子是这样的:
#include <iostream>
#include <cstdio>
__device__ __host__ void makedigits(unsigned char x, unsigned char (&digits)[2])
{
unsigned char d0 = x / 16;
digits[1] = x - d0 * 16;
unsigned char d1 = d0 / 16;
digits[0] = d0 - d1 * 16;
}
__device__ __host__ void makehex(unsigned char (&digits)[2], char (&hex)[2])
{
for(int i=0; i<2; ++i) {
if (digits[i] < 10) {
hex[i] = '0' + digits[i];
} else {
hex[i] = 'a' + (digits[i] - 10);
}
}
}
__global__ void kernel(unsigned char* input, char* output)
{
for (int i = 0; i < 16; ++i) {
unsigned char val = input[i];
unsigned char d[2];
char h[2];
makedigits(val, d);
makehex(d, h);
output[2*i] = h[0];
output[2*i+1] = h[1];
}
}
int main()
{
unsigned char md5Hash[32];
for(int i=0; i<32; ++i) md5Hash[i] = 255-i;
unsigned char* d_md5Hash;
cudaMalloc(&d_md5Hash, 32 * sizeof(unsigned char));
cudaMemcpy(d_md5Hash, &md5Hash[0], 32 * sizeof(unsigned char),
cudaMemcpyHostToDevice);
char str[16][2];
for (int j = 0; j < 16; ++j) {
sprintf(str[j], "%02x", md5Hash[j]);//convert by 2 symbols
}
for(int i=0; i<16; i++) std::cout << str[i][0] << str[i][1];
std::cout << std::endl;
char* d_str1;
cudaMalloc(&d_str1, 32 * sizeof(char));
char str1[32];
kernel<<<1,1>>>(d_md5Hash, d_str1);
cudaMemcpy(&str1[0], d_str1, 32 * sizeof(char), cudaMemcpyDeviceToHost);
for(int i=0; i<32; i+=2) std::cout << str1[i] << str1[i+1];
std::cout << std::endl;
return 0;
}
[注意,所有代码都是在喝咖啡的时候写的,而且是极少的测试。使用时风险自担]
编译并运行这个。
$ nvcc -o hexconv hexconv.cu
$ ./hexconv
fffefdfcfbfaf9f8f7f6f5f4f3f2f1f0
fffefdfcfbfaf9f8f7f6f5f4f3f2f1f0
这似乎会产生与 sprintf
基于你问题中的主机代码。