Home > Mobile >  How can i convert binary string to Int and covert bytes in Swift
How can i convert binary string to Int and covert bytes in Swift

Time:11-17

String value = "0001111000010000000100000100100110000000000000101010000101000000011000000010011110011001001101100011101011110110000100001101010111010011101010011001011001100001001000010000000010110001001001001011"
BigInteger bi = new BigInteger(value, 2);
// bi : 11794183182202048648710358761397377436458666044077659329099
byte[] bytes = bi.toByteArray();
// bytes : [B@647aa62

I have Java code like above. Trying to convert this to Swift.

I wonder how to do the above conversion to get the same result in Swift.

I know it's okay to use 'BigInteger' as 'Int' in Swift.

Int(value,radix: 2)!

I found 'Int(value,radix: 2)!' in Swift. But this didn't work.(I got nil as result. )

What method should I use to get the same result as above java in Swift? And what additional information do I need to get to do this?

CodePudding user response:

First, your string is 196 bits long, which cannot fit in Int (which is 64 bits - when compiled for 64 bits machine). That is probably why you get nil : it does not fit. If you try with a shorter string, it should work.

Well that does not solve your problem. So the first thing is, we have to find a type that can store your integer. The biggest positive integer in swift is UInt64. No fit.

So I suggest we refer to this question. Itcontains references to several libraries allowing to play with big integers in Swift. But before, you should wonder what you intend to do with this number ? Calculations ? Display only ? Or storage only (with less bytes than entire string) ? Is approximation enough ?

If nothing suits your need, then you will need to write your very own type.

CodePudding user response:

[B@647aa62

That is not the data - it is the result of calling toString() on a byte array which provides you [B@ ([B is JVMese for 'byte array') as well as the system identity hashcode of the array, which is, more or less, its memory address (not really - point is, it has nothing to do with the data in it; run the app again and it'll likely be different; the string serves to differentiate it from others, that's all). If that's your confusion ('how do I get 647aa62 out of swing code'), the answer is: You cannot get that.

There are a ton of questions on SO about 'how do I print a byte array', you can use any of those if you're interested in actually seeing this data in hexnibble or base64 or decimal form.

I know it's okay to use 'BigInteger' as 'Int' in Swift.

This is incorrect. Per the swift docs Int is, depending on platform, either 64 or 32 bits large. Your String containing bit values has 192 bits in it. It cannot fit, therefore, the answer to your question of: "How do I get Int(value, 2)! to produce the same value as it does in this java code" is: "That is not possible".

You certainly can't get [@B@647aa62 out, as that has nothing to do with the data and is effectively a random number. You also can't get those 192 bits out, no matter what form you wanted them rendered in, because a 64-bit data structure couldn't possibly contain 192 bits worth of data.

  • Related