Convert Hexadecimal string to ascii string in Flutter/Dart

2,878

ASCII doesn't support brazilian characters such as ç, á and etc. You have to use Unicode.

Dart String is natively a Unicode UTF-16, so unless you need ASCII for some reason, just use the Dart String and you will be fine.

Share:
2,878
Gilberto Lopes Inacio
Author by

Gilberto Lopes Inacio

Updated on December 18, 2022

Comments

  • Gilberto Lopes Inacio
    Gilberto Lopes Inacio over 1 year

    I need help because I'm a DART beginner. I get a Json in the following format:

    {"RETORNO":"0","OPERACAO":"PESQUISA_DADOS","UF":"RJ","MUNICIPIO":"","CODIGO_BENEFICIO":"7","BAIRRO":"","PALAVRA_CHAVE":"",
    "REG":[
    {"NOME":"3247204544554341c7c34f20452043554c5455524120464953494341204c5444412e","NOME_ABREVIADO":"435552564553202d2043454e54524f20505241c741204d415541","TIPO_BENEFICIO":"4553504f52544553","ENDERECO":"4156454e4944412052494f204252414e434f2c203433202f20534f4252454c4f4a41202d20434550203230303930303033202d2052494f204445204a414e4549524f202d20524a","TELEFONES":"2832312920203232303330333836","URL_SITE":"7777772e6375727665732e636f6d2e6272"}
    ]
    }
    

    When retrieving the information decode using the instruction below,

    _nome = dados["NOME"]==null ? " " : ascii.decode(hex.decode(dados["NOME"])).toString();
    
    

    I get the following error:

    Error: I/flutter ( 7525): FormatException: Invalid value in input: 199
    

    3247204544554341c7c34f20452043554c5455524120464953494341204c5444412e 2G EDUCAÇÃO E CULTURA FISICA LTDA.

    The Hex format string has special characters (ÇÃ) which causes the error. Do I need to convert to UTF8 format? How should I do? Can anyone help?

  • Gilberto Lopes Inacio
    Gilberto Lopes Inacio about 4 years
    Perfect! Replaces with _name = data ["NAME"] == null? "": String.fromCharCodes (hex.decode (data ["NAME"])); And it worked perfectly. Thanks!!!
  • Michel Feinstein
    Michel Feinstein about 4 years
    My pleasure. If this answered your question, don't forget to mark it as "Answered", clicking on the Check mark below the vote numbers.
  • Ber
    Ber about 3 years
    Your answer would be much more useful if you included actual sample code. Thanks
  • Michel Feinstein
    Michel Feinstein about 3 years
    The sample code is the first comment on this answer.