To compare a char variable to its hex value?

Hello, I am making a program that reads a bin file and stores it in a char array.
Then I have to check each char value to then decided what to do.
For instance, for the first array, this works for me
 
 case 'x': {


However, second character is 'D8'

so if I do a:

 
 case 'D8': {


it will give a warn:

1
2
warning: multi-character character constant [-Wmultichar]
          case 'D8':[


and it does not work.

I suppose I can compare it to a int value, however, to make my life easier compared to what the hex editor shows me, I would like to compare it to a hex representation like 'D8'. Also, I would like to be able to print such representation in the screen.

So, which are the recommended ways of doing so?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <iomanip>
#include <fstream>

int main()
{
    std::ifstream fin("bin01.bin", fin.binary);

    // Set cout to display integers as hex, using uppercase letters,
    // and with '0' instead of ' ' as the fill character for std::setw.
    // https://en.cppreference.com/w/cpp/io/manip
    std::cout << std::hex << std::uppercase << std::setfill('0');

    for (char ch; fin.get(ch); )
    {
        switch (ch)
        {
        case 'x':
            std::cout << "found x\n";
            break;
        case '\xD8':
            std::cout << "found 0xD8\n";
            break;
        }

        // First convert the char, which may be signed, to an unsigned char.
        // Then convert that to int. Output with a 2-digit fieldwidth.
        std::cout << "0x" << std::setw(2) << (int)(unsigned char)ch << '\n';
    }
}

Last edited on
just use the numbers, in hex format if you like.
case 0xD8:
Last edited on
@jonnin, Before the char is compared to the int 0xD8, the char is converted to an int. If char is signed and above 0x7F it will be negative and therefore converted into a negative (sign-extended) int, and so will not equal 0xD8.

1
2
3
// Prints "nope" if char is signed.
#include <iostream>
int main() { std::cout << ('\xD8' == 0xD8 ? "yep\n" : "nope\n"); }

@colt, Here's another program to consider:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <iomanip>
#include <fstream>

int main()
{
    std::ifstream fin("bin01.bin", fin.binary);
    if (!fin)
    {
        std::cerr << "Cannot open input file.\n";
        return 1;
    }

    std::cout << std::hex << std::uppercase << std::setfill('0');

    char text[17] {};
    size_t offset = 0;

    std::cout << std::setw(8) << offset << ' ';
    for (char ch; fin.get(ch); )
    {
        std::cout << ' ' << std::setw(2) << (int)(unsigned char)ch;
        text[offset % 16] = std::isprint(ch) ? ch : '.';
        if (++offset % 16 == 0)
            std::cout << "  |" << text << "|\n" << std::setw(8) << offset << ' ';
        else if (offset % 8 == 0)
            std::cout << ' ';
    }

    if (offset % 16 != 0)
    {
        auto o = offset % 16;
        text[o] = '\0';
        std::cout << std::setfill(' ')
                  << std::setw(3*(16 - o) + (o < 8)) << ""
                  << "  |" << text << '|';
    }

    std::cout << '\n';
}

Example output:

00000000  00 01 02 03 04 05 06 07  08 09 0A 0B 0C 0D 0E 0F  |................|
00000010  10 11 12 13 14 15 16 17  18 19 1A 1B 1C 1D 1E 1F  |................|
00000020  20 21 22 23 24 25 26 27  28 29 2A 2B 2C 2D 2E 2F  | !"#$%&'()*+,-./|
00000030  30 31 32 33 34 35 36 37  38 39 3A 3B 3C 3D 3E 3F  |0123456789:;<=>?|
00000040  40 41 42 43 44 45 46 47  48 49 4A 4B 4C 4D 4E 4F  |@ABCDEFGHIJKLMNO|
00000050  50 51 52 53 54 55 56 57  58 59 5A 5B 5C 5D 5E 5F  |PQRSTUVWXYZ[\]^_|
00000060  60 61 62 63 64 65 66 67  68 69                    |`abcdefghi|

Last edited on
Topic archived. No new replies allowed.