How to convert integers in an array that are in decimal to base 4 (signed and unsigned)?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
You can use the algorithm of dividing the number by the desired base repeatedly until the quotient is zero, using the remainders as the final result in reverse order, example :
QUOTIENTS OF EACH DIVISION
▼ ▼ ▼
23÷4 = 5÷4 = 1÷4 = 0
3 1 1
▲ ▲ ▲
REMAINDERS OF EACH DIVISION
The remainders are the digits in the new base (in reverse order) : "113".
Your code will require two blocks :
- One block to make the divisions until the quotient is zero, in this block you store the remainders in stack (push). Each quotient is the dividend of the next division.
- Another block to pop out the remainders and store them in a string. Remainders will be extracted in reverse order.
Edit : in case of negative numbers, the sign must be detected first, if the sign is negative it is necessary to get the absolute value of the number, example :
abs $t1, $t1
The sign must be re-applied to the result at the end (if necessary).