1

The AMQP messaging library for C has a function that takes a C string and converts it into it's own byte format for handling amqp_cstring_bytes. Is there an inverse of this function to take it's byte format and convert it back into a C string?

pinepain
  • 12,453
  • 3
  • 60
  • 65
Shiri
  • 1,972
  • 7
  • 24
  • 46

1 Answers1

2

You can use (char *) <amqp_bytes_t bytes>.bytes or more advanced function like this (just replace emalloc() which is php-specific with malloc() like in code below):

char *stringify_bytes(amqp_bytes_t bytes)
{
/* We will need up to 4 chars per byte, plus the terminating 0 */
    char *res = malloc(bytes.len * 4 + 1);
    uint8_t *data = bytes.bytes;
    char *p = res;
    size_t i;

    for (i = 0; i < bytes.len; i++) {
        if (data[i] >= 32 && data[i] != 127) {
            *p++ = data[i];
        } else {
            *p++ = '\\';
            *p++ = '0' + (data[i] >> 6);
            *p++ = '0' + (data[i] >> 3 & 0x7);
            *p++ = '0' + (data[i] & 0x7);
        }
    }

    *p = 0;
    return res;
}

Also, give a look to void amqp_dump(void const *buffer, size_t len); function from rabbitmq-c.

pinepain
  • 12,453
  • 3
  • 60
  • 65