Marco Almeida
Marco Almeida

Reputation: 1303

How to generate random long hexadecimal in 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?

Upvotes: 4

Views: 3376

Answers (1)

Marco Almeida
Marco Almeida

Reputation: 1303

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

Upvotes: 4

Related Questions