Thyme string to integer
5 posts • Page 1 of 1
Thyme string to integer
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
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
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.
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
}
-
homieeee - Posts: 14
- Joined: Thu Oct 09, 2014 2:13 am
Re: Thyme string to integer
Thanks, but and for Integer to Hex ?
- TheLeandroNex01
- Posts: 11
- Joined: Mon Jan 19, 2015 3:20 am
Re: Thyme string to integer
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
}
-
homieeee - Posts: 14
- Joined: Thu Oct 09, 2014 2:13 am
Re: Thyme string to integer
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
Just for knowledge
- TheLeandroNex01
- Posts: 11
- Joined: Mon Jan 19, 2015 3:20 am
5 posts • Page 1 of 1
Who is online
Users browsing this forum: No registered users and 17 guests