-1

Only examples I could find are about Uint8. How to convert Int8 to Data in Swift?

let int8Value: Int8 = -10
let data = int8Value.data // I need some extension like this
Bigair
  • 1,452
  • 3
  • 15
  • 42

1 Answers1

-1

You can use this simple extension to achieve your desired output.

extension Int8{
    var data:Data? {
        return "\(self)".data(using: .utf8)
    }
}
Hammer Class
  • 359
  • 1
  • 19
  • 1
    That would return the *string representation* of the number as UTF-8 data, in this case `2d 31 30`. That may be the desired result or not. – Martin R Mar 17 '22 at 07:39
  • @MartinR What do you mean by this and why is it relevant? Data is always "String-representable", using one encoding or another. And, in the end, Data is Data. – lazarevzubov Mar 17 '22 at 07:45
  • 2
    @lazarevzubov: It is not clear to me what the desired result is. Is it the string representation of that number as data (in this case: `2d 31 30`) or the binary representation as data (in this case: `F6`)? I assume the latter, but clarification from the question author would be helpful. – Martin R Mar 17 '22 at 07:48