我需要通过套接字发送一些unsigned long
个。由于unsigned long
是4字节的事实,接收器只需要4字节。我写的转换函数,你会发现下面的工作,但只有当必须存储在char中的数字不大于127 resp。 0x7F的。对于大于0x7f的值,我希望根据扩展ASCII表(http://www.asciitable.com/)的字符将存储在char中,但这绝对不是他的情况。例如,对于0x90,不存储任何内容。我使用带有Unicode字符集的VS12。
任何想法如何使转换正确?
void number2char(unsigned long number, char* nrAsByte){
std::stringstream numberSS;
numberSS << std::hex << number;
int length = numberSS.str().length();
length = length / 2.0 + 0.5;
nrAsByte = new char[sizeof(number)]();
std::fill(nrAsByte, nrAsByte + length, '\x20');
while (length > 0){
int lastTwo = (number & 0xff);
number >>= 8;
unsigned char a = lastTwo; // this doesn't work if lastTwo > 0x7F
std::memcpy(nrAsByte + length - 1, &a, 1);
--length;
}
}
我很抱歉代码,它没有经过我的测试,并且它包含了错误,请不要使用它,请按照答案中的建议代替
答案 0 :(得分:2)
为什么不能这样:
void number2char(unsigned long number, char* nrAsByte){
unsigned char *dst= reinterpret_cast<unsigned char *> nrAsByte;
for (int i=0; i<sizeof(unsigned long); ++i) {
*dst++= number & 0xFF;
number >>= 8;
}
}
答案 1 :(得分:1)
以下代码也在执行htonl
/ ntohl
之类的操作 - 就像手动转换一样(可能是相反的字节顺序,所以不希望将它与htonl
混合使用,使用这个,或用htonl
重写它。
与Antonio的源不同,当输入数字类型长度为8个字节时,它不会覆盖内存(unsigned long
在我的测试platform上是8个字节 - 顺便说一句,试试吧?!),它将截断该值以适合所需的网络char *缓冲区。
我试图对其进行广泛的评论,以便为每个决定提供更多复杂性的决定(基本上使用unsigned int number
作为(char *)(&number)
使用的内容,这也提供了,但是没有保护如果混合使用不同长度的类型,则可能以覆盖内存结束。但是如果你看到一些不清楚的东西,就问问什么。
#include <iostream>
#include <string>
// Experiment with different types to see differences
// (and how data are truncated when sizeof number > sizeof networkdata)
//typedef unsigned int numberType_t;
typedef unsigned long numberType_t; // on my platform this is 8 bytes long
constexpr int networkBytesSize = 4; // number of chars to be sent trough network with (char *)
// define network data type:
// used "struct" to make sizeof(networkData_t) return actual number of bytes
typedef struct {
unsigned char d[networkBytesSize];
char *cptr() { return reinterpret_cast<char *>(d); }
} networkData_t;
// Writes number into network char* buffer nrAsByte, endianness agnostic
void number2char(numberType_t number, networkData_t & nrAsByte) {
for (size_t i = 0; i < sizeof(networkData_t); ++i) {
nrAsByte.d[i] = number & 0xFF;
number >>= 8;
}
}
// Read number back from network char* buffer
numberType_t char2number(const networkData_t & nrAsByte) {
numberType_t number = 0;
size_t i = sizeof(networkData_t);
while (i--) number = (number<<8) | nrAsByte.d[i];
return number;
}
int main()
{
printf("numberType_t size in bytes: %lu, networkData_t size in bytes: %lu\nAll following numbers are hex:\n",
sizeof(numberType_t), sizeof(networkData_t));
numberType_t number = numberType_t(0x9ABCDEF0123456FEul);
std::cout << "source number: " << std::hex << number << std::endl;
// Write number into char buffer
networkData_t networkData;
number2char(number, networkData);
std::cout << "network bytes:";
for (size_t i = 0; i < sizeof(networkData_t); ++i) std::cout << " [" << unsigned(networkData.d[i]) << "]";
std::cout << std::endl;
// Test usability of (char *) pointer access
const char * testCharPtrConversion = networkData.cptr();
printf("as char * (decimal signed): %d %d ...\n", testCharPtrConversion[0], testCharPtrConversion[1]);
// Read number from char buffer
number = char2number(networkData);
std::cout << "read number: 0x" << std::hex << number << std::endl;
}