Search code examples
stringhexprintfvala

How to format int64 to upper case hex in Vala?


The following example does not compile

    public static int main (string[] args) {
        var now = new GLib.DateTime.now_utc();
        int64 val = now.to_unix();
        print ("%" + int64.FORMAT + "\n", val);
        print ("%X\n", val);  // ERROR
        return 0;
    }

There is a int64 format string for decimal representation but none for hex (See Valadoc). The %X does also not work. How do I get upper case hex formatting for a int64?


Solution

  • The error is a type error: Argument 2: Cannot convert from int64 to uint

    print uses the printf format string and accepts the long long type, which is specified as at least 64 bits. You can use %llX to print out an int64 as upper case hexadecimal.

    Working example:

    void main () {
        var now = new GLib.DateTime.now_utc();
        int64 val = now.to_unix();
        print ("%" + int64.FORMAT + "\n", val);
        print ("%llX\n", val);
    }