Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

258
Visualizações
Encoding a large unsigned integer as a fixed length (256bit) unsigned data

I have a large unsigned integer (close to but less than 256bit). I have the integer as BigInt.

I want to encode it as 256bit fixed length binary and convert it base64. (also decode it later)

Is there a good way to do it?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Well, BigInt really helps with this..
look at the snippet below

function encode(int,encoding=256,signed){
  let size=2n**BigInt(encoding)
  let toReturn=null, filling="", sign=""
  if(typeof int!=="bigint"){int=BigInt(int)}
  
  //determining the binary string value
  if(int.toString()[0]==="-"){
    signed?sign=1:null //change sign from empty string to 1(meaning negative) if a signed bit, else do nothing
    toReturn=(size-(int%size)).toString(2)
  }
  else{
    signed?sign=0:null //change sign from empty string to 0(meaning positive) if a signed bit, else do nothing
    toReturn=(int%size).toString(2)
  }
  
  //to fill the spaces of empty bits
  let length=encoding-toReturn.length
  for(let i=0;i<length;i++){filling+=0}
  
  return sign+filling+toReturn //returns a String of 1s and 0s representing a binary encoded number
}

function decode(string,signed){ //returns a BigInt of the encoded string's value
  return signed?
  -1n*BigInt("0b"+string.substr(1)): //the signed translation
  BigInt("0b"+string) //the unsigned translation
}

console.log(encode(42)) //unsigned binary 256 bit of normal number
console.log(encode(4213213283721893738293789312731893n)) //unsigned binary 256 bit of a BigInt
console.log(encode("21157920892373161954235709850086879078532699846656405640394575840079131296399362")) //unsigned binary 256 bit of number larger than 2**256
console.log(encode("-21157920892373161954235709850086879078532699846656405640394575840079131296399362",128)) //unsigned binary 128 bit of number larger than 2**128
console.log(encode("-21157920892373161954235709850086879078532699846656405640394575840079131296399362",128,true)) //signed binary 128 bit of number larger than 2**128

console.log(decode( //unsigned encoded string DECODED
  encode(4213213283721893738293789312731893n)
))
console.log(decode( //signed encoded string DECODED
  encode(-4213213283721893738293789312731893n),true
))

In case the StackOverflow's console.log didn't show the full results, here's a screenshot: console results

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda