如何将十进制格式转换为数字格式

Posted

技术标签:

【中文标题】如何将十进制格式转换为数字格式【英文标题】:How to convert decimal format to number format 【发布时间】:2020-01-04 10:13:48 【问题描述】:

当我将此二进制转换为十进制时:

010000010110110001101001001000000100000101101000011011010110010101100100001000000100111101110011011011010110000101101110001000000110001001101001011100100111010001101000011001000110000101110100011001010011101000110001001011010011001100101101001100010011100100111000001101110010000001100011011011110111010101101110011101000111001001111001001110100111001101110101011001000110000101101110 P>

使用 VB.Net,我得到了这个双数:1.00695950340148E+115

我需要将其格式化为正常数字,如下所示:

10069595034014783469636931351646363690636553221072125712116008033168565762469882090200055592025448896479090348155246

我如何使用 php 或 VB.Net 做到这一点?

Public Class Test
    Public Shared Sub Main()

Dim Bin as String = "010000010110110001101001001000000100000101101000011011010110010101100100001000000100111101110011011011010110000101101110001000000110001001101001011100100111010001101000011001000110000101110100011001010011101000110001001011010011001100101101001100010011100100111000001101110010000001100011011011110111010101101110011101000111001001111001001110100111001101110101011001000110000101101110"

Dim dec As Double = Nothing
Dim length As Integer = Len(Bin)
Dim temp As Integer = Nothing
Dim x As Integer = Nothing
        For x = 1 To length
            temp = Val(Mid(Bin, length, 1))
            length = length - 1
            If temp <> "0" Then
                dec += (2 ^ (x - 1))
            End If
        Next
        System.Console.WriteLine("Sum of x and y = " &  dec)
    End Sub
End Class

输出是:

Visual Basic.Net 编译器版本 0.0.0.5943 (Mono 4.7 - tarball) 版权所有 (C) 2004-2010 Rolf Bjarne Kvinge。保留所有权利。 程序集'jdoodle,版本 = 0.0,文化 = 中性,PublicKeyToken = null' 成功保存到“/home/jdoodle.exe”。编译成功 编译耗时 00:00:01.1970230 x 和 y 之和 = 1.00695950340148E+115

【问题讨论】:

System.Numerics.BigInteger。逐个字符解析输入字符串(For Each 循环会做),1 解析left shift (&lt;&lt;=) 然后添加10,具体取决于您要解析的字符。跨度> 如果您要标记为vb.net,请使用vb.net。您应该已将其标记为 vb6,这是愚蠢的古老事物的正确标记,例如:LenValMid。你为什么要做= Nothing之类的事情?你知道那是什么意思吗?请阅读vb.net的快速介绍。 @video.baba 这是一个新的贡献者。您的观点有效,但您的交付方式没有帮助。 @video.baba :实际上这些函数在 VB.NET 中也存在,Microsoft.VisualBasic.Strings module 中也存在。 VB6 标记将不合适,因为其余代码实际上是 VB.NET。 -- 但是,正如您所说,这些功能 旧的,并且仅为了向后兼容/更易于移植而存在,所以我同意应该努力使用 .NET 替代方案。 :) 【参考方案1】:

请用我的这个方法试一试,但我没有验证它是否正确:

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    Dim myBin = "110101010100001111101010101010101000000110110101100000010111110101010100001111101010101010101000000110110101100000010111110101010100001111101010101010101000000110110101100000010111110101010100001111101010101010101000000110110101100000010111110101010100001111101010101010101000000110110101100000010111110101010100001111101010101010101000000110110101100000010111110101010100001111101010101010101000000110110101100000010111110101010100001111101010101010101000000110110101100000010111110101010100001111101010101010101000000110110101100000010111110101010100001111101010101010101000000110110101100000010111110101010100001111101010101010101000000110110101100000010111110101010100001111101010101010101000000110110101100000010111110101010100001111101010101010101000000110110101100000010111110101010100001111101010101010101000000110110101100000010111110101010100001111101010101010101000000110110101100000010111110101010100001111101010101010101000000110110101100000010111110101010100001111101010101010101000000110110101100000010111110101010100001111101010101010101000000110110101100000010111110101010100001111101010101010101000000110110101100000010111110101010100001111101010101010101000000110110101100000010111110101010100001111101010101010101000000110110101100000010111110101010100001111101010101010101000000110110101100000010111110101010100001111101010101010101000000110110101100000010111110101010100001111101010101010101000000110110101100000010111"
    Debug.Print(ConvBinToDec(myBin))
End Sub

Function ConvBinToDec(ByVal theVal As String)
    ConvBinToDec = ""
    Dim BinVal(Len(theVal)) As String
    BinVal(1) = "1"
    Dim Total = "0"
    If Mid(theVal, Len(theVal), 1) = "1" Then Total = "1"
    Dim myLast As String = ""
    For a = 2 To Len(theVal)
        BinVal(a) = Multi(BinVal(a - 1))
        myLast = BinVal(a)
        If Mid(theVal, Len(theVal) - a + 1, 1) = "1" Then
            Total = Adding(Total, myLast)
        End If
    Next
    ConvBinToDec = Total

End Function

Function Multi(ByVal TheVal As String) As String
    Dim simpan As Byte = 0
    Multi = ""
    Dim myLen As Byte = 10
    Dim myTimes = -Int(-Len(TheVal) / myLen)

    For a = 1 To myTimes
        Dim myStr As String = Strings.Right(TheVal, a * myLen)
        Dim myRemain As String = Strings.Left(myStr, Len(myStr) - (a - 1) * myLen)
        Multi = Strings.Right("0000000000" & CLng(myRemain) * 2 + simpan, myLen) & Multi
        If Len(CStr(CLng(myRemain) * 2 + simpan)) > myLen Then simpan = 1 Else simpan = 0
        If a = myTimes And simpan = 1 Then Multi = "1" & Multi
    Next
End Function

Function Adding(ByVal TheVal1 As String, ByVal TheVal2 As String) As String
    Dim simpan As Byte = 0
    Adding = ""
    Dim myLen As Byte = 10
    Dim myTimes = -Int(-Len(TheVal2) / myLen)
    TheVal1 = Strings.Right("00000000000" & TheVal1, Len(TheVal2))
    For a = 1 To myTimes
        Dim myStr1 As String = Strings.Right(TheVal1, a * myLen)
        Dim myStr2 As String = Strings.Right(TheVal2, a * myLen)
        Dim myRemain1 As String = Strings.Left(myStr1, Len(myStr1) - (a - 1) * myLen)
        Dim myRemain2 As String = Strings.Left(myStr2, Len(myStr2) - (a - 1) * myLen)
        Adding = Strings.Right("0000000000" & CLng(myRemain1) + CLng(myRemain2) + simpan, myLen) & Adding
        If Len(CStr(CLng(myRemain1) + CLng(myRemain2) + simpan)) > myLen Then simpan = 1 Else simpan = 0
        If a = myTimes And simpan = 1 Then Adding = "1" & Adding
    Next
End Function

当我尝试使用你的二进制文件时,

  "010000010110110001101001001000000100000101101000011011010110010101100100001000000100111101110011011011010110000101101110001000000110001001101001011100100111010001101000011001000110000101110100011001010011101000110001001011010011001100101101001100010011100100111000001101110010000001100011011011110111010101101110011101000111001001111001001110100111001101110101011001000110000101101110"

结果是:

"000010069595034014783469636931351646363690636553221072125712116008033168565762469882090200055592025448896479090348155246"

【讨论】:

OP 要求的是实际值,而不是另一个字符串。试试这个:Dim bigInt As BigInteger = 0 For Each c As Char In myBin bigInt &lt;&lt;= 1 bigInt += If(c.Equals("1"c), 1, 0) Next 谢谢@jimi,我可以在VB.net中获取按位的信息,从现在到未来我都会使用它。

以上是关于如何将十进制格式转换为数字格式的主要内容,如果未能解决你的问题,请参考以下文章

输入时如何将文本字段格式设置为十进制?

如何将 gi-normous 整数(字符串格式)转换为十六进制格式? (C#)

如何在 Delphi 中将浮点数转换为字符串,指定有效数字,而不是十进制数字(而不是 G 格式)

如何将十六进制字符串转换为十六进制数字

如何将大数(例如 1.23e29)转换为 IEEE 754 单精度浮点格式?

在Excel中如何将数字格式批量转换为文本格式,并且保留二位小数?