将amqp_cstring_bytes转换回C字符串

时间:2016-04-05 15:34:30

标签: c rabbitmq amqp rabbitmq-c

C的AMQP消息传递库有一个函数,它接受一个C字符串并将其转换为它自己的字节格式,以便处理amqp_cstring_bytes。是否有这个函数的反函数采用它的字节格式并将其转换回C字符串?

1 个答案:

答案 0 :(得分:2)

您可以使用(char *) <amqp_bytes_t bytes>.bytes或更高级的功能like this(只需将emalloc()替换为malloc(),这与下面的代码中的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; } 一致):

#include <sys/types.h>
#include <sys/wait.h>
#include <stdio.h>
#include <unistd.h>

void childFunction(){
    printf("Child : %d\n", getpid());
    // do stuff
}

int main(){
    int childLimit = 3; // number of children wanted
    int childrenPids[childLimit]; // array to store children's PIDs if needed
    int currentPid, i;

    for(i=0; i<childLimit; i++){
        switch(currentPid = fork()){
            case 0:
                // in the child
                childFunction();
                // exit the child normally and prevent the child
                // from iterating again
                return 0;
            case -1:
                printf("Error when forking\n");
                break;
            default:
                // in the father
                childrenPids[i] = currentPid; // store current child pid
                break;
        }

    }

    printf("Father : %d childs created\n", i);

    // do stuff in the father

    //wait for all child created to die
    waitpid(-1, NULL, 0);
}

另外,请查看rabbitmq-c中的void amqp_dump(void const *buffer, size_t len);函数。