Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Script Name: dec_to_any
- Original Creator: ragarnak
- Use: dec_to_any(real, string)
- Arg0: Real decimal value greater than or equal to 0 to convert.
- Arg1: String set of symbols to use for conversion.
- Examples:
- "01" for BINARY
- "0123456789ABCDEF" for HEX
- "01234567" for OCTAL
- etc.
- Modified By: Big J/Sporkinator
- Fixed the problem of
- infinite loops when passing
- negative numbers, and added
- some checks to prevent
- unexpected errors.
- Also, instead of flooring,
- used truncation.
- */
- show_debug_message("dec_to_any(" + string(argument0) + ", " + argument1 + ")");
- var base, num, result, error, str;
- error = ""
- if (is_real(argument0))
- {
- num = argument0 - frac(argument0); //Truncate the number
- }
- else
- {
- show_error("First argument to dec_to_any() must be a real.", false);
- return error;
- }
- if (num < 0) //Prevent infinite loop
- {
- show_error("dec_to_any() cannot handle negative values.", false);
- return error;
- }
- str = argument1;
- if (is_real(str)) //Prevent symbol set being passed as real
- {
- show_error("Second argument to dec_to_any() must be a string.", false);
- return error;
- }
- base = string_length(str); //Determine base
- result = "";
- do
- {
- result = string_char_at(str, 1 + (num mod base)) + result;
- show_debug_message("Num = " + string(num) + " mod [Base " + string(base) + "] " + string(num mod base) + " Character Added: " + string_char_at(str, 1 + (num mod base)));
- show_debug_message(string(result));
- //num = num div base; //The "div" operator buggy and can have unexpected results!
- num /= base;
- num -= frac(num);
- }
- until (num == 0);
- return result;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement