0

I have a Base 64 Encoded (http://www.adp-gmbh.ch/cpp/common/base64.html) string as follows:

KGVuYy12YWwgCiAoZWNkaCAKICAocyAjMDQwMTE3RkFBRDEwQzAwRDAxMENDOTA4NkUwRjVCMEMyN0YzQkM3REY4NENDNjMxQjM5MEFEODJEOERGOUZFNjYxOTU0NkY0NzgyREY1QkRBMjFEOTY3NTkxQzc1QTVDOTc1RUJFRDMyNUI4ODBCNzk0NDdFMTY2NUQ5Q0M0M0MxQSMpCiAgKGUgIzA0MzExNjUyNzE0QkFDRkQzOERFM0NFQTM4NDA4Q0ZBQkVFQTNGRjZGNjIwQkQyQzBBNzU1MTY0MjlBQzJERTRCOTI4OTFDOEZBQ0RDNDEyMjNGMTlGQjc2NjgzQzI4RDc5NkY5Njc4QTU4QzRDMzVDMkJDRUEyMEJEQzYzRURCQTkjKQogICkKICkKAA==

The following Bluez function is used to compress this Base 64 Encoded string to send it to a BLE device:

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;
}

The data_from_string function compress the Base 64 Encoded string into the following format:

0000001200000C0A0000AC0A000A0C0A0000000000000000000000000000000000000000000A00000000000000BC000000000000000000000000000000DD000000000000000000000000000000000000000C00000000000000A400000000000000000000000000000C0A000000A000C100C00000000000000000000000A5000000000000000000000000000000000000000000000000000000000000000000000000000E0000000E00E3000000E20000000D000E0000000000C20000000000000000000E000000000000AA00

I want to convert this string back into the Base 64 Encoded string format upon receipt of this buffer. I have tried some C++ (http://alt.comp.lang.learn.c-cpp.narkive.com/ErJ18iQm/binascii-in-c) binascii::a2b_hex functionality to re-convert the buffer but to no avail.

Does anyone have any ideas how to accomplish this objective?

Ed Johns
  • 211
  • 3
  • 11
  • 1
    Your data_from_string function converts string consisting of hexadecimal representation (e.g. "FF12...") into byte array and is not suitable for general text like a base64 string, it just corrupts data and is not recoverable. – j.holetzeck May 08 '15 at 22:11
  • I agree with @j.holetzeck, this function isn't even close to doing the right thing. Base64 is supposed to take 4 characters, each containing 6 bits of data making 24 bits total, and unpack it into 3 bytes. So right off the bat, by making `size` 1/2 instead of 3/4 of the input size, you're messing up. – Mark Ransom May 08 '15 at 22:16
  • I'm stuck with the function data_from_string since it's part of the Bluez library. I need to encode my data. I now see that Base 64 Encoding is the wrong approach. Is there a more suitable encoding such as UTF-8? Any suggestions would be appreciated. – Ed Johns May 09 '15 at 02:08
  • 1
    You want to send data from which program/platform to which program/platform? Maybe you need UTF-8 or maybe you need base64 – Barmak Shemirani May 09 '15 at 03:11
  • What exactly is expected by your _transport layer_? E.g. you have some data on one side (e.g. text or binary) and you want to transport to the other side. In between you need the encoding expected by your transport protocol. What is it (base64, binary, xml, fancy other format, ...)? – j.holetzeck May 09 '15 at 10:42
  • I did create a new questions that more represents the problem I want to solve at URL: http://stackoverflow.com/questions/30143148/decode-array-unit8-t-array-to-char-array. I plan to close this question. – Ed Johns May 09 '15 at 17:51

0 Answers0