Home > Net >  How can I parse these binary datas from memory?
How can I parse these binary datas from memory?

Time:03-29

#include <string.h>

struct Market {
    char ticker[16];
    char price[5];
};


void parser(const void **ptr, struct Market arr) {
    char ticker[16];
    memcpy(arr.price, *ptr, 5);
    ptr = ptr   4;
    int j = 0;
    while (1) {
        if (*ptr) {
            ticker[j] = *ptr;
            j  ;
            ptr  ;
        } else {
            break;
        }
    }
    strcpy(arr.ticker, ticker);
}


struct Market b;

int main(void) {
    char a[20] = {'\xe7', 'h', '7', 'G', '\0','B', 'T', 'C', 'U', 'S', 'D', 'T', '\0'};
    parser(a, b);
    printf("  %s", b.ticker);
}

How can I parse these binary datas from memory? I'm very novice in c.. Above codes can't compile. It has so many error. I can't use fread, because it's from memory. And Data that i have to parse is like..

b':\x197GBTCUSDT\x00q\x0fOEETHUSDT\x00\x10\xea\xbbCBCHUSDT\x00\x9b\xe6]?XRPUSDT\x00\x92\xb3C@EOSUSDT\x00\xcfP\x03CLTCUSDT\x00\xc2Y\x8e=TRXUSDT\x00Qp?BETCUSDT\x00\n\x95\x87ALINKUSDT\x00\x16\x19n>XLMUSDT\x00\x83\xdd\x97?ADAUSDT\x00\x1d\x82XCXMRUSDT\x00\xa4\xf0\x03CDASHUSDT\x003sLCZECUSDT\x00\xd1={@XTZUSDT\x00\x81\xb6\xd8CBNBUSDT\x00Xk\xf2AATOMUSDT\x00D\xd2%?ONTUSDT\x00Y\x1de?IOTAUSDT\x00\xf5Ji?BATUSDT\x00t\x..

How can I solve this difficult job?

CodePudding user response:

If something hasn't changed C standard has defined that the implementation shall define char to have the same range, representation, and behavior as either signed char or unsigned char . \xe7 has a decimal value 237 which is more than singed char can hold. Can this work? Another thing, passing a (char*) to ptr (void**), how can that work? Or, I a misunderstand something?

  • Related