简体   繁体   中英

In C++, how to assign a hex value to a letter/string gotten from user input?

I need some help. Currently I'm writing a program where the user inputs a sentence, the sentence is put into a string, then the string is broken up and each character is displayed on a 7-segment display for 2 seconds each. The problem I'm having is how to assign a hex value to each character; when each character is parsed from the string, naturally it's using its ASCII decimal value. However, the ASCII decimal value will not display the appropriate character on the 7-segment display, which is why I need to change the value so if a user inputs an "h" it'll actually look like an "h" on the display. Could someone please help? I've tried a few things, like '(letter)' == (0x__), but it still displays using the ASCII decimal value.

std::string input = "";

cout << "Please enter a valid sentence (with spaces):\n>";
getline(cin, input);
transform(input.begin(), input.end(), input.begin(), toupper);
cout << "You entered: " << input << endl << endl;


for (char& c : input) {
    OutputData(c);
    Sleep(2000);
    cout << c << endl;
}

You're going to want a large switch case statement:

switch (c) {
    case '0':
        // write 0 on the seven segment display
        break;

    case '1':
        // write 1 on the seven segment display
        break;

    ...

    case 'Z':
        // write Z on the seven segment display
        break;

    default:
        // A character that is not handled by a case.
        // Do something useful.
        break;
}

Alternatively, build a lookup table and use that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM