Questions tagged [unsigned-char]

321 questions
217
votes
8 answers

uint8_t can't be printed with cout

I wrote a simple program that sets a value to a variable and then prints it, but it is not working as expected. My program has only two lines of code: uint8_t a = 5; cout << "value is " << a << endl; The output of this program is value is , i.e.,…
CoderInNetwork
  • 2,923
  • 4
  • 22
  • 39
85
votes
17 answers

how do I print an unsigned char as hex in c++ using ostream?

I want to work with unsigned 8-bit variables in C++. Either unsigned char or uint8_t do the trick as far as the arithmetic is concerned (which is expected, since AFAIK uint8_t is just an alias for unsigned char, or so the debugger presents it. The…
Nathan Fellman
  • 122,701
  • 101
  • 260
  • 319
66
votes
3 answers

When is uint8_t ≠ unsigned char?

According to C and C++, CHAR_BIT >= 8. But whenever CHAR_BIT > 8, uint8_t can't even be represented as 8 bits. It must be larger, because CHAR_BIT is the minimum number of bits for any data type on the system. On what kind of a system can uint8_t be…
user541686
  • 205,094
  • 128
  • 528
  • 886
64
votes
3 answers

Why can't I static_cast between char * and unsigned char *?

Apparently the compiler considers them to be unrelated types and hence reinterpret_cast is required. Why is this the rule?
Nick
  • 2,821
  • 5
  • 30
  • 35
58
votes
6 answers

Why does the complement behave differently through printf?

I was reading a chapter on bitwise operators, I came across 1's complement operator program and decided to run it on Visual C++. int main () { unsigned char c = 4, d; d = ~c; printf("%d\n", d); } It gives the valid output: 251 Then instead…
Sanketssj5
  • 655
  • 5
  • 17
26
votes
6 answers

cout not printing unsigned char

I am working on below code: #include #include using namespace std; main() { unsigned char a; a=1; printf("%d", a); cout<
Hitesh Menghani
  • 977
  • 1
  • 6
  • 14
23
votes
7 answers

C - unsigned int to unsigned char array conversion

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…
Jake
  • 16,329
  • 50
  • 126
  • 202
20
votes
6 answers

Bytewise reading of memory: "signed char *" vs "unsigned char *"

One often needs to read from memory one byte at a time, like in this naive memcpy() implementation: void *memcpy(void *dest, const void *src, size_t n) { char *from = (char *)src; char *to = (char *)dest; while(n--) *to++ = *from++; …
Philip
  • 5,795
  • 3
  • 33
  • 68
19
votes
2 answers

std::move between std::string and std::vector

I am working with 2 libraries. One takes in and returns std::strings while the other uses std::vectors. It would be good if I could steal the underlying arrays from std::string and std::vector and be able to move them…
genjix
  • 852
  • 2
  • 11
  • 21
16
votes
5 answers

How to sprintf an unsigned char?

This doesn't work: unsigned char foo; foo = 0x123; sprintf("the unsigned value is:%c",foo); I get this error: cannot convert parameter 2 from 'unsigned char' to 'char'
Christoferw
  • 741
  • 3
  • 7
  • 20
15
votes
3 answers

How to print unsigned char[] as HEX in C++?

I would like to print the following hashed data. How should I do it? unsigned char hashedChars[32]; SHA256((const unsigned char*)data.c_str(), data.length(), hashedChars); printf("hashedChars: %X\n", hashedChars); // doesn't seem to…
louis.luo
  • 2,921
  • 4
  • 28
  • 46
14
votes
3 answers

Put bytes from unsigned char array to std::string using memcpy() function

I have std::string variable. And I need to put some bytes from array of unsigned chars to it. I know the first byte and the the legth. I can use the std::string::assign function. I've done it. But I want to solve that issue in a right way using the…
Oleksandr Balabanov
  • 629
  • 1
  • 6
  • 30
13
votes
1 answer

Copy unsigned char array

What would be the best way to copy unsigned char array to another? For example: unsigned char q[1000]; unsigned char p[1000]; strcpy (q,&p); The above code does not work, it gives me error saying "cannot convert parameter 1 from unsigned char…
user2829
  • 451
  • 1
  • 7
  • 21
13
votes
4 answers

Invalid conversion from unsigned char* to char*

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…
nightlytrails
  • 2,448
  • 3
  • 22
  • 33
11
votes
1 answer

P/Invoke, c#: unsigned char losing a byte

Im working towards a dll file for a software's SDK and i'm trying to call a function to get information about the host of the software. there are two unsigned char variables(HostMachineAddress, HostProgramVersion) in the struct the function wants…
Tistatos
  • 641
  • 1
  • 6
  • 16
1
2 3
21 22