0

How do I convert a UInt32 value to 4 bytes in swift?

I have a value of (3) when I get;

IPP_ORIENTATION.PORTRAIT.rawValue

Now, I need to convert that value into 4 bytes.

Thanks.

Andrew
  • 552
  • 2
  • 10
  • 29

1 Answers1

2
let value: UInt32 = 1
var u32LE = value.littleEndian // or simply value
let dataLE = Data(bytes: &u32LE, count: 4)
let bytesLE = Array(dataLE)  // [1, 0, 0, 0]

var u32BE = value.bigEndian
let dataBE = Data(bytes: &u32BE, count: 4)
let bytesBE = Array(dataBE)  // [0, 0, 0, 1]
Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
  • 1
    Thank you, didn't know the property little/big Endian existed! – Andrew Mar 22 '21 at 17:44
  • Interesting. (voted.) Is there a similar method to littleEndian/bigEndian to get platform specific binary representations of floating point values? – Duncan C Mar 22 '21 at 17:51
  • @DuncanC https://stackoverflow.com/questions/47502591/convert-a-date-absolute-time-to-be-sent-received-across-the-network-as-data-in/47502712#comment81980820_47502712 – Leo Dabus Mar 22 '21 at 17:54