Thyme string to integer

About advanced scenes, and the Thyme scripting language used in Algodoo.

Thyme string to integer

Postby TheLeandroNex01 » Sun Aug 30, 2015 10:15 pm

Hello there, after some time i'm BACK!!!!
Well, i'm need to convert a HEX format string to int, but the algodoo "Math.ToInt" does not support HEX formats :(
Any ideas ?
Example :
scene.my.toInt("FF") = 255
TheLeandroNex01
 
Posts: 11
Joined: Mon Jan 19, 2015 3:20 am

Re: Thyme string to integer

Postby homieeee » Mon Aug 31, 2015 10:54 pm

Here is a simple code to convert Hex to Dec. It is nothing special but it works.

This videos explains what the code does well https://www.youtube.com/watch?v=gCs__X-6szs

Any other questions just ask.

Code: Select all
_hexToDec = (hex)=>{
    sum := 0;
    for(string.length(hex), (i)=>{
        sum = sum + _trueval(string.str2list(hex)(string.length(hex) - 1 - i)) * 16 ^ i
    });
    sum
}


Code: Select all
_trueVal = (char)=>{
    return := -1;
    (eval(char) != undefined) ? {
        return = eval(char)
    } : {
        (char == "a") ? {
            return = 10
        } : {
            (char == "b") ? {
                return = 11
            } : {
                (char == "c") ? {
                    return = 12
                } : {
                    (char == "d") ? {
                        return = 13
                    } : {
                        (char == "e") ? {
                            return = 14
                        } : {
                            (char == "f") ? {
                                return = 15
                            } : {}
                        }
                    }
                }
            }
        }
    };
    return
}
User avatar
homieeee
 
Posts: 14
Joined: Thu Oct 09, 2014 2:13 am

Re: Thyme string to integer

Postby TheLeandroNex01 » Tue Sep 01, 2015 2:18 am

Thanks, but and for Integer to Hex ?
TheLeandroNex01
 
Posts: 11
Joined: Mon Jan 19, 2015 3:20 am

Re: Thyme string to integer

Postby homieeee » Tue Sep 01, 2015 4:54 am

What do you need this for anyway?

Code: Select all
_decToHex = (dec)=>{
    val = dec;
    hex := "";
    for(math.toint(math.log(dec) / math.log(16)) + 1, (i)=>{
        (val % 16 > 9) ? {
            (val % 16 == 10) ? {
                hex = "a" + hex
            } : {
                (val % 16 == 11) ? {
                    hex = "b" + hex
                } : {
                    (val % 16 == 12) ? {
                        hex = "c" + hex
                    } : {
                        (val % 16 == 13) ? {
                            hex = "d" + hex
                        } : {
                            (val % 16 == 14) ? {
                                hex = "e" + hex
                            } : {
                                (val % 16 == 15) ? {
                                    hex = "f" + hex
                                } : {}
                            }
                        }
                    }
                }
            }
        } : {
            hex = math.tostring(val % 16) + hex
        };
        val = val / 16
    });
    hex
}
User avatar
homieeee
 
Posts: 14
Joined: Thu Oct 09, 2014 2:13 am

Re: Thyme string to integer

Postby TheLeandroNex01 » Thu Sep 03, 2015 2:54 am

Well, i'm making a simple computer(in thyme) that consists in ROM, CPU and RAM, i need to make it works at hexadecimal(actualy it is using decimal).
Just for knowledge :)
TheLeandroNex01
 
Posts: 11
Joined: Mon Jan 19, 2015 3:20 am


Return to Thyme scripting

Who is online

Users browsing this forum: No registered users and 17 guests

cron