Jrp0h
Jrp0h

Reputation: 43

Convert from char to byte

Hello is there a way that makes you convert from char to byte. I know that in c# there is the Method.

Convert.ToByte('a');

But what is it in c++? I have tryed google it but i cant find any answer.

Upvotes: 2

Views: 10683

Answers (1)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726519

Unlike C#, C++ does not have a built-in data type named byte. In addition, its char type has a size of one byte, as opposed to C#, which uses 16-bit characters, so the conversion is always trivial.

If you want your code to be explicit about the sign of your eight-bit data type, use <cstdint> header, declare a variable of type uint8_t, and assign the character to it:

uint8_t x = 'a';

Upvotes: 3

Related Questions