如何在python中将数字转换为有符号的short?

时间:2021-08-01 22:53:52

标签: python

在 arduino -32750-(32760) 上,它产生 26 的结果,因为它默认使用有符号整数 16 位。

假设我想通过串行在python脚本中获取arduino读取interios,并且我希望python脚本中的减法结果-32750-(32760)也为26。是否有可能在 python 而不是 -65510 中得到相同的结果?也许使用 Struct 库,但我不知道该怎么做。

html,
body {
  height: 100%;
  padding: 0;
  margin: 0;
}

.outer {
  display: table;
  height: 100%;
  margin: 0 auto;
  width: 100%;
  overflow: hidden;
}

.tcell {
  display: table-cell;
  vertical-align: middle;
  padding: 8px 8px;
}

.curtain-wrapper {
  min-width: 40%;
  max-width: 511px;
  margin: auto;
  border: 15px solid transparent;
  border-radius: 12px;
  background: #333;
  background: linear-gradient(to bottom right, gray, black), url("https://i.imgur.com/pwdit9N.png"), linear-gradient(to bottom right, #eee, #ccc);
  background-origin: padding-box, border-box, border-box;
  background-clip: padding-box, border-box, border-box;
  box-shadow: 1px 1px 3px black inset, 0 -1px white, 0 -1px 0 1px #bbb, 0 2px 0 1px #aaa, 0 2px 10px 1px rgb(0 0 0 / 20%);
  overflow: hidden;
}

.curtain-ratio-keeper {
  position: relative;
  padding-top: 56.25%;
}

.curtain {
  position: absolute;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  background: #333;
}

.video-frame {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}

<div class="outer">
  <div class="tcell">
    <div class="curtain-wrapper">
      <div class="curtain-ratio-keeper">
        <div class="curtain">

          <div class="video video-frame"></div>

        </div>
      </div>
    </div>
  </div>
</div>

1 个答案:

答案 0 :(得分:4)

您可以将结果限制为 16 位,如下所示:

result = (int(a) - int(b)) & 0xffff

我知道消极的一面会出现。

x16 = (int(a) - int(b)) & 0xffff
result = x16-65536 if x16 & 0x8000 else x16