2

I am a little bit confused on how to convert a struct to a char[] in C.

My CDMA modem doesn't support sending variables - it only understands ASCII characters. I need to do the conversion operation.

Let's say that I have an sMSG struct like this:

struct sMSG
{
    int a;
    int b[];
    char c[];
    double d;
    float f;
};

So, I have to make a string like char str[] = "sMSG_converted_into_ASCII_chars";

I'm wondering if somebody will help me out on this, please.

Thanks in advance.

embedded_guy
  • 1,939
  • 3
  • 24
  • 39
  • Do they have to be `ASCII` or can they be `0-255` ? Also, use the real definition of your struct. – cnicutar Apr 03 '12 at 09:15
  • Okay, say the byte `240` appears somewhere in your structure. How will this be sent to the modem ? – cnicutar Apr 03 '12 at 09:19
  • 1
    Are the sender and receiver different kinds of machines (endian-ness, word size)? That is of paramount importance. – ArjunShankar Apr 03 '12 at 10:33
  • cnicular, cdma modem works like this: to send 1234-> AT$TCPWRITE=31323334 so, instead 1234, i should have a structure. –  Apr 03 '12 at 10:38
  • This still doesn't answer the question 'who sends data to whom?'. What are the machines involved? What is the mode of communication? – ArjunShankar Apr 03 '12 at 12:58
  • cdma modem(3g-CC864 module) sends data to server, mode of communication is wireless. –  Apr 04 '12 at 04:13

1 Answers1

5

First you need to copy the data of the struct into a byte array

int len = sizeof(struct sMSG);
unsigned char * raw = malloc(len);
memcpy(raw, &msg, len);

Now use a function to convert the byte array into Base64 text or just hexadecimal representation (2 chars/byte). Since you use the embedded tag, the latter might be the easiest to implement.

#define TOHEX(x) (x > 9 ? (x - 10 + 'A') : (x + '0'));
char * text = malloc(2 * len + 1);
for (int i = 0; i < len; i++)
{
    text[2 * i + 0] = TOHEX(raw[i] >> 4);
    text[2 * i + 1] = TOHEX(raw[i] & 0xF);
}
text[2 * len] = '\0';

free(raw);
free(text);
unwind
  • 391,730
  • 64
  • 469
  • 606
huysentruitw
  • 27,376
  • 9
  • 90
  • 133
  • I was gonna say `sprintf` it into a large enough array, but this solution is better! The `sprintf` solution has a problem handling field separators. – Shahbaz Apr 03 '12 at 09:22
  • will the data those arrays point to be copied using `memcpy` this way (or will rather the pointers be copied)? – moooeeeep Apr 03 '12 at 09:54
  • 3
    whether this 'just works' really depends on who 'sends' messages to whom, what the sizes of different data types are, endian-ness, etc etc. You can't just read this on a different machine and 'memcpy' it back into the right structure. – ArjunShankar Apr 03 '12 at 10:28
  • @ArjunShankar: that's correct, especially floats and double could even raise more trouble (if both platforms use a different notation) – huysentruitw Apr 03 '12 at 11:59
  • This assumes (BIG ASSUMPTION) that there are no empty bytes in the data structure that you are memcpy'ing. If the data elements are not assigned to a single contiguous set of memory locations then you will have extra padding bytes in the transmitted data. The values in these locations are undefined and could have undesirable effects. – uɐɪ Apr 03 '12 at 13:36