How to convert between number systems
Horner - Converting Between Number SystemsApril 8, 2013 at 7:25 pm | Posted in Bash, Free Software / Open Source, Programming, Ubuntuusers | 11 comments
"Mom, do you know how I convert from one number system to another?"
"No, honey. Doesn't Linux know? "
"No, I can't find a program that can do that."
“There is no program to convert between number systems? Oh no …"
* The door is pushed open, Horner-you storm in *
“Don't be afraid, law-abiding Linuxers! Your complaints have been answered! "
"Ooh, Horner-man. Our hero!"
Ouch. That hurts. I wrote it myself and it still hurts a lot. Well-worn scenario, implausible actors, a touch of sexism. Ouch.
Let's get to the topic. For a long time I have wanted a program with which I can represent a value in different number systems. To do this, however, this program had to be able to convert between these systems. My primary goal was to convert from the decimal system to the binary system and vice versa. There are certainly programs out there that are written for it, or at least can do it on the side. In particular, the conversion between number systems based on base 2, 8, 10 and 16 is often found in digital pocket calculators. Speedcrunch is a fine example of this. However, I didn't just want a program that would show me a result, I also wanted the ability to use this program in a script. So it had to be console-based and input and output should take place via the standard channels.
Since I couldn't find a suitable program, I decided to write one myself. Since I had also wanted to implement the Horner scheme on a test basis for a long time, I decided to use it for the conversion. It must be mentioned: I developed this program for my personal needs, which is why it can only deal with whole numbers. Calculating with floating point numbers is a lot more complicated and it takes a bit of brainpower to develop a program that does not immediately have problems with calculation accuracy. Maybe at some point I will expand the program in this direction. For now it can only use integers. For this it can work with numbers on different bases. The lower limit is base 2 (binary), the upper limit is 36. In theory, of course, it could process even more bases, but with 36 I run out of meaningful number representations (0-9, a-z).
The program, or more the instruction collection, can use positive and negative numbers from 0 to 4294967295. However, 4294967295 is the minimum upper limit. On a 64-bit system, the upper limit is 18446744073709551615 (depending on the compiler; here: GCC). Installation and use will be described later. But first a little excursion:
What is the Horner Scheme?
The Horner scheme was developed by William George Horner and is used to calculate the polynomial. Since the conversion into another number system can be represented as a polynomial, the Horner scheme (or Horner’s method in English) is a very simple and fast type of calculation. By and large, it works like this:
Conversion to the decimal system; B. is the base, a is the starting number, a1 the first position of the same ...:
We want to convert the binary value (base = 2) 101010 into the decimal system:
Converting a decimal value to another system works in the same way. First, the residual value is calculated using modulo (residual value division). This already represents a digit of the result. Since this calculation process is exactly the opposite of the conversion ins Decimal system, the result is also reversed:
As soon as the number with which you calculate 0 has become, the bill ends. The numbers in bold give the result, read from bottom to top. So the string is 101010. This is the same string that we converted to the decimal system earlier. It works!
I wrote a C ++ program for every calculation. 2dec converts a number to the decimal system, dec2 converts a decimal number to any base (from 2 to 36). The two programs can be called up individually with:
If I now want to convert the value 42 into the binary system, I call the program dec2 on:
If I want to reverse the calculation, I use the program 2dec:
The two programs can be combined if I want to convert from one base to another and neither of them is base 10. I want to know what binary value the hexadecimal expression is monkey then I do it like this:
The script takes over this combination horns. This takes over by -i the base of the I.nputs and per -O the base into which the value is to be converted (Output):
corresponds to the above combination of the two programs. The script assumes that the two programs are stored in a directory that is in the$ PATH-Variable of the user is entered.
It is best to compile the code yourself. For those of you who feel uncomfortable with it or just haven't done it yet: don't worry, the Makefile will do the job. To do this, the package automake be installed in the system.
First you need the code, which you can download here.
This archive can be accessed via
unzip to the current directory. The unpacked files are then in the same directory as the archive.
As soon as you have extracted the files you just have to use a terminal
call. This compiles the code and creates the two programs 2dec and dec2.
you can then install the programs automatically. They are stored in the / bin directory in the user's home directory. This directory should always be in the$ PATH and the user always has write access there. If you want to adapt the installation (read: put the files somewhere else), then you just have to change the files 2dec, dec2 and horns store in the desired directory.
The programs, the script and the Makefile are under the GPLv3. They are not tested in any way and implement next to no error handling. I am happy to receive bug reports, but I cannot promise that I will fix them quickly. As mentioned: the programs were created out of curiosity and meet my requirements. That's why I don't plan to upload them to GitHub or anywhere else for the time being. If that's what you want to do, don't force yourself. In this case, please inform me about it.
I'm not going to waste any big words here about the content of the programs and the script. If you are interested in finding out what exactly is going on here, write me in the comments. I will either answer you there or write a new article on the topic.
Powered by Blogilo
I like it:
11 Comments »
RSS feed for comments on this post.TrackBack URI
This site uses Akismet to reduce spam. Learn how your comment data is processed.
- What is the characteristic of urbanization
- Used car dealers are a profitable investment
- French women really don't get fat
- Hiring Microsoft H1B employees
- What is Huawei English translation
- Does private health insurance cover mental health?
- What does medical devices contain
- What is the global unemployment rate
- Can I settle in Germany after MS?
- Who hates pani puri
- Indian women meet up with Hispanic men
- What do Arabs think of the Japanese?
- Why don't the Americans speak French properly?
- How do university teachers teach
- What is it like to skip breakfast
- What is your favorite food in Gujrati
- Are Portuguese particularly good-looking
- How much do teenagers eat
- How to become a Pharmaceutical Representative
- Why don't we have an American anime
- Why do the rich exploit the poor
- Why does China want Tibet
- Why doesn't China have more universities?
- Who maintains the records for cricket databases