« MSYSAsort for Mawk »

Convertion from hexa to decinal

03.01.12

Permalink 03:59:00 pm, by mazet Email , 55 words   English (US) latin1
Categories: Programmation, Awk

Convertion from hexa to decinal

Simple function to convert hexadecimal figures to decimal. It need to define a constant table hex

BEGIN {
    for (i = 0; i < 10; i++) hex[i] = i

    hex["a"] = hex["A"] = 10
    hex["b"] = hex["B"] = 11

    hex["c"] = hex["C"] = 12
    hex["D"] = hex["d"] = 13

    hex["e"] = hex["E"] = 14
    hex["f"] = hex["F"] = 15

    hex["x"] = hex["X"] = 0
}


function hex2dec(h,  i, x) {

    x = 0
    for (i = 1; i <= length(h); i++)

        x = x * 16 + hex[substr(h, i, 1)]
    return x

}

No feedback yet