dongrouli2667 2018-12-18 09:57
浏览 573

从Java到Go的字符串模Int

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.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 安卓adb backup备份应用数据失败
    • ¥15 eclipse运行项目时遇到的问题
    • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
    • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
    • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
    • ¥50 成都蓉城足球俱乐部小程序抢票
    • ¥15 yolov7训练自己的数据集
    • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
    • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
    • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)