5

Is this possible? I wanted to convert this into a char* so I could later retrieve this values.

16
  • Can you clarify what you're looking for? Do you want an array of strings with your numbers in them? Do you want to access your int's one byte at a time? Commented Apr 22, 2010 at 3:35
  • Pretty much anything, as long as I can retrieve all the ints later. Commented Apr 22, 2010 at 3:36
  • Are you trying to preserve data after a function's return, by any chance? Commented Apr 22, 2010 at 3:40
  • Well, I was actually send this over the network as a packet. Commented Apr 22, 2010 at 3:41
  • 1
    @seed: No, it could mean interpreting the array as a char* or converting the numbers to some string. Commented Apr 22, 2010 at 3:50

5 Answers 5

8

Sure:

int array[4] = {1, 2, 3, 4};
char* c = reinterpret_cast<char*>(array);

The valid range is from c to c + sizeof(array). You are allowed to do this to any POD type.

You can cast back from a sequence of bytes:

// assuming c above
int (&pArray)[4] = *reinterpret_cast<int(*)[4]>(c);

This is guaranteed to work. But, it seems you're trying to send stuff across a network, which can introduce other problems


The process you're looking for is called serialization (and has a FAQ entry). This is when you take an object, transform it into a series of bits, which can later be "deserialized" into the original object.

Making this work across multiple platforms can be tricky, because you need to make sure you serialize into a specific format, and that each platform knows how it should read from that format. (For example, a big-endian platform might always convert to little-endian before sending, and likewise convert back to big-endian when receiving.) You cannot treat non-POD types as a stream of bytes (such as std::string), so you need to write serialization functions for those, to transform their data into a stream of bytes, and deserialization functions to transform it back.

I particularly like that way Boost does this, and if you can I'd use their serialization library. They basically first define routines for serializing fundamental types, then you can serialize more complex types by building off that. Of course, Boost also has their ASIO library to do sockets for you.

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

13 Comments

Ideally the last element in the array would be a zero in case someone down the line passes the char* to a str* function
That intention isn't explicitly stated in the question though.
This seemed to have worked, but just wondering- how do I get the actual data from it? For example, if I printed this out, it actually gave me a smiley face symbol (=D)- I'm guessing that's what 1234 represents or something. Can I actually disassembles this back into 1,2,3,4?
@seed: if it prints a smiley, that means that the last byte of the first int (assuming little-endian) equals the DOS character value for the smiley.
@Frustrated: Haha, it totally does print a smiley. I'm badass and clearly planned this out.
|
3

Yes, but you probably shouldn't.

Doing so will treat the ints as sequences of bytes. It is tempting to then pass these bytes to be written to files or across sockets. The problem is that that the result will not be portable. There is no guarantee that whatever computer reads those bytes will interpret them in the same way. The biggest issue is big-endian vs little-endian. Essentially, some computers put the most significant byte first whereas others put the least significant byte first. Switching between them will result in the number being read backwards.

1 Comment

Ah yes, I read about this.... while I guess it is a very bad idea, I'll just try it, it's more of an experiment than something that'll go on the market ;)
0

You can use C-style, but in C++:

int arr[] = {0, 1, 2, 3, 4};
char* p_arr = reinterpret_cast<char*>(arr);

Comments

0

If you want a string, use std::string, not char*. If you want serialization, use <stringstream>.

#include <stringstream>

std::ostringstream packet_os;
for ( int i = 0; i < 5; ++ i ) packet_os << arr[ i ] << " ";
network_send( packet_os.str().c_str() ); // c_str() returns char*

On the other end:

network_receive( recv_buf );
std::istringstream packet_is( recv_buf->bytes );
for ( int i = 0; i < 5; ++ i ) packet_is >> arr[ i ];
assert ( packet_is ); // for debugging, check that everything was received OK

7 Comments

@seed: sorry, I was writing a code comment as you wrote your question comment.
Okay, I see. (Not enough chars so I'll put this in).
@seed: Especially considering "The params for a function only accept char*", this would seem to completely answer your question. Is that not the case?
@potatoswatter- Yeah, but I think I just need a way of turning it back.
@potatoswatter- I'm guessing network_send is pseudo function?
|
-1

This what you're looking to do?

int list[5] = {1,2,3,4,5};

char * pChar = (char *)list;

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.