Search code examples
c++octal

Squawk code generation (4-digit octal identifier) in c++


I'm trying to generate in my C++ project squawk codes in order to assign them to a radar target.

Squawk codes are used to identified airplanes using secondary radar, they are 4 digit octal codes (eg 7066 7067 7070...) : http://en.wikipedia.org/wiki/Transponder_(aeronautics)

I would like to have a function that generate a squawk code based on the last one given, this code is at the moment stored into an int.

I would like to have something like this:

        while (IsSquawkInUse(LAST_ASSIGNED_MODEA_VFR) && LAST_ASSIGNED_MODEA_VFR < 7070) {
        if (LAST_ASSIGNED_MODEA_VFR >= 7067) {
            break;
        }
        else {
            //increment LAST_ASSIGNED_MODEA_VFR 
        }
    }

I still haven't found anything on how to actually do that without having to generate all the existing codes and pick the next one from thoses.

I'm still new to C++ and help would be greatly appreciated.

Cordialement


Solution

  • #include <iostream>
    #include <string>
    using namespace std;
    
    int main() {
        // source number as string
        string x;
        cin >> x;
        // convert it into a number
        int y = 0;
        for (int i = 0, ilen = x.size(); i < ilen; ++i) {
            y *= 8; // it's octal
            y += x[i] - '0';
        }
        // add 1 to that number
        ++y;
        // if we have a code 7777, next should be 0000
        y = y & 07777;
        // clear string x, so that we can
        x.clear();
        // write an octal number there as string
        while (y) {
            x = char(y % 8 + '0') + x;
            y /= 8;
        }
        // output that number
        cout << x << endl;
    }
    

    Live version.