# Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

### Code Snippets / decimal -> base conversion

Message
Posted: 6th Jul 2021 06:46
Here's a piece of code I knocked up in couple of mins which converts decimal whole numbers to either binary, hex, octal. of course we have functions hex\$ and bin\$ but wanted to test out octal (base 8) also.
I wrote a recursive function which worked to a point, but "endfunction res\$" for example did not return the final string, and am working on a differernt way to get it to return correctly.
No code to handle negative numbers yet i.e. the complement system

Professional Programmer, languages: SAS, C++, SQL, PL-SQL, DBPro, Purebasic, JavaScript, others
Posted: 6th Jul 2021 07:51 Edited at: 6th Jul 2021 07:53
recursion version:

Professional Programmer, languages: SAS, C++, SQL, PL-SQL, DBPro, Purebasic, JavaScript, others
Posted: 7th Jul 2021 12:12 Edited at: 7th Jul 2021 12:15
2's complement to be added to code. This is for those not familiar with 2's complemente, is where all the bits get reversed and +1 added. range for neg values would be -128 -> +127 e.g. -128 would be stored as 10000000
Professional Programmer, languages: SAS, C++, SQL, PL-SQL, DBPro, Purebasic, JavaScript, others
Posted: 9th Jul 2021 07:14
of course doing it the other way around i.e. binary to decimal:

To get -7, we use 2's complement (and adding 1).. so 65529-65535(+1)= -7

2's complement for those that don't know, is where the bits get reversed e.g. 7 in binary = 00000111 and 2's complement 11111001

Professional Programmer, languages: SAS, C++, SQL, PL-SQL, DBPro, Purebasic, JavaScript, others

Login to post a reply

Server time is: 2023-03-30 07:02:32
Your offset time is: 2023-03-30 07:02:32