【问题标题】:How to convert 64 bit binary string in to a decimal number in tcl?如何在 tcl 中将 64 位二进制字符串转换为十进制数?
【发布时间】:2013-11-27 18:34:59
【问题描述】:

我正在尝试将 64 位二进制字符串值 () 转换为整数,但我显然达到了 tcl 限制并得到:

integer value too large to represent

二进制字符串:

000000000000101000000000000010100000000000000100

我用来从二进制转换为整数的过程是:

  proc binary2decimal {bin} {
     binary scan [binary format B64 [format %064d $bin]] I dec
     return $dec
  }

我尝试使用 64 进行格式化,也尝试了 LI 无济于事。我读到了“wide”,但不明白是否可以在这里应用。

P.S:我使用的是 tcl 8.4

【问题讨论】:

    标签: binary 64-bit tcl decimal


    【解决方案1】:

    首先,错误来自format,默认情况下它对 64 位数字不太满意(出于向后兼容性的原因),但我们确实在那里处理字符串,所以 @987654322 @(s,而不是d)是一个合适的修复方法。

    你真的应该改用 Tcl 8.5,这要容易得多

    binary scan [binary format B64 [format "%064s" $bin]] W dec
    

    甚至只是:

    set dec [expr "0b$bin"]
    

    在 8.4 中,您必须做更多的工作,因为 64 位支持更加原始。

    binary scan [binary format B64 [format %064s $bin]] II d1 d2
    set dec [expr {wide($d1)<<32 | wide($d2)}]
    

    这有点不明显,但I 格式一直被记录为仅适用于 32 位值。所以我们在扫描后将两个(大端)半值缝合在一起。

    【讨论】:

      猜你喜欢
      • 2017-08-08
      • 1970-01-01
      • 1970-01-01
      • 2012-05-02
      • 1970-01-01
      • 2014-11-07
      • 1970-01-01
      • 1970-01-01
      • 2013-11-24
      相关资源
      最近更新 更多