How many digits will there be after converting from one system of digits to another

The main question: how many digits?

Let me explain. I have a number in the binary system: 11000000, and in decimal - 192.

After converting to decimal, how many digits will it have (in dicimal)? In my example, these are 3 digits. But it's not a problem. I searched the Internet and found one algorithm for the integral part and one for the fractional part. I do not quite understand them, but (I think) they work.

When converting from binary to octal, this is simpler: every 3 bits give you 1 digit in octal. Same thing for hex: every 4 bits = 1 hexadecimal digit.

But I’m very curious what to do if I have a number in the number system P and you want to convert it to a Q-digital system? I know how to do this (I think I know :)), but fourthly, I want to know how many digits in the Q system it will take (no, I have to pre-allocate space).

+2
source share
7 answers

There was a mistake in my previous answer: look at Ben Schwen's comment. Sorry for the confusion, I found and explained the error I made in my previous answer below.

Please use the answer provided by Paul Tomblin. (rewritten to use P, Q and n)

Y = ln(P^n) / ln(Q) Y = n * ln(P) / ln(Q) 

So, Y (rounded) is the number of characters you need in the Q system to express the largest number that you can encode in n characters in the P system.

I have no answer (which will not convert the number already and take up so much space in the temporary variable) in order to get the minimum minimum for the given number 1000 (bin) = 8 (dec) while you reserve 2 decimal positions using this formula.

If using temporary memory is not a problem, you can cheat and use (Python):

 len(str(int(otherBaseStr,P))) 

This will give you the number of decimal places needed to convert the number in base P other than the string (otherBaseStr) to decimal numbers.


Old WRONG answer:

If you have a number in a system with numbers P of length n then you can calculate the largest number that is possible in n characters:

 P^(n-1) 

To express this highest number in the Q number system, you need to use logarithms (because they are the inverse of exponentiation):

 log((P^(n-1))/log(Q) (n-1)*log(P) / log(Q) 

For example, 11000000 in binary format is 8 characters. To get it in the decimal system, you will need:

 (8-1)*log(2) / log(10) = 2.1 digits (round up to 3) 

The reason is wrong :

The maximum number that is possible in n characters is

 (P^n) - 1 

not

 P^(n-1) 
+5
source

Writing n in base b takes on the upper limit (log base b (n)).

The coefficient you noticed (octal / binary) is base 8 (n) / log base 2 (n) = 3.

(From memory, will he be a stick?)

+6
source

If you have a number that X digits is longer in base B, then the maximum value that can be represented is B ^ X - 1. Therefore, if you want to know how many digits it can take in base C, find the number Y, that C ^ Y - 1 is no less than B ^ X - 1. The way to do this is to take the logarithm in the base of C from B ^ X-1. And since the logarithm (log) of the number in the base C coincides with the natural log (ln) of this number divided by the natural logarithm of C, which becomes:

 Y = ln((B^X)-1) / ln(C) + 1 

and since ln (B ^ X) is X * ln (B) and is probably faster to calculate than ln (B ^ X-1) and close enough to the correct answer, rewrite what

 Y = X * ln(B) / ln(C) + 1 

Hide it in your favorite language. Since we reset β€œ-1,” in some cases we could dial one more number than you need. But even better, you can pre-compute ln (B) / ln (C) and simply multiply it by the new β€œX” and the length of the number you are trying to convert.

+5
source

The calculation of the number of digits can be performed using formulas given by other answers, but in fact it may be faster to first assign a buffer with the maximum size, and then return the corresponding part of this buffer instead of calculating the logarithm.

Note that the worst case for buffer size occurs when converting to binary, which gives you a buffer size of 32 characters for 32-bit integers.

Converting a number to an arbitrary base can be done using the C # function below (the code will be very similar to other languages ​​such as C or Java):

 public static string IntToString(int value, char[] baseChars) { // 32 is the worst cast buffer size for base 2 and int.MaxValue int i = 32; char[] buffer = new char[i]; int targetBase= baseChars.Length; do { buffer[--i] = baseChars[value % targetBase]; value = value / targetBase; } while (value > 0); char[] result = new char[32 - i]; Array.Copy(buffer, i, result, 0, 32 - i); return new string(result); } 
+1
source

Look at the base of the logarithms of P and the base of Q. Break up to the nearest integer.

The logarithmic base P can be calculated using your favorite base (10 or e): log_P (x) = log_10 (x) / log_10 (P)

0
source

You need to calculate the length of the fractional part separately.

For a binary value up to a decimal number, there are as many decimal digits as there are bits. For example, the binary file 0.11001101001001 is the decimal value of 0.80133056640625, with both digits equal to 14 digits after the number.

There are two cases for a binary decimal number. If the decimal fraction is dyadic , then there will be as many bits as decimal digits (the same as for a binary number to a decimal number). If the fraction is not binary, then the number of bits is infinite.

(You can use my decimal / binary converter to experiment with this.)

0
source

Source: https://habr.com/ru/post/1235078/


All Articles