-2
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?

KimJitae
  • 139
  • 11
  • `[B@647aa62` __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. Are you calling `Int("[B@647aa62", 2)!`? – rzwitserloot Nov 17 '22 at 07:24
  • Possibly helpful: https://stackoverflow.com/q/25531914/1187415 – Martin R Nov 17 '22 at 09:24
  • @rzwitserloot As I looked at the code more, Java converted the binary String to a decimal BigInteger and converted it to a Byte Array in order to do binary String To Byte Array. So I'm looking for a way to convert the long binary string written above into a byte array by dividing it into 8 digits. – KimJitae Nov 17 '22 at 09:54

2 Answers2

1

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.

Pierre
  • 422
  • 2
  • 12
  • This is a great hint, but I don't think it answers the question. – lazarevzubov Nov 17 '22 at 06:04
  • 1
    You are completely right. But that was definitely too long for a comment. So I added that the link I provide will most probably contain the answer he is looking for. – Pierre Nov 17 '22 at 06:14
  • It was applied in the process of creating data separately before the existing developer attempted DEFLATE compression to reduce the data to be transmitted to the server. Since the server is also Java, it seems that the data is parsed while applying the logic in reverse. Thanks to everyone who responded and I'll take note of what you've told me. – KimJitae Nov 17 '22 at 08:41
  • Anyway, if you are working with binary data that do not represent numbers, BigInteger is definitely NOT the way you want to go. In Swift that would be `Data`, or maybe `[UInt8]`.... – Pierre Nov 17 '22 at 08:49
  • And do not forget that storing binary in a string like the first line of your question is using 196 *bytes* to represent 196 *bits*, which is 8 times more space... – Pierre Nov 17 '22 at 08:52
1

[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.

rzwitserloot
  • 85,357
  • 5
  • 51
  • 72
  • Thanks for correcting my misunderstanding. Your kind reply helped me a lot. I can see where I am missing. – KimJitae Nov 17 '22 at 08:43