unsigned-char

Invalid conversion from ‘void*’ to ‘unsigned char*’

折月煮酒 提交于 2019-12-06 02:06:40
问题 I have the following code; void* buffer = operator new(100); unsigned char* etherhead = buffer; I'm getting the following error for that line when trying to compile; error: invalid conversion from ‘void*’ to ‘unsigned char*’ Why do I get that error, I thought a void was "type-less" so it can point at anything, or anything can point to it? 回答1: You need to cast as you can not convert a void* to anything without casting it first. You would need to do unsigned char* etherhead = (unsigned char*

Comparison signed and unsigned char

↘锁芯ラ 提交于 2019-12-05 05:22:07
问题 It seems so strange. I found misunderstanding. I use gcc with char as signed char. I always thought that in comparison expressions(and other expressions) signed value converts to unsigned if necessary. int a = -4; unsigned int b = a; std::cout << (b == a) << std::endl; // writes 1, Ok but the problem is that char a = -4; unsigned char b = a; std::cout << (b == a) << std::endl; // writes 0 what is the magic in comparison operator if it's not just bitwise? 回答1: According to the C++ Standard 6

-Wconversion warning while using operator <<= on unsigned char

不羁岁月 提交于 2019-12-05 02:16:02
When I compile the following code with gcc : int main() { unsigned char c = 1; c <<= 1; // WARNING ON THIS LINE return 0; } I get this warning : conversion to ‘unsigned char’ from ‘int’ may alter its value [-Wconversion] Why ? What is wrong with this code ? Actually, can I really use the oprator <<= on a unsigned char variable ? Compilation command : g++ test.cpp -Wconversion -o test.exe This is a valid warning: c <<= 1; is equivalent to: c = c << 1 and the rules for << say that the operands are promoted and in this case will be promoted to int and the result is of the promoted type. So there

Invalid conversion from unsigned char* to char*

送分小仙女□ 提交于 2019-12-05 00:21:40
Here is a code - 1 int main(int argc, char *argv[]) 2 { 3 signed char S, *psc; 4 unsigned char U, *pusc; 5 char C, *pc; 6 7 C = S; 8 C = U; 9 10 pc = psc; 11 pc = pusc; 12 13 return 0; 14 } $ gcc test.cpp -o a test.cpp: In function ‘int main(int, char**)’: test.cpp:10:7: error: invalid conversion from ‘signed char*’ to ‘char*’ [-fpermissive] test.cpp:11:7: error: invalid conversion from ‘unsigned char*’ to ‘char*’ [-fpermissive] This is compiled on gcc version 4.6.3 on Ubuntu 12.10 on an Intel 32-bit machine. Considering that char type is unsigned char on x86. - If assignments on line 7 and 8

C++ Converting a float to an unsigned char?

99封情书 提交于 2019-12-04 11:35:42
I'm new to C++, and doing a bit of googling I thought sprintf would do the job, but I get an error upon compiling that I can't convert between an unsigned char and a char . I need an unsigned char because I am going to print to an image file (0-255 RGB). unsigned char*** pixels = new unsigned char**[SIZE]; vector<float> pixelColors; ... sprintf(pixels[i][j][k], "%.4g", pixelColors.at(k)); (pixelColors has size of 3 and 'k' refers to a 'for loop' variable) I'll guess that the floats are in the range 0.0 ... 1.0, then you does like this: float redf = 0.5f; unsigned char reduc = redf * 255; The

Invalid conversion from ‘void*’ to ‘unsigned char*’

时间秒杀一切 提交于 2019-12-04 06:50:18
I have the following code; void* buffer = operator new(100); unsigned char* etherhead = buffer; I'm getting the following error for that line when trying to compile; error: invalid conversion from ‘void*’ to ‘unsigned char*’ Why do I get that error, I thought a void was "type-less" so it can point at anything, or anything can point to it? You need to cast as you can not convert a void* to anything without casting it first. You would need to do unsigned char* etherhead = (unsigned char*)buffer; (although you could use a static_cast also) To learn more about void pointers, take a look at 6.13 —

C - unsigned int to unsigned char array conversion

懵懂的女人 提交于 2019-12-03 16:58:52
问题 I have an unsigned int number (2 byte) and I want to convert it to unsigned char type. From my search, I find that most people recommend to do the following: unsigned int x; ... unsigned char ch = (unsigned char)x; Is the right approach? I ask because unsigned char is 1 byte and we casted from 2 byte data to 1 byte. To prevent any data loss, I want to create an array of unsigned char[] and save the individual bytes into the array. I am stuck at the following: unsigned char ch[2]; unsigned int

Standard Input for Unsigned Character

喜夏-厌秋 提交于 2019-12-03 01:21:56
问题 I am trying to send unsigned characters through a program, and I would like to be able to get the numbers through standard input (ie std::cin). For example when I type 2 I would like it send ☻ (unsigned char 2). when I use the code: std::cout << "Enter values: "; { unsigned char d; unsigned char e = 2; std::cin >> d; WriteFile(file, &d, 1, &written, NULL); std::cout << "d= " << d << "\n"; std::cout << "e= " << e; } I get Enter values: 2 d=2 e=☻ Can anyone tell me why d is being interpreted

Standard Input for Unsigned Character

删除回忆录丶 提交于 2019-12-02 13:42:32
I am trying to send unsigned characters through a program, and I would like to be able to get the numbers through standard input (ie std::cin). For example when I type 2 I would like it send ☻ (unsigned char 2). when I use the code: std::cout << "Enter values: "; { unsigned char d; unsigned char e = 2; std::cin >> d; WriteFile(file, &d, 1, &written, NULL); std::cout << "d= " << d << "\n"; std::cout << "e= " << e; } I get Enter values: 2 d=2 e=☻ Can anyone tell me why d is being interpreted Incorrectly as unsigned char 50 while e is being interpreted correctly as unsigned char 2? And of course

Comparison signed and unsigned char

↘锁芯ラ 提交于 2019-11-30 16:35:59
It seems so strange. I found misunderstanding. I use gcc with char as signed char. I always thought that in comparison expressions(and other expressions) signed value converts to unsigned if necessary. int a = -4; unsigned int b = a; std::cout << (b == a) << std::endl; // writes 1, Ok but the problem is that char a = -4; unsigned char b = a; std::cout << (b == a) << std::endl; // writes 0 what is the magic in comparison operator if it's not just bitwise? According to the C++ Standard 6 If both operands are of arithmetic or enumeration type, the usual arithmetic conversions are performed on