2

I'm trying to save memory by storing a game's 8x8 board as a binary value but am having trouble:

Each square on the board can be either 10 (black), 11 (white), or 00 (empty) and there are 64 squares. That's 128 binary digits so this should be 16 bytes (128/8) right?

How do I handle that in solidity? Would I say bytes16 foo = 0b10111000...?

Anyone have a link or advice with working directly with bytes in solidity?

Ultimately I'd like to use them as keys in a mapping...

1 Answer 1

1

So BigNumber has really helpful base change operations. For anyone it might be helpful for here's what I did:

var board = 'bbbbbbbbbbbbbbbbwbbwbwbbbbwwwwbbbbbbbbbbwbwwbwbww-bbbbwwwwwwwwww' boardConverted (board) { return board && '0b' + (board.match(/.{1,1}/g).map((spot) => { return spot === 'b' ? '11' : (spot === 'w' ? '10' : '00') }).join('')) } bin2hex (val) { if (!val) return var foo = new web3.BigNumber(val, 2) return foo.toString(16) } hex2bin (val) { if (!val) return var foo = new web3.BigNumber(val, 16) return foo.toString(2) } var hex = bin2hex(boardConvert(board)) var backToBin = hex2bin(hex)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.