隐藏压缩十六进制字符串(Base 64编码)

时间:2015-05-08 21:48:43

标签: c++ c string encoding

我有一个Base 64编码(http://www.adp-gmbh.ch/cpp/common/base64.html)字符串,如下所示:

  

KGVuYy12YWwgCiAoZWNkaCAKICAocyAjMDQwMTE3RkFBRDEwQzAwRDAxMENDOTA4NkUwRjVCMEMyN0YzQkM3REY4NENDNjMxQjM5MEFEODJEOERGOUZFNjYxOTU0NkY0NzgyREY1QkRBMjFEOTY3NTkxQzc1QTVDOTc1RUJFRDMyNUI4ODBCNzk0NDdFMTY2NUQ5Q0M0M0MxQSMpCiAgKGUgIzA0MzExNjUyNzE0QkFDRkQzOERFM0NFQTM4NDA4Q0ZBQkVFQTNGRjZGNjIwQkQyQzBBNzU1MTY0MjlBQzJERTRCOTI4OTFDOEZBQ0RDNDEyMjNGMTlGQjc2NjgzQzI4RDc5NkY5Njc4QTU4QzRDMzVDMkJDRUEyMEJEQzYzRURCQTkjKQogICkKICkKAA ==

以下Bluez函数用于压缩此Base 64编码字符串以将其发送到BLE设备:

size_t data_from_string(const char *str, uint8_t **data)
{
    char tmp[3];
    size_t size, i;

    info("data_from_string");

    size = strlen(str) / 2;
    *data = (uint8_t *)malloc(size);
    if (*data == NULL)
        return 0;

    tmp[2] = '\0';
    for (i = 0; i < size; i++) {
        memcpy(tmp, str + (i * 2), 2);
        (*data)[i] = (uint8_t) strtol(tmp, NULL, 16);
    }

    return size;
}

data_from_string函数将Base 64 Encoded字符串压缩为以下格式:

  

0000001200000C0A0000AC0A000A0C0A0000000000000000000000000000000000000000000A00000000000000BC000000000000000000000000000000DD000000000000000000000000000000000000000C00000000000000A400000000000000000000000000000C0A000000A000C100C00000000000000000000000A5000000000000000000000000000000000000000000000000000000000000000000000000000E0000000E00E3000000E20000000D000E0000000000C20000000000000000000E000000000000AA00

我希望在收到此缓冲区后将此字符串转换回Base 64编码字符串格式。我尝试了一些C ++(http://alt.comp.lang.learn.c-cpp.narkive.com/ErJ18iQm/binascii-in-c)binascii :: a2b_hex功能来重新转换缓冲区但无济于事。

有没有人有任何想法如何实现这个目标?

0 个答案:

没有答案