多少位将是一个数字系统转换为另一种后 [英] How many digits will be after converting from one numeral system to another
问题描述
主要的问题:有多少位数
The main question: How many digits?
让我来解释一下。我公司拥有一批在二元体系:11000000和十进制为192
Let me explain. I have a number in binary system: 11000000 and in decimal is 192.
转换为十进制后,有多少个数字,将有(以dicimal)?在我的例子,它是3位数。但是,这是没有问题的。我已经搜查了互联网,发现一种算法的组成部分,一个是小数部分。我不是很理解他们,但(我认为),他们的作品。
After converting to decimal, how many digits it will have (in dicimal)? In my example, it's 3 digits. But, it isn't a problem. I've searched over internet and found one algorithm for integral part and one for fractional part. I'm not quite understand them, but (I think) they works.
当从二进制转换为八进制,它更容易:每次3位给你1位八进制。同为十六进制:每4位= 1十六进制数字
When converting from binary to octal, it's more easy: each 3 bits give you 1 digit in octal. Same for hex: each 4 bits = 1 hex digit.
不过,我很好奇,怎么做,如果我有一些P中的数字系统,并想将其转换到Q数字系统?我知道如何做到这一点(我想,我知道:)),但是,所有的第一次,我想知道它有多少位数Q系统采取(U不行,我必须preallocate空间)。
But, I'm very curious, what to do, if I have a number in P numeral system and want to convert it to the Q numeral system? I know how to do it (I think, I know :)), but, 1st of all, I want to know how many digits in Q system it will take (u no, I must preallocate space).
推荐答案
有我的previous答案错误:看评论由Ben Schwehn。 对不起,我的困惑,我发现和解释我在$ P $所产生的误差低于pvious回答。
There was an error in my previous answer: look at the comment by Ben Schwehn. Sorry for the confusion, I found and explain the error I made in my previous answer below.
请使用由保罗汤布林给出了答案。 (重写使用P,Q和N)
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)
所以,Y(四舍五入)是字符,你在系统的Q给前preSS的数量,您可以连接$ C $最需要c在n个字符中的System p的数量。
So Y (rounded up) is the number of characters you need in system Q to express the highest number you can encode in n characters in system P.
我没有答案(这不会转换的数量已经走了,在一个临时变量很多空间),以获得最低限度的给定数量1000(箱)= 8(DEC),而你将保留2使用这个公式小数位。
I have no answer (that wouldn't convert the number already and take up that many space in a temporary variable) to get the bare minimum for a given number 1000(bin) = 8(dec) while you would reserve 2 decimal positions using this formula.
如果一个临时的内存使用是没有问题的,你可以欺骗和使用(Python)的:
If a temporary memory usage isn't a problem, you might cheat and use (Python):
len(str(int(otherBaseStr,P)))
这会给你转换的基础P上一些必要的小数位数,转换为字符串(otherBaseStr),进入小数。
This will give you the number of decimals needed to convert a number in base P, cast as a string (otherBaseStr), into decimals.
旧的错误答:
如果您有多个P中的数字长度为n的系统 然后你就可以计算出最高的数字,这是可能的n个字符:
If you have a number in P numeral system of length n Then you can calculate the highest number that is possible in n characters:
P^(n-1)
要这个次数最多的号码系统的Q值,你需要使用对数(因为他们是逆幂)EX preSS:
To express this highest number in number system Q you need to use logarithms (because they are the inverse to exponentiation):
log((P^(n-1))/log(Q)
(n-1)*log(P) / log(Q)
例如 二进制11000000的8个字符。 为了得到它的小数,你将需要:
For example 11000000 in binary is 8 characters. To get it in Decimal you would need:
(8-1)*log(2) / log(10) = 2.1 digits (round up to 3)
原因,它是错
最高的数字,这是可能的n个字符是
The highest number that is possible in n characters is
(P^n) - 1
不是
P^(n-1)
这篇关于多少位将是一个数字系统转换为另一种后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!