【问题标题】:Is there a Python equivalent to these functions?是否有与这些功能等效的 Python?
【发布时间】:2021-03-17 03:23:49
【问题描述】:

此代码将数组缓冲区转换为字符串,反之亦然

function ab2str(buf) {
  return String.fromCharCode.apply(null, new Uint16Array(buf));
}

function str2ab(str) {
  var buf = new ArrayBuffer(str.length*2); // 2 bytes for each char
  var bufView = new Uint16Array(buf);
  for (var i=0, strLen=str.length; i < strLen; i++) {
    bufView[i] = str.charCodeAt(i);
  }
  return buf;
} 

【问题讨论】:

  • 为什么你需要一个等价物? Python 中没有 ArrayBuffers。告诉我们您真正想要做什么,我们会为您提供建议。

标签: python jscript


【解决方案1】:

我认为 Python bytearray() 可以完成这项工作。

您可以参考此页面了解更多信息:https://www.programiz.com/python-programming/methods/built-in/bytearray

【讨论】:

    猜你喜欢
    • 2023-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多