d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > C - Converting Elements Of A Struct Into A String?
Add Reply New Topic New Poll
Member
Posts: 24,072
Joined: Nov 8 2007
Gold: 2,065.70
Oct 23 2014 07:13pm
So I have a struct that is a header for some message consisting of 20 bytes

Code
struct msgHeader{
uint8_t version;
uint8_t ttl;
uint16_t payload_length;
.....
}


I'm trying to convert the elements of the struct into a string representation of those elements.

Assume the struct is filled with some values, for example.

Code
struct msgHeader *temp;
temp = malloc(sizeof(msgHeader);
temp->version = 1;
temp->ttl = 2;
temp->payload = 13;
....
}


I'm trying to get a string of the elements of the struct - 1213....

I've been trying to use memcpy but it doesn't seem to want to work

Code
char buffer[1024] // message will be concat'd to the end of the header contained by buffer
memcpy(buffer, &temp, sizeof(temp));


Is this possible?

This post was edited by lopelurag on Oct 23 2014 07:26pm
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Oct 23 2014 07:27pm
Member
Posts: 24,072
Joined: Nov 8 2007
Gold: 2,065.70
Oct 24 2014 07:21am
Quote (Eep @ Oct 23 2014 09:27pm)


Yea was trying to avoid sprintf as it's just more code
Member
Posts: 24,072
Joined: Nov 8 2007
Gold: 2,065.70
Oct 24 2014 11:38am
How would you even use sprintf for uint8_t, uint16_t ect..
Member
Posts: 62,204
Joined: Jun 3 2007
Gold: 9,039.20
Oct 24 2014 12:18pm
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Oct 24 2014 12:20pm
Quote (lopelurag @ Oct 24 2014 01:38pm)
How would you even use sprintf for uint8_t, uint16_t ect..


%u works for any uintX_t types or at least uint8_t.

You can also simply assign it to a unsigned char array.

Code
unsigned char packet[] = {temp->version, temp->ttl, temp->payload };


This may or may not work in your circumstance since the data in the packet had to be added before run time so the compiler can allocate enough space. However you can malloc space for it and assign the data and it would still work.

Alternatively you can specify a packet[x] length and it would work anyways.

Edit:: also FYI, sizeof(*pointer) is always 4 or 8 based on the architecture of your application. You needed to use sizeof() on your struct definition. Also you want to turn it into a byte array, so it should be an unsigned char array.

This post was edited by AbDuCt on Oct 24 2014 12:28pm
Member
Posts: 24,072
Joined: Nov 8 2007
Gold: 2,065.70
Oct 24 2014 02:32pm
Quote (AbDuCt @ Oct 24 2014 02:20pm)
%u works for any uintX_t types or at least uint8_t.

You can also simply assign it to a unsigned char array.

Code
unsigned char packet[] = {temp->version, temp->ttl, temp->payload };


This may or may not work in your circumstance since the data in the packet had to be added before run time so the compiler can allocate enough space. However you can malloc space for it and assign the data and it would still work.

Alternatively you can specify a packet[x] length and it would work anyways.

Edit:: also FYI, sizeof(*pointer) is always 4 or 8 based on the architecture of your application. You needed to use sizeof() on your struct definition. Also you want to turn it into a byte array, so it should be an unsigned char array.


Yea thanks, my original code was correct just made a mistake when rewriting it here.

Trying sprintf atm, I assume it would be something like this.

Code

char buffer[1024]; // contains msgHeader + msg
struct msgHeader *node;
node = malloc(sizeof(msgHeader));
sprintf(node->version, "%u", buffer));
sprintf(node->ttl, "%u", buffer + sizeof(byte));
ect...
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Oct 24 2014 03:38pm
Replied to his pm, but here is the solution I provided in case anyone wants to add to it or suggest another:

The premise of this is that the byte array of the msgheader will be in the same order/layout as the structure itself, so you can simply overlay the byte array ontop of the header and everything will fall into place.



Code
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>

int main() {

struct msgHeader {
uint8_t version;
uint8_t ttl;
uint16_t payload_length;
};

struct msgHeader *temp = malloc(sizeof(struct msgHeader));

temp->version = 1;
temp->ttl = 2;
temp->payload_length = 13;

char *buffer = malloc(sizeof(struct msgHeader));

memcpy(buffer, temp, sizeof(struct msgHeader));

struct msgHeader *newHeader = (struct msgHeader*)buffer;

return 0;
}


Edit:: also your memcpy() function was incorrect. The temp variable should not have the & sign before it as memcpy() expects a pointer to the source.

This post was edited by AbDuCt on Oct 24 2014 03:41pm
Member
Posts: 24,072
Joined: Nov 8 2007
Gold: 2,065.70
Oct 24 2014 07:15pm
Thanks for all the help! Found out most of my problems came from using sizeof some pointer value
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll