14

How would you convert any struct into byte array on processors with little-endian?

2
  • 2
    expand upon your request please - and note that your answer will be extremely processor architecture dependent Commented Apr 22, 2010 at 15:56
  • What is the addressing unit for little endian? What are you really trying to do? Commented Apr 22, 2010 at 16:10

6 Answers 6

19

You can use a char* to access any type of object in C++, so:

struct S
{
    int a;
    int b;
    // etc.
};

S my_s;

char* my_s_bytes = reinterpret_cast<char*>(&my_s);

// or, if you prefer static_cast:
char* my_s_bytes = static_cast<char*>(static_cast<void*>(&my_s));

(There is at least some debate over the correctness of the reinterpret_cast vs. the static_cast; in practice it doesn't really matter--both should yield the same result)

Sign up to request clarification or add additional context in comments.

4 Comments

:-) too many language improvements now on C++. Whereas before, this would suffice: char* b = (char*)&my_s; Perhaps if i learn C++ again, I'll read something about those cast constructs first.
@Michael: That C-style cast is the same as the reinterpret_cast in this case, because no combination of static_cast and const_cast is valid. Using the C++ style casts guarantees you get the cast you intend; the C-style cast basically just tries a sequence of five different types of casts until it finds one that works.
The answer is right given the struct you specified. However, throw some shorts, large_integers, or character data in there and it is horribly wrong.
@T.E.D.: "horribly wrong" depends entirely on how the char array is to be used. Certainly there are times where this is not the answer, but frequently this is good enough. The requirements were certainly underspecified.
10
(char*)&someStruct

Comments

10

I like to use a union:

typedef struct b {
  unsigned int x;
  unsigned int y;
} b_s;

typedef union a {
  b_s my_struct;
  char ary[sizeof(b_s)];
} a_u;

4 Comments

Why not just ary[sizeof(b)]?
This is the way
@RobKennedy Check this out stackoverflow.com/a/19577625/8628293 , nothing is late :)
Thank you, @Anlgrses, but my question from 13 years prior wasn't "Why a union instead of just a standalone array?" My question was "Why sizeof the typedef instead of simply sizeof the struct type?"
3

What are you trying to do? If you're trying to serialize the struct so you can save it to a file or pass it in a message, you're better off using a tool designed for that like boost::serialization.

If you just want an array of bytes you could reinterpret_cast<char*> as others have mentioned, or do:

MyStruct s;
char [] buffer = new char[sizeof(s)];
memcpy(&buffer, &s, sizeof(s));

Comments

1

I would peer into the void*.

struct gizmo 
{
//w/e
};

//stuff

gizmo *G = new gizmo;

void* bytearray = (void*)G;

How your struct gets packed is ambiguous and depends on compiler, ABI, and CPU. You'll have to figure that out from your manuals & some assembly reading.

2 Comments

You can't access bytes through a void*
Since you cannot dereference a void*, you can't access whatever it points to(perhaps bar using memcpy, in which case a intermediate void* isn't needed) foo[0]; is a compiler error if foo is a void*. Use an unsigned char* to access individual bytes.
0

The problem with all of these answers is that you can't really do dumb byte swapping without knowing something about the data you are swapping. Character data does not get swapped. 64-bit integers need a different kind of swapping depending on exactly how the two processors in question implemented them.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.