简体   繁体   English

将int数组转换为char *

[英]Converting int array to char*

Is this possible? 这可能吗? I wanted to convert this into a char* so I could later retrieve this values. 我想将其转换为char *,以便稍后检索此值。

Sure: 当然:

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

The valid range is from c to c + sizeof(array) . 有效范围是从cc + sizeof(array) You are allowed to do this to any POD type. 您可以对任何POD类型执行此操作。

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 ). 您正在寻找的过程称为序列化 (并有一个FAQ条目 )。 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. (例如,big-endian平台在发送之前可能总是转换为little-endian,并且在接收时也会转换回big-endian。)您不能将非POD类型视为字节流(例如std::string因此,您需要为这些函数编写序列化函数,将其数据转换为字节流,并使用反序列化函数将其转换回来。

I particularly like that way Boost does this, and if you can I'd use their serialization library . 我特别喜欢Boost这样做的方式,如果可以,我可以使用他们的序列化库 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. 当然,Boost也有他们的ASIO库为你做套接字。

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. 最大的问题是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. 在它们之间切换将导致数字被向后读取。

You can use C-style, but in C++: 您可以使用C风格,但在C ++中:

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

If you want a string, use std::string , not char* . 如果你想要一个字符串,使用std::string ,而不是char* If you want serialization, use <stringstream> . 如果要序列化,请使用<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

This what you're looking to do? 这是你想要做的?

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

char * pChar = (char *)list;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM