0

I'm currently working on a program that converts to and from base64 in Eclipse. However, I've just noticed that char values seem to have 7 bits instead of the usual 8. For example, the character 'o' is shown to be represented in binary as 1101111 instead of 01101111, which effectively prevents me from completing my project, as I need a total of 24 bits to work with for the conversion to work. Is there any way to either append a 0 to the beginning of the value (i tried bitshifting in both directions, but neither worked), or preventing the issue altogether?

The code for the (incomplete/nonfuntional) offending method is as follows, let me know if more is required:

std::string Encoder::encode( char* src, unsigned char* dest)
{
 char ch0 = src[0];
 char ch1 = src[1];
 char ch2 = src[2];

 char sixBit1 = ch0 >> 1;


 dest[0] = ch2;
 dest[1] = ch1;
 dest[2] = ch0;
 dest[3] = '-';
}
Jon Derr
  • 43
  • 5
  • 1
    How did you find that out? Is it because the leading `0` is not included when you print? That doesn't mean much. For instance, printing "12" will show "12", even though you can represent numbers up to 2^32-1 for unsigned integers. – EyasSH Mar 25 '17 at 02:47
  • @EyasSH I'm looking at it in Eclipse's Variables window while debugging. It allows me to view different values for a char(like hex, decimal, and binary), and it shows 7 bits (or 5 after the bitshift). It also appears that the result of the shift is incorrect, my code shifts 'Z' to the right by 2, and results in 22 rather than 45 (though I could be mistaken). – Jon Derr Mar 25 '17 at 02:56
  • 'Z' is 90 in ASCII, which is `0b01011010`. Shifted by 2 to the right, you get `0b00010110`, which is 22. – EyasSH Mar 25 '17 at 03:02
  • @EyasSH I just worked that out; I was thinking of the wrong bitwise operator when I was trying to do it in my head. Looks like I was getting worried over nothing. Anyway, thanks for pointing that out, I don't know how long it would have been before I worked that out entirely on my own. – Jon Derr Mar 25 '17 at 03:08
  • In addition to my answer, you can make either GCC or clang treat `char` as `unsigned char` with the `-funsigned-char` flag. – Davislor Mar 25 '17 at 03:48

2 Answers2

0

char for C/C++ language is always signed int8. So, it is excepted that you have only 7 useable bits - because one bit is used for sign storage.

Try to use unsigned char instead.

  • 4
    *char for C/C++ language is always signed int8* No, that is not true. Whether `char` is signed or unsigned is implementation dependent. – R Sahu Mar 25 '17 at 03:07
  • 1
    It is also implementation defined if a `char` is [8 bits or larger](http://stackoverflow.com/questions/32091992/is-char-bit-ever-8) – Bo Persson Mar 25 '17 at 03:16
0

Either unsigned char or uint8_t from <stdint.h> should work. For maximum portability, uint_least8_t is guaranteed to exist.

Davislor
  • 14,674
  • 2
  • 34
  • 49