0

I am trying to translate this code from Javascript to Go.

if(secret_binary % secret_alphabet_bitlength > 0) {
    secret_binary += zeropad("0", secret_alphabet_bitlength - (secret_binary % secret_alphabet_bitlength));
}

secret_binary is a string (representing a binary).
secret_alphabet_bitlength is an int (6 to be precise).

Note : zeropad function is just adding the number of 0 needed. I replace it by the for loop that you can see below.

From what I understood in this topic Remainder/Modulus of string is a number, the ToNumber is applied to the value that is not a string.

Fine, that is what I tried.

bToBig, _ := new(big.Int).SetString(secretBinary, 2)
nb := int64(secretAlphabetBitLength) - (bToBig.Int64() % int64(secretAlphabetBitLength))
if bToBig.Int64()%int64(secretAlphabetBitLength) > 0 {
    for i := 0; int64(i) < nb; i++ {
        secretBinary += "0"
    }
}

This 000001001100010000000001000111000001000000 is becoming 0000010011000100000000010001110000010000000000 for both the Javascript code and the Golang one. So it is "working", if I can say so.

But, this 001011001001000100001110000001010000010000000101000100000000000010011001000000010000001001010010000001010100000101010011000000 should become 0010110010010001000011100000010100000100000001010001000000000000100110010000000100000010010100100000010101000001010100110000000000 according to the Javascript code but here is what I get 00101100100100010000111000000101000001000000010100010000000000001001100100000001000000100101001000000101010000010101001100000000 (only two zeros are added instead of 4)

I do not know what to do, I tried to parse the string into INT, HEXA, BINARY, OCTAL, etc.. nothing work.

fallais
  • 577
  • 1
  • 8
  • 32
  • *"the ToNumber is applied to the value that is not a string."* You misunderstood, the linked answer states that ToNumber is applied to the *left* value. The left value is `'13'` a string. – mkopriva Dec 18 '18 at 10:19
  • @mkopriva : Ok, thanks. But in that case, it is "the same" right ? The string is converted into a Number. – fallais Dec 18 '18 at 10:21
  • 1
    You are interpreting the string in base 2 in Go, but in base 10 in JavaScript. Also, [Int.Int64](https://golang.org/pkg/math/big/#Int.Int64) says "if x cannot be represented in an int64, the result is undefined". Your number is much bigger than 64 bit in base 2. – Peter Dec 18 '18 at 10:28
  • @Peter : If I change `new(big.Int).SetString(value, 2)` from `2` to `10`, it is not working for the first example (that is working with base 2), any `0` is added whereas `4` should be added. – fallais Dec 18 '18 at 10:31
  • You are dealing with numbers that are much bigger than 64 bit here. Who knows what JavaScript does with that (I seem to remember that you can only use 32 bits accurately, but these numbers are too big in any case). You are trying to replicate JavaScript's undefined behaviour in Go. That's just futile. – Peter Dec 18 '18 at 10:47
  • @Peter : I am trying, the best I can, to translate a code of a very permissive langage into Go. I perfectly know that I am not on the right direction, this is why I need help here, I just discovered `math/big` when trying to translate it. – fallais Dec 18 '18 at 10:50
  • @Peter : I also took a look at this : https://stackoverflow.com/questions/307179/what-is-javascripts-highest-integer-value-that-a-number-can-go-to-without-losin and even if I understand the limitation, I can't stop thinking that there should be a way to get the same result from one langage to another. – fallais Dec 18 '18 at 12:58
  • There is no single solution that always works, because what JS does is implementation dependent. For instance, [parseInt](https://www.ecma-international.org/ecma-262/6.0/#sec-parseint-string-radix) says, "if [the radix] is 10 and [your number string] contains more than 20 significant digits, every significant digit after the 20th may be replaced by a 0 digit, at the option of the implementation". So you really have to figure out what your JS engine does and replicate that in Go. You won't find matching functions in the standard library. – Peter Dec 18 '18 at 13:46
  • @Peter : Thanks, I get it now. I will take a look at the global view of the script to see if I can implement this little function in another way. Maybe it works for him in JS but it is totally lucky – fallais Dec 18 '18 at 13:50

0 Answers0