Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate random long hexadecimal in Swift?

Tags:

hex

swift

I am trying to generate a random 6 bytes long hex but I ma having problems. the error I am getting is: "Integer literal overflows when stored into 'UInt32'".

Here is my code:

var baseInt = UInt64(arc4random() % 281474976710655)
var str = String(format: "%06X", baseInt)
println("\(str)")

Any help?

like image 962
Marco Almeida Avatar asked Oct 19 '25 01:10

Marco Almeida


1 Answers

Following @Amadan suggestion I got it working.

var baseIntA = Int(arc4random() % 65535)
var baseIntB = Int(arc4random() % 65535)
var str = String(format: "%06X%06X", baseIntA, baseIntB)
println("\(str)")

Then it outputs: "009DD7004974

like image 102
Marco Almeida Avatar answered Oct 21 '25 16:10

Marco Almeida



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!