简体   繁体   English

C ++ const void *转换

[英]C++ const void* conversion

I'm trying to pass three parameters to my write() function: 我试图将三个参数传递给我的write()函数:

write(fd, "C,1,1\r\n", 7);

This works fine. 这很好。 But I would like to take a parameter and pass it to the command portion to make it dynamic: 但我想采用一个参数并将其传递给命令部分以使其动态:

write(fd, N, 4);

I'm not familiar with c++ types, but it keeps asking for a type of "const void*" I've been able to convert my variable, N, to a couple different formats hoping one would be easier to convert. 我对c ++类型不熟悉,但是它一直在询问“ const void *”类型,因此我已经能够将变量N转换为几种不同的格式,希望它们会更容易转换。 This is what I have tried: 这是我尝试过的:

const fmx::FixPt&  outValue = dataVect.AtAsNumber(3);
const double  N = outValue.AsLong();

double  N = outValue.AsLong();

So double and const double(*which might be pretty much the same thing... I don't know c++ very well) 所以double和const double(*这可能几乎是同一件事...我不太了解c ++)

I could also leave it as just: 我也可以将其保留为:

const fmx::FixPt&  outValue = dataVect.AtAsNumber(3);
write(fd, outValue, 4);

but I thought asking everyone how to convert a double would be much better than trying to explain or figure out something as unique as type const fmx::FixPt&... 但是我认为问每个人如何转换双精度比尝试解释或找出像const fmx :: FixPt&...类型一样独特的东西要好得多。

*I also tried: *我也尝试过:

write(fd, &N, 4);

which only gets rid of my error, but still doesn't work. 这只会摆脱我的错误,但仍然无法正常工作。

So, is it even possible to convert to a type of "const void*"? 因此,是否有可能转换为“ const void *”类型?

Thank you very much! 非常感谢你!

Here is code: 这是代码:

const fmx::FixPt& outValue = dataVect.AtAsNumber(3);
double  N = outValue.AsLong();

int fd;
struct termios options;
fd=open("/dev/tty.KeySerial1", O_RDWR | O_NOCTTY | O_NDELAY);
fcntl(fd, F_SETFL, 0);
tcgetattr(fd,&options);
options.c_ispeed=57600;
options.c_ospeed=57600;
options.c_cflag |= (CLOCAL | CREAD);
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
options.c_cflag &= ~CSTOPB;
options.c_lflag &= ~ECHO;
options.c_oflag &= ~ECHO;
options.c_oflag &= ~OPOST;
options.c_cflag |= CS8;
options.c_cflag |= CRTSCTS;
options.c_cc[VMIN] = 0;
options.c_cc[VTIME] =10;
tcflush(fd, TCIFLUSH);
tcsetattr(fd,TCSANOW,&options);


if( tempText->Assign("2"), *tempText == direction ) {
    write(fd, "C,1,2\r\n", 7);//Direction
}else{
    write(fd, "C,1,1\r\n", 7);//Direction
}

if( tempText->Assign("1"), *tempText == speed ) {
    write(fd, "C,2,1\r\n", 7);//Speed
} else if( tempText->Assign("2"), *tempText == speed ) {
    write(fd, "C,2,2\r\n", 7);//Speed
} else if( tempText->Assign("3"), *tempText == speed ) {
    write(fd, "C,2,3\r\n", 7);//Speed
} else if( tempText->Assign("4"), *tempText == speed ) {
    write(fd, "C,2,4\r\n", 7);//Speed
} else if( tempText->Assign("5"), *tempText == speed ) {
    write(fd, "C,2,5\r\n", 7);//Speed
} else if( tempText->Assign("6"), *tempText == speed ) {
    write(fd, "C,2,6\r\n", 7);//Speed
} else if( tempText->Assign("7"), *tempText == speed ) {
    write(fd, "C,2,7\r\n", 7);//Speed
} else if( tempText->Assign("8"), *tempText == speed ) {
    write(fd, "C,2,8\r\n", 7);//Speed
} else if( tempText->Assign("9"), *tempText == speed ) {
    write(fd, "C,2,9\r\n", 7);//Speed
} else if( tempText->Assign("10"), *tempText == speed ) {
    write(fd, "C,2,10\r\n", 8);//Speed
} else if( tempText->Assign("11"), *tempText == speed ) {
    write(fd, "C,2,11\r\n", 8);//Speed
} else if( tempText->Assign("12"), *tempText == speed ) {
    write(fd, "C,2,12\r\n", 8);//Speed
} else if( tempText->Assign("13"), *tempText == speed ) {
    write(fd, "C,2,13\r\n", 8);//Speed
} else if( tempText->Assign("14"), *tempText == speed ) {
    write(fd, "C,2,14\r\n", 8);//Speed
} else if( tempText->Assign("15"), *tempText == speed ) {
    write(fd, "C,2,15\r\n", 8);//Speed
} else if( tempText->Assign("16"), *tempText == speed ) {
    write(fd, "C,2,16\r\n", 8);//Speed
} else if( tempText->Assign("17"), *tempText == speed ) {
    write(fd, "C,2,17\r\n", 8);//Speed
}



if(tempText->Assign("1"), *tempText == length){
    write(fd, "C,3,", 4);
    write(fd, "1", 1);
    write(fd, "\r\n", 2);
} else if(tempText->Assign("2"), *tempText == length){
    write(fd, "C,3,", 4);
    write(fd, "10", 2);
    write(fd, "\r\n", 2);
} else if(tempText->Assign("3"), *tempText == length){
    write(fd, "C,3,", 4);
    write(fd, "100", 3);
    write(fd, "\r\n", 2);
} else if(tempText->Assign("4"), *tempText == length){

    write(fd, "C,3,", 4);
    write(fd, N, 4);
    write(fd, "\r\n", 2);
}


close(fd);

error: cannot convert 'double' to 'const void*' for argument '2' to 'ssize_t write(int, const void*, size_t)' 错误:无法将参数'2'的'double'转换为'const void *'到'ssize_t write(int,const void *,size_t)'

The write command just takes an blob of data and writes to the file descriptor. write命令仅获取一个数据块,然后写入文件描述符。 So, if you wanted to write the binary data held in your double , you would do something like: 因此,如果您想写入保存在double的二进制数据,则可以执行以下操作:

write(fd, &N, sizeof(double));

Note that this write the binary data, not the human-readable ASCII data to your file descriptor. 请注意,这会将二进制数据而不是人类可读的ASCII数据写入文件描述符。 This can be dangerous, since the data is stored in the local architecture's byte order, but that may not be a concern depending on your application. 这是很危险的,因为数据是按本地体系结构的字节顺序存储的,但是根据您的应用程序,可能不必担心。

Normally, in C++, you would use the iostream operators: 通常,在C ++中,您将使用iostream运算符:

double N = 0.0;

// write data to file
ofstream out("myfile", ofstream::binary);
out << N;
out.close();

// read data back
ifstream in("myfile", ifstream::binary);
in >> N;
in.close();

The stream operators are overloaded to take many kinds of arguments, including strings: 流运算符被重载以接受许多类型的参数,包括字符串:

out << "hello world: " << 42 << endl;

The write() function's second parameter is a pointer to a buffer (an array of bytes) and the third parameter is the size of that buffer. write()函数的第二个参数是指向缓冲区(字节数组)的指针,而第三个参数是该缓冲区的大小。 So, you need to somehow get an array of bytes that you want to write out to that file. 因此,您需要以某种方式获取要写入该文件的字节数组。

Your "solution" 您的“解决方案”

write(fd, &N, 4);

will work if N is something that is 4 bytes long. 如果N为4个字节长,它将起作用。 However, it seems like that's not what you want. 但是,这似乎不是您想要的。

It's not clear from your question exactly what it is that you are trying to accomplish. 从您的问题中尚不清楚您要完成的目标到底是什么。 Do you want to write a string? 你想写一个字符串吗? Then you need to convert N to a string, and pass the address of that string's first byte to write . 然后,您需要将N转换为字符串,并将该字符串的第一个字节的地址传递给write

It looks to me as if your problem is with conversion from a double to a string. 在我看来,您的问题似乎是从双精度到字符串的转换。 I take it that, if N has the value 2000, you want to write out the characters '2', '0', '0', and '0', rather than the internal representation of 2000.0. 我认为,如果N的值为2000,则要写出字符“ 2”,“ 0”,“ 0”和“ 0”,而不是内部表示2000.0。

Actually, I see you using a function outValue.AsLong() , which I would expect to return a long value (which is an integer) instead of a double value (which is floating point), and it'll be more convenient to deal with an integer in this case. 实际上,我看到您使用函数outValue.AsLong() ,我希望它返回一个long值(是整数)而不是double outValue.AsLong()值(这是浮点数),这样处理起来会更方便在这种情况下为整数。 In this case, double and long are data types. 在这种情况下, doublelong是数据类型。

Converting numbers to strings is a little awkward in C, and is often done with the sprintf() function. 将数字转换为字符串在C语言中有点尴尬,通常是使用sprintf()函数完成的。 It's a bit tricky. 这有点棘手。 First, you have to specify an array of char to write the string into. 首先,您必须指定一个char数组以将字符串写入其中。 Make sure it's big enough for the biggest number you're going to have, and add one because sprintf() writes one more character to mark the end of the string. 确保它足够大,可以容纳最大数目的数字,并添加一个数字,因为sprintf()再写一个字符来标记字符串的结尾。 (Most C string functions do.) Then call sprintf with it. (大多数C字符串函数都这样做。)然后用它调用sprintf Let's go through this. 让我们来看一下。

char string_value[5];  /* we're assuming the number is always between 0 and 9999 */
long long_value = outValue.asLong();
sprintf(string_value, "%04ld", long_value);
write (fd, string_value, 4);

To explain some of these: We used 5 characters for string_value , since we need the number of characters plus 1. We get the value into long_value , and then use the sprintf() function to put the value into string_value. 为了解释这些问题,我们使用5个字符作为string_value ,因为我们需要字符数加1。我们将值放入long_value ,然后使用sprintf()函数将值放入string_value中。 The first parameter of sprintf() is the memory area where the result goes, the second is the format string, and the following are the values you're putting into the string. sprintf()的第一个参数是结果所在的内存区域,第二个参数是格式字符串,以下是您要放入字符串中的值。 In the format string, the '%' means it's going to describe a field, the '0' means to zero-fill the result, the '4' means to use at least four characters, and the "ld" means you're passing in a long value. 在格式字符串中,“%”表示将要描述一个字段,“ 0”表示对结果进行零填充,“ 4”表示至少使用四个字符,“ ld”表示您正在传递long值。

From your working example, it looks like write takes a const char* as it's second parameter, and the length (number of characters in the const char*) as the 3rd. 在您的工作示例中,write似乎将const char *作为其第二个参数,并将长度(const char *中的字符数)作为第三个参数。

so write(fd, "C,1,1\\r\\n", 7); 因此write(fd, "C,1,1\\r\\n", 7); is passing a const char* that points to "C,1,1\\r\\n", which is 7 characters long. 传递了一个const char *,它指向7个字符长的“ C,1,1 \\ r \\ n”。

If you want to pass in a variable instead of the hardcoded "C,1,1\\r\\n", then you want to do the following: 如果要传递变量而不是硬编码的“ C,1,1 \\ r \\ n”,则需要执行以下操作:

int someNum = 7; // example variable

const int BUFFERSIZE = 256;
char strBuffer[BUFFERSIZE];
snprintf( strBuffer, BUFFERSIZE, "whatever you like, even a number: %d\n\r", someNum );
write( fd, str, BUFFERSIZE );

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

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