Skip to content

To hexadecimal

C++ loop + array

Flowchart (ISO 5807)

YesNoYesNoStartInput ndigits = «0123456789ABCDEF»k = 0n = 0Output «0»ReturnEndn > 0buf[k] = digits[n mod 16]n = n / 16k = k + 1i = k − 1, 0, −1Output buf[i]EndFigure 1 — toHex

Source code

C++
void toHex(int n) {
    const char* digits = "0123456789ABCDEF";
    char buf[32];
    int k = 0;
    if (n == 0) {
        cout << "0";
        return;
    }
    while (n > 0) {
        buf[k] = digits[n % 16];
        n /= 16;
        k++;
    }
    for (int i = k - 1; i >= 0; i--) {
        cout << buf[i];
    }
}

The editor opens with this code — change it and the chart redraws.

Language
C++
Constructs
loop + array
Topic
Algorithms
Formats
Word, Visio, draw.io, PDF, SVG

Related examples