c - 如何将 unsigned char 数组转换为 base64 字符串?

标签 c arrays string base64

我有一个类型为 unsigned char buffer[2850] 的大型缓冲区数组,我想将其转换为 Base64 字符串。我正在尝试使用可以找到的 libb64 库 on github

这是我尝试转换它的方式:

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.

有没有更好的方法来转换它,或者因为我试图传入一个无符号字符数组,所以我需要改变什么。谢谢!

最佳答案

您的缓冲区是 char** 类型。您需要传递 char* 类型之一。

你可以使用

const char *buffer = "whatever";
encode(buffer);

关于c - 如何将 unsigned char 数组转换为 base64 字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22256872/

相关文章:

python - 字符串 block 格式化挑战

php - zend引擎调用父方法

c - IOCTL 调用适用于某些架构

c - 在函数中初始化数组

c - 为什么在这个程序中 strcpy() 函数会产生不需要的输出?

string - 如何对字符串列表进行批量排序?

外部全局变量的 C 优化

c、创建 child 和wait()

C++ 将字节从 char* 传递到 BYTE*

Java GUI - 从数组中获取随机值