Decoding on Base 64 does not consider "Ç"

Asked

Viewed 429 times

1

I have the following code snippets:

window.btoa(CodAcabamento) //Codifica meu código para enviar para a WebApi

string CodAcabamentoDes = Base64Decode(CodAcabamento); //Decodifica meu código para realizar buscas no bd

    public static string Base64Decode(string base64EncodedData)
    {
        var base64EncodedBytes = System.Convert.FromBase64String(base64EncodedData);
        return System.Text.Encoding.UTF8.GetString(base64EncodedBytes);
    }

But the Base64Decode does not consider the Ç, is problem with the Base I’m using or some other problem?

inserir a descrição da imagem aqui

Example of how this in the Database:

inserir a descrição da imagem aqui

Supplementary information:

It was necessary to use the Base64, due to the fact that I use Route and some code receive /, trouble.

  • 1

    I don’t understand why Base64

  • Sorry, that part who did was a colleague of service, because they have some codes that have special characters and was giving me trouble, according to my colleague it was necessary

  • Show an example of how the string is in the database

  • I added in the question @Leandroangelo

  • 1

    The correct one would not use base 64, if you are passing special characters on the route via GET you need to do is an Encode and Decode URL (on account of the /)

  • 1

    There’s no way to know the error without understanding what the method does Base64Decode

  • Ah, sorry, I forgot to post the @LINQ method

  • @Leandroangelo I will research how I can do this and try to change

  • @Jeffhenrique I’ll write a reply.

  • Okay. I’m on hold

  • Base64 as applied has no meaning (at least for me), I suppose Base64 is for avoid losses when transporting BINARY and only data, such as image content. If the idea is to create an Ids that are not numbers for your Urls it would be better to use something like GUID

Show 6 more comments

1 answer

4


It doesn’t make any sense to use Base64 there. My tip is to rewrite in the right way.

In addition, it is important to understand the cause of the problem. Well, most likely, the representation in Base64 that is coming to the application backend refers to the character ç "encoded" in Windows-1252.

In your code, you assume that the encoding of the string is UTF-8. Hence the problem.

Keep in mind that it makes no sense to have a string not knowing what encoding she uses.

The adaptation of your code to work in this particular case would be something like:

public static string Base64Decode(string base64EncodedData)
{
    var base64EncodedBytes = System.Convert.FromBase64String(base64EncodedData);

    var enc = Encoding.GetEncoding(1252); // Windows-1252
    return enc.GetString(base64EncodedBytes);
}
  • Thank you very much for the reply, it worked, I will read the article you sent me, thank you

Browser other questions tagged

You are not signed in. Login or sign up in order to post.