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

149
Visualizações
How do I decode the response from an AVAX RPC Call in Vanilla Javascript?

I am trying to fetch the latest block from the Avalanche network's public RPC server. I have succeeded in sending JSON-RPC calls to their server and I get a response. The problem is how to decode the response and make meaning out of the data returned.

Here are my attempts

 <script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>

    <script>

      jQuery.ajax({
    url: "https://api.avax.network/ext/bc/C/rpc",
    type: "post",
    data: JSON.stringify({
    "jsonrpc": "2.0",
    "id": 65,
    "method": "eth_getBlockByNumber",
    "params": ["latest", false]
  }),
    contentType: "application/json; charset=utf-8",
    dataType   : "json",
    success    : function (data) {
      console.log((data.result.gasUsed))
            console.log(data);
        },
    error      : function (xhr, status, errorThrown) {
        console.log(xhr);
    }
});
</script>

My issue now is that each time I try to decode each value from the response object, it fails. I've tried using hexcode to string converter, but it doesn't work. Here is the code snippet I've tried:

function hex2a(hex) {
var str = '';
for (var i = 0; i < hex.length; i += 2) {
    var v = parseInt(hex.substr(i, 2), 16);
    if (v) str += String.fromCharCode(v);
}
return str;

}

This does not return a readable string. And I am stuck on a side project. Can someone help me out?

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

0

You can use the native parseInt() function to convert hex numbers to their decimal form.

Example: parseInt(0x1c6500) returns 1860864.

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