0

I have this code

func myfunc(data: Data, offset: Int) {

  // read two bytes to "length" variable
  let length = data.withUnsafeBytes { rawBuffer in
    rawBuffer.load(fromByteOffset: offset, as: Int16.self).bigEndian
  }
  offset += length 

  // then I want to read "length" bytes to the "value" variable
  // length can be 10 or 20 or 30 or 100.
  // how can I read 10 or 100 bytes from Data?
  let value = data.withUnsafeBytes { rawBuffer in
    rawBuffer.load(fromByteOffset: offset, as: XXX.self)
  }
}

I know in golang I can do this

key := make([]byte, length)
binary.Read(reader, binary.BigEndian, &key)

yes, I'm kinda doing something stupid coverting golang to swift.

Dávid Pásztor
  • 51,403
  • 9
  • 85
  • 116
Gary Allen
  • 385
  • 1
  • 3
  • 11

0 Answers0