C++ |将 int 转换为 byte[4],反之亦然
C++ | Convert int to byte[4] and vice versa
我正在尝试将整数转换为字节(又名无符号字符)数组,以通过 C++ 中的 TCP 流发送数组,反之亦然。
我在 Whosebug 上尝试了很多解决方案和自己的想法,但似乎没有什么对我有用。
我最后的解决方案是这样的:
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <iostream>
#include "tcpconnector.h"
typedef unsigned char byte;
using namespace std;
/*
char* byteToChar(byte* b, int length) {
char c[length];
for (int i = 0; i < length; i++) {
c[i] = b[i] - 128;
}
return c;
}
byte* charToByte(char* c, int length) {
byte b[length];
for (int i = 0; i < length; i++) {
b[i] = c[i] + 128;
}
return b;
}
*/
byte* intToByte(int n) {
byte byte[4];
byte[0] = n & 0x000000ff;
byte[1] = n & 0x0000ff00 >> 8;
byte[2] = n & 0x00ff0000 >> 16;
byte[3] = n & 0xff000000 >> 24;
return byte;
}
int byteToInt(byte* byte) {
int n = 0;
n = n + (byte[0] & 0x000000ff);
n = n + ((byte[1] & 0x000000ff) << 8);
n = n + ((byte[2] & 0x000000ff) << 16);
n = n + ((byte[3] & 0x000000ff) << 24);
return n;
}
int main(int argc, char** argv)
{
if (argc != 3) {
printf("usage: %s <port> <ip>\n", argv[0]);
exit(1);
}
int number = 42;
byte* line = intToByte(number);
cout << "Number: " << number << "\n";
cout << "ArrayLength: " << sizeof line << "\n";
cout << "Array: " << line << "\n";
cout << "Array to Number: " << byteToInt(line) << "\n";
/*
TCPConnector* connector = new TCPConnector();
TCPStream* stream = connector->connect(argv[2], atoi(argv[1]));
if (stream) {
stream->send(byteToChar(line, 4), 4);
delete stream;
}
*/
exit(0);
}
每次我执行此代码时,无论我设置什么 "int number",我都会得到结果“4202308”。
如有任何帮助,我们将不胜感激。
更新:
void intToByte(int n, byte* result) {
result[0] = n & 0x000000ff;
result[1] = n & 0x0000ff00 >> 8;
result[2] = n & 0x00ff0000 >> 16;
result[3] = n & 0xff000000 >> 24;
}
摘自 main():
int number = 42;
byte line[4];
intToByte(number, line);
cout << "Number: " << number << "\n";
cout << "ArrayLength: " << sizeof line << "\n";
cout << "Array: " << line << "\n";
cout << "Array to Number: " << byteToInt(line) << "\n";
您的 intToByte
函数正在其主体范围内分配一个 byte[4]
,然后 return 指向它的指针。
因此,一旦您的函数 returns 和所有调用者收到的是指向现在无效位置的指针,该值就会被丢弃 - 当值超出范围时会被销毁,而指针不会延长寿命。
使用标准容器对象,例如 std::array
或 std::vector
,您的函数应该 return 给调用者,或者让 intToByte
接受 byte[4]
/byte*
作为参数并填写.
为了完整性...您也可以让函数使用 new
创建字节数组,但是您必须记住 delete[]
它,虽然这看起来很容易在这种情况下,当您没有充分理由进行动态分配时,通常是不好的做法。
此外,语句x & y >> z
会先执行y >> z
然后然后与x
的结果位与,这当然是不是你想要的。
遗憾的是,这不是您问题的准确答案,但可能会有所帮助。我想你可以像下面的代码那样做。
#include <stdio.h>
using namespace std;
unsigned char* intToByte(const int& N) {
unsigned char* byte = new unsigned char[4];
byte[0] = (N >> 24) & 0xFF;
byte[1] = (N >> 16) & 0xFF;
byte[2] = (N >> 8) & 0xFF;
byte[3] = N & 0xFF;
return byte;
}
int main()
{
unsigned char * result = intToByte(255);
printf("%x %x %x %x\n", (unsigned char)result[0], (unsigned char)result[1],
(unsigned char)result[2], (unsigned char)result[3]);
delete result;
return 0;
}
第一点:
您从 byte* intToByte(int n)
return 获取的缓冲区不安全。
您正在 return 获取本地数组的基地址。将字节数组作为输入传递或在堆中分配字节和 return 地址
第二点:
考虑运算符优先级。
byte byte[4];
byte[0] = n & 0x000000ff;
byte[1] = ( n & 0x0000ff00 ) >> 8;
byte[2] = ( n & 0x00ff0000 ) >> 16;
byte[3] = ( n & 0xff000000 ) >> 24;
此函数将所有 C++ 中的标准类型转换为字节向量。
template <typename T>
std::vector<byte> ToByte(T input)
{
byte* bytePointer = reinterpret_cast<byte*>(&input);
return std::vector<byte>(bytePointer, bytePointer + sizeof(T));
}
我正在尝试将整数转换为字节(又名无符号字符)数组,以通过 C++ 中的 TCP 流发送数组,反之亦然。
我在 Whosebug 上尝试了很多解决方案和自己的想法,但似乎没有什么对我有用。
我最后的解决方案是这样的:
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <iostream>
#include "tcpconnector.h"
typedef unsigned char byte;
using namespace std;
/*
char* byteToChar(byte* b, int length) {
char c[length];
for (int i = 0; i < length; i++) {
c[i] = b[i] - 128;
}
return c;
}
byte* charToByte(char* c, int length) {
byte b[length];
for (int i = 0; i < length; i++) {
b[i] = c[i] + 128;
}
return b;
}
*/
byte* intToByte(int n) {
byte byte[4];
byte[0] = n & 0x000000ff;
byte[1] = n & 0x0000ff00 >> 8;
byte[2] = n & 0x00ff0000 >> 16;
byte[3] = n & 0xff000000 >> 24;
return byte;
}
int byteToInt(byte* byte) {
int n = 0;
n = n + (byte[0] & 0x000000ff);
n = n + ((byte[1] & 0x000000ff) << 8);
n = n + ((byte[2] & 0x000000ff) << 16);
n = n + ((byte[3] & 0x000000ff) << 24);
return n;
}
int main(int argc, char** argv)
{
if (argc != 3) {
printf("usage: %s <port> <ip>\n", argv[0]);
exit(1);
}
int number = 42;
byte* line = intToByte(number);
cout << "Number: " << number << "\n";
cout << "ArrayLength: " << sizeof line << "\n";
cout << "Array: " << line << "\n";
cout << "Array to Number: " << byteToInt(line) << "\n";
/*
TCPConnector* connector = new TCPConnector();
TCPStream* stream = connector->connect(argv[2], atoi(argv[1]));
if (stream) {
stream->send(byteToChar(line, 4), 4);
delete stream;
}
*/
exit(0);
}
每次我执行此代码时,无论我设置什么 "int number",我都会得到结果“4202308”。
如有任何帮助,我们将不胜感激。
更新:
void intToByte(int n, byte* result) {
result[0] = n & 0x000000ff;
result[1] = n & 0x0000ff00 >> 8;
result[2] = n & 0x00ff0000 >> 16;
result[3] = n & 0xff000000 >> 24;
}
摘自 main():
int number = 42;
byte line[4];
intToByte(number, line);
cout << "Number: " << number << "\n";
cout << "ArrayLength: " << sizeof line << "\n";
cout << "Array: " << line << "\n";
cout << "Array to Number: " << byteToInt(line) << "\n";
您的 intToByte
函数正在其主体范围内分配一个 byte[4]
,然后 return 指向它的指针。
因此,一旦您的函数 returns 和所有调用者收到的是指向现在无效位置的指针,该值就会被丢弃 - 当值超出范围时会被销毁,而指针不会延长寿命。
使用标准容器对象,例如 std::array
或 std::vector
,您的函数应该 return 给调用者,或者让 intToByte
接受 byte[4]
/byte*
作为参数并填写.
为了完整性...您也可以让函数使用 new
创建字节数组,但是您必须记住 delete[]
它,虽然这看起来很容易在这种情况下,当您没有充分理由进行动态分配时,通常是不好的做法。
此外,语句x & y >> z
会先执行y >> z
然后然后与x
的结果位与,这当然是不是你想要的。
遗憾的是,这不是您问题的准确答案,但可能会有所帮助。我想你可以像下面的代码那样做。
#include <stdio.h>
using namespace std;
unsigned char* intToByte(const int& N) {
unsigned char* byte = new unsigned char[4];
byte[0] = (N >> 24) & 0xFF;
byte[1] = (N >> 16) & 0xFF;
byte[2] = (N >> 8) & 0xFF;
byte[3] = N & 0xFF;
return byte;
}
int main()
{
unsigned char * result = intToByte(255);
printf("%x %x %x %x\n", (unsigned char)result[0], (unsigned char)result[1],
(unsigned char)result[2], (unsigned char)result[3]);
delete result;
return 0;
}
第一点:
您从 byte* intToByte(int n)
return 获取的缓冲区不安全。
您正在 return 获取本地数组的基地址。将字节数组作为输入传递或在堆中分配字节和 return 地址
第二点:
考虑运算符优先级。
byte byte[4];
byte[0] = n & 0x000000ff;
byte[1] = ( n & 0x0000ff00 ) >> 8;
byte[2] = ( n & 0x00ff0000 ) >> 16;
byte[3] = ( n & 0xff000000 ) >> 24;
此函数将所有 C++ 中的标准类型转换为字节向量。
template <typename T>
std::vector<byte> ToByte(T input)
{
byte* bytePointer = reinterpret_cast<byte*>(&input);
return std::vector<byte>(bytePointer, bytePointer + sizeof(T));
}