将 char* 转换为 int [英] Converting a char* into an int
问题描述
我目前正在尝试将整数转换为 char* 以便通过套接字发送它.在接收方法中,我在逻辑上尝试再次将 char* 视为整数,但我似乎遗漏了一些东西,因为我无法正确处理.
I'm currently trying to convert an integer into a char* in order to send it over a socket. In the receiving method I logically try to treat the char* as an integer again, but I seem to be missing something because I can't get it right.
#include <iostream>
using namespace std;
int main(int argc, char** argv) {
int num1 = 42; // Works
int num2 = 100; // Works
int num3 = 126; // Works
int num4 = 517; // Doesn't seem to work for int > 127
char p1[sizeof(int)];
*p1 = num1;
char p2[sizeof(int)];
*p2 = num2;
char p3[sizeof(int)];
*p3 = num3;
char p4[sizeof(int)];
*p4 = num4;
void* pA = p4;
void* pB = &num4;
int result1 = static_cast<int>(*p1);
int result2 = static_cast<int>(*p2);
int result3 = static_cast<int>(*p3);
int result4 = static_cast<int>(*p4);
int resultV1 = *static_cast<int*>(pA);
int resultV2 = *reinterpret_cast<int*>(p3);
unsigned int resultV3 = static_cast<int>(*p4);
int resultV4 = *static_cast<int*>(pB); // Works, but I need to convert a char* into an int, not a void* into an int
cout << "R1: " << result1 << endl;
cout << "Expected: " << num1 << endl << endl;
cout << "R2: " << result2 << endl;
cout << "Expected: " << num2 << endl << endl;
cout << "R3: " << result3 << endl;
cout << "Expected: " << num3 << endl << endl;
cout << "R4: " << result4 << endl;
cout << "Expected: " << num4 << endl << endl;
cout << "RV1: " << resultV1 << endl;
cout << "Expected: " << num4 << endl << endl;
cout << "RV2: " << resultV2 << endl;
cout << "Expected: " << num4 << endl << endl;
cout << "RV3: " << resultV3 << endl;
cout << "Expected: " << num4 << endl << endl;
cout << "RV4: " << resultV4 << endl;
cout << "Expected: " << num4 << endl << endl;
getchar();
return 0;
}
我现在完全不知道如何解决这个问题.我尝试了其他几种方法,但它们似乎都无法正常工作.必须先将整数转换为 char*,因为 WinSock-API 中的 recv() 方法将其读取的字节存储在缓冲区字符数组中.
I have absolutely no idea how to solve this problem at this point. I tried several other ways, but none of them seemed to work correctly yet. It is essential that the integer is converted into a char* first as the recv() method in the WinSock-API stores its read bytes in a buffer char-array.
有什么解释或解决办法吗?提前致谢.
Any explanations or solutions? Thanks in advance.
推荐答案
你可以这样做:
//to a char*
char *P2 = static_cast<char *>((void*)Tst);
//from a char *
int *S1 = static_cast<int *>((void *)P2);
但是,如果每次发送时发送相同数量的整数,您可能需要考虑发送数据块,如下所示:
However, if you are sending the same number of ints each time you send, you may want to consider sending blocks of data instead, like so:
int Data[4] ={42,100,126,517};
char *C1 = static_cast<char*>((void*)Data);
send(socket,C1, sizeof(int[4]),NULL);
并接收:字符 * Buff = 新字符 [16];recv(socket, buff, sizeof(int[4]), 0);int Data = static_cast((void)buff);int R1 = 数据[0];int R2 = 数据[1];int R3 = 数据[2];int R4 = 数据[3];
and to receive: char * Buff = new char[16]; recv(socket, buff, sizeof(int[4]), 0); int Data = static_cast((void)buff); int R1 = Data[0]; int R2 = Data[1]; int R3 = Data[2]; int R4 = Data[3];
或者如果您要发送混合数据,但每次传输的格式都相同,请使用结构体,如下所示:
or if you are sending mixed data, but the format is the same for each transmission, use structs, like so:
struct dataBlock
{
char ID[20];
int R1;
int R2;
int R3;
int R4;
};
...
dataBlock Data = {"test\0", 57,100,127,156};
char *Buff = static_cast<char*>((void*)&Data);
send(socket, Buff, sizeof(dataBlock), 0);
然后接收:
char *Buff = new char[sizeof(dataBlock)];
recv(socket, Buff, sizeof(dataBlock),0);
dataBlock * Data = static_cast<dataBlock*>((void*)Buff);
这篇关于将 char* 转换为 int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!