Scala.js – 如何将ArrayBuffer转换为Array[Byte]?

huangapple go评论117阅读模式
英文:

Scala.js - How to convert ArrayBuffer to Array[Byte]?

问题

DOM API 返回 ArrayBuffer,但我需要的是 Array[Byte]。如何进行转换?

import scala.scalajs.js.typedarray.ArrayBuffer

def toScalaArray(input: ArrayBuffer): Array[Byte] = {
  // 有疑问的代码
}

英文:

DOM API returns ArrayBuffer but what I need is Array[Byte]. How can I do a conversion?

import scala.scalajs.js.typedarray.ArrayBuffer

def toScalaArray(input: ArrayBuffer): Array[Byte] = {
  // code in question
}

答案1

得分: 0

Wrap it in an Int8Array, then call the extension method .toArray defined in the typedarray package:

<!-- language: scala -->

import scala.scalajs.js.typedarray._

def toScalaArray(input: ArrayBuffer): Array[Byte] =
  new Int8Array(input).toArray
英文:

Wrap it in an Int8Array, then call the extension method .toArray defined in the typedarray package:

<!-- language: scala -->

import scala.scalajs.js.typedarray._

def toScalaArray(input: ArrayBuffer): Array[Byte] =
  new Int8Array(input).toArray

huangapple
  • 本文由 发表于 2023年6月13日 18:50:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76464130.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定