我有一个二进制数,表示为11.1111111(与小数点类似)。点前有2位,点后有1024位。这是一个将e计算到高精度的练习,但现在我被困在如何将其转换为十进制的问题上。万一你们想知道号码,就在这里:
10.1011011111100001010100010110001010001010111011010010101001101010101111110111000101011000100000001001110011110100111100111100011101100010111001110001011000001111001110001011010011011010010101101010011110000100110110010000010001010001100100001100111111101111001100100100111001110111001110001001001001101100111110111110010111110100101111111000110110001101100011000011000111010111011000111101101000000110110010000000101010111011000100011000010111101011010011110111110001111011010101110101011111110101100101011000010010010000110011111101010001111101011111000001100110111011010000100001010110001101100101010101010011110111101101000110101111001110110101010101110001001101011110011111110101011111001001001101011001100001001111000011000111000011100000111001101000101101110111111000101010011010001001110110101111001111101111111010000111001000011101111100010101100010100001001101101010110111100111001101010011000010101100110010100100111101001000001110100111100101111010101111000000101010110001100000101011001100100100111110110011101011
如何将其转换为2.718。。。。(小数点后应该有309位左右)我不能简单地将每一位乘以2^x,因为一段时间后,数字2^x将=0,即使使用双精度浮点。我使用的是Visual Basic,所以我不确定是否存在更大的变量。
[Spektre编辑]
只要用我的代码运行字符串(基于我评论中的链接),结果是:
e(bigdecimal)=2.71828182845904523536028747135266249775724709369995957496696762772407663035354759457138217852516642742746639193200305992181741359662904357290033429526059563073813232862794349076323382988075319525101901157383418793070215408914993488416750924476146066808226480016847741185374234544243710753907774499206955170189257927265177296267786175561825444670874889747782175809270565601486538810885558129926100522647929865142359038501319247028975364903531383896590857864585070203793060262761378008328322397393650711101939331201
e (text)=2.71828182845904523536028747135266249775724709369995957496696762772407663035354759457138217852516642742746639193200305992181741359662904357290033429526059563073813232862794349076323382988075319525101901157383418793070215408914993488416750924476146066808226480016847741185374234544243710753907774499206955170189
e (reference)=2.718281828459045235360287471352662497757247093699959574966967627724076630353547594571382178525166427427466391932003059921817413596629043572900334295260595630738132328627943490763233829880753195251019011573834187930702154089149934884167509244761460668082264800168477411853742345442437107539077744992069551702761838606261331384583000752044933826560297606737113200709328709127443747047230696977209310141692836819025515108657463772111252389784425056953696770785449969967946864454905987931636889230098793127736178215424999229576351482208269895193668033182528869398496465105820939239829488793320362509443117301238197068416140397019837679320683282376464804295311802328782509819455815301756717361332069811250996181881593041690351598888519345807273866738589422879228499892086805825749279610484198444363463244968487560233624827041978623209002160990235304369941849146314093431738143640546253152096183690888707016768396424378140592714563549061303107208510383750510115747704171898610687396965521267154688957035035402123407849819334321068170121005627880235193033224745015853904730419957777093503660416997329725088687696640355570716226844716256079882651787134195124665201030592123667719432527867539855894489697096409754591856956380236370162112047742722836489613422516445078182442352948636372141740238893441247963574370263755294448337998016125492278509257782562092622648326277933386566481627725164019105900491644998289315056604725802778631864155195653244258698294695930801915298721172556347546396447910145904090586298496791287406870504895858671747985466775757320568128845920541334053922000113786300945560688166740016984205580403363795376452030402432256613527836951177883863874439662532249850654995886234281899707733276171783928034946501434558897071942586398772754710962953741521115136835062752602326484728703920764310059584116612054529703023647254929666938115137322753645098889031360205724817658511806303644281231496550704751025446501172721155519486685080036853228183152196003735625279449515828418829478761085263981395599006737648292244375287184624578036192981971399147564488262603903381441823262515097482798777996437308997038886778227138360577297882412561190717663946507063304527954661855096666185664709711344474016070462621568071748187784437143698821855967095910259686200235371858874856965220005031173439207321139080329363447972735595527734907178379342163701205005451326383544000186323991490705479778056697853358048966906295119432473099587655236812859041383241160722602998330535370876138939639177957454016137223618789365260538155841587186925538606164779834025435128
第一个是从文本转换到我的arbnum
数据类型,然后转换回文本,中间是纯文本到文本的转换(就像之前转换到十六进制的链接一样),最后是引用e
这里是二进制字符串的十六进制字符串:
e (hex) =2.B7E151628AED2A6ABF7158809CF4F3C762E7160F38B4DA56A784D9045190CFEF324E7738926CFBE5F4BF8D8D8C31D763DA06C80ABB1185EB4F7C7B5757F5958490CFD47D7C19BB42158D9554F7B46BCED55C4D79FD5F24D6613C31C3839A2DDF8A9A276BCFBFA1C877C56284DAB79CD4C2B3293D20E9E5EAF02AC60ACC93ECEBh
我截短了十进制半字节的大小,所以最后可能会有1、2或3位未处理。。。
你可以使用这个系统。数字大整数。它可以容纳大量数据。https://msdn.microsoft.com/en-us/library/system.numerics.biginteger(v=vs.110)。aspx
编辑:我以为十进制后的二进制像往常一样被计算。因此最后一位将是2^1,其中1将一直增加到点。这使得一个数字看起来像1.2*10^308。然后截断为2.12......
但它实际上是点被计算为2^-1和-1被减少到2^-1024后的第一位。因此呈现0。不能用BigInteger表示的x数字。
小数点后的每个二进制数字代表2^-n的十进制权重,从n=1开始。
这可以通过使用Horner方法的任何bignum库来评估:(这只是伪代码)
power_of_five = 1;
digits = 0;
while digits_left
digits = digits * 10;
power_of_five = power_of_five * 5;
if (next_digit_is_set)
digits = digits + power_of_five;
end
这将产生一个1024位的bignum,其中只有前309位是有效的。
>
将二进制字符串转换为十六进制
整数部分很容易"10."-
10.1011011111100001010100010110 bin
10.1011 0111 1110 0001 0101 0001 0110 bin
2.B 7 E 1 5 1 6 hex
正如你所见,每个半字节整数值也是十六进制数字
0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F
这里有一些C代码:
int i,j,l; char *t16="0123456789ABCDEF";
AnsiString s0="10.your binary number",s1="2.";
for (i=4,l=s0.Length();i<=l;)
{
j=0;
if ((i<=l)&&(s0[i]=='1')) j+=8; i++;
if ((i<=l)&&(s0[i]=='1')) j+=4; i++;
if ((i<=l)&&(s0[i]=='1')) j+=2; i++;
if ((i<=l)&&(s0[i]=='1')) j+=1; i++;
s1+=char(t16[j]);
} // here s1 holds the hex string
通过预先分配结果s1大小,可以显著加快速度
运行十六进制到dec字符串转换
AnsiString str_hex2dec(const AnsiString &hex)
{
char c;
AnsiString dec="",s;
int i,j,l,ll,cy,val;
int i0,i1,i2,i3,sig;
sig=+1; l=hex.Length();
if (l) { c=hex[l]; if (c=='h') l--; if (c=='H') l--; }
i0=0; i1=l; i2=0; i3=l;
for (i=1;i<=l;i++) // scan for parts of number
{
char c=hex[i];
if (c=='-') sig=-sig;
if ((c=='.')||(c==',')) i1=i-1;
if ((c>='0')&&(c<='9')) { if (!i0) i0=i; if ((!i2)&&(i>i1)) i2=i; }
if ((c>='A')&&(c<='F')) { if (!i0) i0=i; if ((!i2)&&(i>i1)) i2=i; }
if ((c>='a')&&(c<='f')) { if (!i0) i0=i; if ((!i2)&&(i>i1)) i2=i; }
}
l=0; s=""; if (i0) for (i=i0;i<=i1;i++)
{
c=hex[i];
if ((c>='0')&&(c<='9')) c-='0';
else if ((c>='A')&&(c<='F')) c-='A'-10;
else if ((c>='a')&&(c<='f')) c-='A'-10;
for (cy=c,j=1;j<=l;j++)
{
val=(s[j]<<4)+cy;
s[j]=val%10;
cy =val/10;
}
while (cy>0)
{
l++;
s+=char(cy%10);
cy/=10;
}
}
if (s!="")
{
for (j=1;j<=l;j++) { c=s[j]; if (c<10) c+='0'; else c+='A'-10; s[j]=c; }
for (i=l,j=1;j<i;j++,i--) { c=s[i]; s[i]=s[j]; s[j]=c; }
dec+=s;
}
if (dec=="") dec="0";
if (sig<0) dec="-"+dec;
if (i2)
{
dec+='.';
s=hex.SubString(i2,i3-i2+1);
l=s.Length();
for (i=1;i<=l;i++)
{
c=s[i];
if ((c>='0')&&(c<='9')) c-='0';
else if ((c>='A')&&(c<='F')) c-='A'-10;
else if ((c>='a')&&(c<='f')) c-='A'-10;
s[i]=c;
}
ll=((l*1234)>>10); // num of decimals to compute
for (cy=0,i=1;i<=ll;i++)
{
for (cy=0,j=l;j>=1;j--)
{
val=s[j];
val*=10;
val+=cy;
s[j]=val&15;
cy=val>>4;
}
dec+=char(cy+'0');
for (;;)
{
if (!l) break;;
if (s[l]) break;
l--;
}
if (!l) break;;
}
}
return dec;
}
此C/VCL代码基于C中从dec到/从十六进制字符串的转换
以下是一些完整的结果(无截断):
e (number)=2.7182818284590452353602874713526624977572470936999595749669676277240766303535475945713821785251664274274663919320030599218174135966290435729003342952605956307381323286279434907632338298807531952510190115738341879307021540891499348841675092447614606680822648001684774118537423454424371075390777449920695517018925792726517729626778617556182544467087488974778217580927056560148653881088555812992610052264792986514235903850131924702897536490353138389659085786458507020379306026276137800832832239739365071110193933120100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
e (text) =2.718281828459045235360287471352662497757247093699959574966967627724076630353547594571382178525166427427466391932003059921817413596629043572900334295260595630738132328627943490763233829880753195251019011573834187930702154089149934884167509244761460668082264800168477411853742345442437107539077744992069551701892
e (const) =2.718281828459045235360287471352662497757247093699959574966967627724076630353547594571382178525166427427466391932003059921817413596629043572900334295260595630738132328627943490763233829880753195251019011573834187930702154089149934884167509244761460668082264800168477411853742345442437107539077744992069551702761838606261331384583000752044933826560297606737113200709328709127443747047230696977209310141692836819025515108657463772111252389784425056953696770785449969967946864454905987931636889230098793127736178215424999229576351482208269895193668033182528869398496465105820939239829488793320362509443117301238197068416140397019837679320683282376464804295311802328782509819455815301756717361332069811250996181881593041690351598888519345807273866738589422879228499892086805825749279610484198444363463244968487560233624827041978623209002160990235304369941849146314093431738143640546253152096183690888707016768396424378140592714563549061303107208510383750510115747704171898610687396965521267154688957035035402123407849819334321068170121005627880235193033224745015853904730419957777093503660416997329725088687696640355570716226844716256079882651787134195124665201030592123667719432527867539855894489697096409754591856956380236370162112047742722836489613422516445078182442352948636372141740238893441247963574370263755294448337998016125492278509257782562092622648326277933386566481627725164019105900491644998289315056604725802778631864155195653244258698294695930801915298721172556347546396447910145904090586298496791287406870504895858671747985466775757320568128845920541334053922000113786300945560688166740016984205580403363795376452030402432256613527836951177883863874439662532249850654995886234281899707733276171783928034946501434558897071942586398772754710962953741521115136835062752602326484728703920764310059584116612054529703023647254929666938115137322753645098889031360205724817658511806303644281231496550704751025446501172721155519486685080036853228183152196003735625279449515828418829478761085263981395599006737648292244375287184624578036192981971399147564488262603903381441823262515097482798777996437308997038886778227138360577297882412561190717663946507063304527954661855096666185664709711344474016070462621568071748187784437143698821855967095910259686200235371858874856965220005031173439207321139080329363447972735595527734907178379342163701205005451326383544000186323991490705479778056697853358048966906295119432473099587655236812859041383241160722602998330535370876138939639177957454016137223618789365260538155841587186925538606164779834025435128
e (hex) =2.B7E151628AED2A6ABF7158809CF4F3C762E7160F38B4DA56A784D9045190CFEF324E7738926CFBE5F4BF8D8D8C31D763DA06C80ABB1185EB4F7C7B5757F5958490CFD47D7C19BB42158D9554F7B46BCED55C4D79FD5F24D6613C31C3839A2DDF8A9A276BCFBFA1C877C56284DAB79CD4C2B3293D20E9E5EAF02AC60ACC93ECEBh
AnsiString
只是具有自动重新分配功能的VCL字符串类,因此您可以通过,=
操作符简单地添加字符串。。。
[Edit1]hex2dec字符串转换说明
好的,我们有包含十六进制格式的数字的
hex
字符串,并希望计算dec
,它应该在末尾以十进制格式保存相同的数字。
>
扫描部分数字
这只是用单个
O(n)
循环扫描hex
字符串,并记住特殊字符的位置:
i0
第一个整数位数
所以我们可以使用后者来进行更简单的数字提取。
转换整数部分
整数部分通过对十六进制数字值求和来计算,例如:
51Ah=500h 10h Ah=5*16^21*16^10*16^0=1306
这通常是这样重写的:
51Ah=((5)*16 1)*16 10)=1306
你可以这样想,就像你在源库(10)算术中用目标库(16)乘以数字一样。
因此,您首先从最高有效位开始读取。将其值添加到数字中。如果没有其他整数位存在,则停止将十进制字符串乘以16,然后读取/添加下一个数字。这就是第二个for循环所做的。
>
if(i0)for(i=i0; i
c
设置为处理后的数字的十进制值
(cy=c,j=1;j)的嵌套
123*16= 100*16 + 20*16 + 3*16
=1600 + 320 + 48
所以再次读取数字十进制值
而在这之后
在这之后,
s
以正常顺序(不再反转)将数字值转换为ASCII,复制到最终的dec
字符串,并在需要时添加符号。这就是整数部分的全部内容。
小数部分以
if(i2)
在源基数(16)算法中,分数乘以目标基数(10)可转换为另一个基数。所以用六进制算术乘以
10=Ah
。。。
------------
0.B7Eh -> B7Eh
------------
B7Eh * Ah = 7 2ECh
2ECh * Ah = 1 D38h
D38h * Ah = 8 430h
430h * Ah = 2 9E0h
-------------
0.B7Eh -> 0.7182 dec
------------
好的,如果存在小数部分,则添加到最后的
dec
字符串小数点
。并将所有分数十六进制数字提取到
s
字符串中(首先是最高有效位),将ASCII转换为十六进制数字值
转换在计算
ll
之后开始。for(cy=0, i=1; i
对于
的最后一个,只需检查
s
中的子结果是否以零结尾,如果是,则将其切断(通过l--
),如果没有剩余的有效数字,则停止该过程。这大大加快了处理大量数据的速度。。。
希望你对它的描述足够多。。。
问题内容: 如何在Swift中将Int转换为UInt8?例。我想将数字22转换为0b00010110 问题答案: 您可以使用带有参数的初始化程序将十进制值转换为人类可读的二进制 表示形式 : 如果您愿意,也可以很容易地用任意数量的零填充它: 斯威夫特5
我正在尝试将数字从十进制值转换为其IEEE 752形式。例如: 我写了这个方法: 有个问题我解决不了,我再举个例子说清楚。 但是当数字为负数时,例如 我收到以下错误: 这意味着我的代码可以完美地处理正数(包括零),但如果是负数,代码就会崩溃。问题出在哪里? 笔记 我想我可以用这种方式解决我的问题 检查是否有一个字符At(0)等于1 如果是(numero.charAt(0)==1),则删除第一个字符
我想知道如何将舞蹈方法的十进制结果包含到灯光方法中。例如,在这个程序中,如果我输入5F,十进制结果将是95。我希望95在light方法中显示为静态int变量,以便转换为二进制数。如果你能告诉我如何将十六进制数限制在2位数以内,那也会很有帮助。感谢阅读! } }
本文向大家介绍如何将十六进制转换为十进制?,包括了如何将十六进制转换为十进制?的使用技巧和注意事项,需要的朋友参考一下 而十六进制数是具有值是16的数字系统中的一个并且它具有唯一的16个码元:0,1,2,3,4,5,6,7,8,9和A,B,C,d,E ,其中A,B,C,D,E和F分别是十进制值10、11、12、13、14和15的单位表示。而十进制系统是最熟悉的号码系统向公众开放。它是10的基数,只
本文向大家介绍如何将十进制转换为十六进制?,包括了如何将十进制转换为十六进制?的使用技巧和注意事项,需要的朋友参考一下 十进制是公众最熟悉的数字系统。它是基数10,只有10个符号-0、1、2、3、4、5、6、7、8和9。而十六进制是计算机或数字系统中最常见的数字系统颜色表示。它是基数16,只有16个符号:0、1、2、3、4、5、6、7、8、9和A,B,C,D,E,F。这些A,B,C,D ,E,F分
我正试图编写一个程序,将二进制数字转换成相应的十进制值。虽然错误报告有效,但我似乎在这里得到了与输出相同的二进制数。fIdx是一种正向扫描仪,不进行任何计算。请帮忙!