簡體   English   中英

C ++ const void *轉換

[英]C++ const void* conversion

我試圖將三個參數傳遞給我的write()函數:

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

這很好。 但我想采用一個參數並將其傳遞給命令部分以使其動態:

write(fd, N, 4);

我對c ++類型不熟悉,但是它一直在詢問“ const void *”類型,因此我已經能夠將變量N轉換為幾種不同的格式,希望它們會更容易轉換。 這是我嘗試過的:

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

double  N = outValue.AsLong();

所以double和const double(*這可能幾乎是同一件事...我不太了解c ++)

我也可以將其保留為:

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

但是我認為問每個人如何轉換雙精度比嘗試解釋或找出像const fmx :: FixPt&...類型一樣獨特的東西要好得多。

*我也嘗試過:

write(fd, &N, 4);

這只會擺脫我的錯誤,但仍然無法正常工作。

因此,是否有可能轉換為“ const void *”類型?

非常感謝你!

這是代碼:

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);

錯誤:無法將參數'2'的'double'轉換為'const void *'到'ssize_t write(int,const void *,size_t)'

write命令僅獲取一個數據塊,然后寫入文件描述符。 因此,如果您想寫入保存在double的二進制數據,則可以執行以下操作:

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

請注意,這會將二進制數據而不是人類可讀的ASCII數據寫入文件描述符。 這是很危險的,因為數據是按本地體系結構的字節順序存儲的,但是根據您的應用程序,可能不必擔心。

通常,在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();

流運算符被重載以接受許多類型的參數,包括字符串:

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

write()函數的第二個參數是指向緩沖區(字節數組)的指針,而第三個參數是該緩沖區的大小。 因此,您需要以某種方式獲取要寫入該文件的字節數組。

您的“解決方案”

write(fd, &N, 4);

如果N為4個字節長,它將起作用。 但是,這似乎不是您想要的。

從您的問題中尚不清楚您要完成的目標到底是什么。 你想寫一個字符串嗎? 然后,您需要將N轉換為字符串,並將該字符串的第一個字節的地址傳遞給write

在我看來,您的問題似乎是從雙精度到字符串的轉換。 我認為,如果N的值為2000,則要寫出字符“ 2”,“ 0”,“ 0”和“ 0”,而不是內部表示2000.0。

實際上,我看到您使用函數outValue.AsLong() ,我希望它返回一個long值(是整數)而不是double outValue.AsLong()值(這是浮點數),這樣處理起來會更方便在這種情況下為整數。 在這種情況下, doublelong是數據類型。

將數字轉換為字符串在C語言中有點尷尬,通常是使用sprintf()函數完成的。 這有點棘手。 首先,您必須指定一個char數組以將字符串寫入其中。 確保它足夠大,可以容納最大數目的數字,並添加一個數字,因為sprintf()再寫一個字符來標記字符串的結尾。 (大多數C字符串函數都這樣做。)然后用它調用sprintf 讓我們來看一下。

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);

為了解釋這些問題,我們使用5個字符作為string_value ,因為我們需要字符數加1。我們將值放入long_value ,然后使用sprintf()函數將值放入string_value中。 sprintf()的第一個參數是結果所在的內存區域,第二個參數是格式字符串,以下是您要放入字符串中的值。 在格式字符串中,“%”表示將要描述一個字段,“ 0”表示對結果進行零填充,“ 4”表示至少使用四個字符,“ ld”表示您正在傳遞long值。

在您的工作示例中,write似乎將const char *作為其第二個參數,並將長度(const char *中的字符數)作為第三個參數。

因此write(fd, "C,1,1\\r\\n", 7); 傳遞了一個const char *,它指向7個字符長的“ C,1,1 \\ r \\ n”。

如果要傳遞變量而不是硬編碼的“ 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