【发布时间】:2014-03-07 17:34:20
【问题描述】:
我有一个 unsigned char buffer[2850] 类型的大型缓冲区数组,我想将其转换为 Base64 字符串。我正在尝试使用可以在 on github 找到的 libb64 库
这是我尝试转换的方式:
char* encode(const char* input)
{
/* set up a destination buffer large enough to hold the encoded data */
char* output = (char*)malloc(SIZE);
/* keep track of our encoded position */
char* c = output;
/* store the number of bytes encoded by a single call */
int cnt = 0;
/* we need an encoder state */
base64_encodestate s;
/*---------- START ENCODING ----------*/
/* initialise the encoder state */
base64_init_encodestate(&s);
/* gather data from the input and send it to the output */
cnt = base64_encode_block(input, strlen(input), c, &s);
c += cnt;
/* since we have encoded the entire input string, we know that
there is no more input data; finalise the encoding */
cnt = base64_encode_blockend(c, &s);
c += cnt;
/*---------- STOP ENCODING ----------*/
/* we want to print the encoded data, so null-terminate it: */
*c = 0;
return output;
}
char *encodedBuffer;
encodedBuffer = encode(buffer);
但我收到了警告
Passing 'unsigned char [2850]' to parameter of type 'const char *' converts between pointers to integer types with different sign.
有没有更好的方法来转换它,或者有什么我需要改变的,因为我试图传入一个无符号字符数组。谢谢!
【问题讨论】:
-
encodedBuffer = encode((const char*)buffer);? -
@JoachimIsaksson 我真的是 C 新手,在缓冲区前面添加 (const char*) 会动态改变类型吗?
-
unsigned char *buffer[2850]不是无符号字符数组;它是一个包含 2850 个指针的数组。如果那不是您的真实代码,请发布它。 -
它只是告诉编译器在这种情况下可以将
unsigned char[2850]处理为const char*。基本上它告诉编译器“我知道我在做什么,所以不要打扰我”,所以如果你是一个初学者,你应该知道像这样将任何类型转换为任何其他类型并不总是安全的。 -
感谢您的解释。