0

I have the following code in Swift 5:

var value1: UInt64 = 9982738475723   
var value2 = UInt32(truncatingIfNeeded: value1)
let bytes = Data(bytes: &value2, count: MemoryLayout.size(ofValue: value2))

What is the Android Kotlin equivalent of this swift data(bytes operation? Basically I am trying to convert UInt32 and UInt64 values to bytes arrays in Android. As an iOS developer it is not easy for me to code in Android so thanks for your help.

Tenfour04
  • 83,111
  • 11
  • 94
  • 154
thus
  • 1,326
  • 1
  • 14
  • 23
  • could you explain what does this code do? It would be easier (at least for me) to write the kotlin equvalent of that code if I knew what It does – JustSightseeing Jan 05 '22 at 12:24
  • 1
    https://stackoverflow.com/questions/1936857/convert-integer-into-byte-array-java – ADM Jan 05 '22 at 12:26

1 Answers1

0
ByteBuffer b = ByteBuffer.allocate(4);    
b.putInt(0xAABBCCDD); 
byte[] result = b.array();

ADM suggested this url: Convert integer into byte array and it worked for me!

Thank you!

thus
  • 1,326
  • 1
  • 14
  • 23