Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

271
Vistas
Codificación de un entero grande sin signo como datos sin signo de longitud fija (256 bits)

Tengo un entero grande sin signo (cercano pero menor a 256 bits). Tengo el entero como BigInt.

Quiero codificarlo como binario de longitud fija de 256 bits y convertirlo en base64. (también decodificarlo más tarde)

¿Hay una buena manera de hacerlo?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Bueno, BigInt realmente ayuda con esto...
mira el fragmento de abajo

 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 ))

En caso de que el archivo console.log de StackOverflow no mostrara los resultados completos, aquí hay una captura de pantalla: resultados de la consola

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda