Convert Byte to Normal Text c#

Asked

Viewed 486 times

2

I need to read a byte (Buffer), and convert it to Common Text, that is, I need to convert to letters and/or numbers of the ASCII table.

I am using this function to convert to ASCII Note: when I put:

string result = System.Text.Encoding.UTF8.GetString(buffer);
string result2 = System.Text.Encoding.ASCII.GetString(buffer);
string hexaSemespaco = result2.Replace("#", "");
string test = hexaSemespaco.Substring(1, (hexaSemespaco.Length - 2)); //Tem um símbolo que é ocultado
string convert = ConvertHex(test); //aqui dá o erro, parece que há conflito entre tipos de variaveis, mas não enho ctz`insira o código aqui`

public static string ConvertHex(string hexString)
{
    try
    {
        string ascii = string.Empty;

        for (int i = 0; i < hexString.Length; i += 2)
        {
            string hs = string.Empty;

            hs = hexString.Substring(i, 2);
            uint decval = System.Convert.ToUInt32(hs, 16);
            char character = System.Convert.ToChar(decval);
            ascii += character;

        }

        return ascii;
    }
    catch (Exception ex)
    {
        StreamWriter vWriterr = new StreamWriter(@"c:\POCtest.txt", true);
        vWriterr.WriteLine("ERRO: " + ex.Message);
        vWriterr.Flush();
        vWriterr.Close();
    }
    return "DEU RUIM";
}
  • Your question is confused, you want to convert byte[] for ASCII text ? or want to convert to hexadecimal ?

  • I want to convert byte to ASCII text, but for this I converted to hexa first, then convert to ASCII

1 answer

6


For ASCII:

string result = System.Text.Encoding.ASCII.GetString(byteArray);

For Hexadecimal String (Method 1):

string result = BitConverter.ToString(byteArray);

For Hexadecimal String (Method 2):

public static string ConvertHex(byte[] byteArray)
{
  return BitConverter.ToString(ba).Replace("-","");
}

For Hexadecimal String (Method 3):

public static string ConvertHex(byte[] byteArray)
{
  StringBuilder hex = new StringBuilder(ba.Length * 2);
  foreach (byte b in ba)
    hex.AppendFormat("{0:x2}", b);
  return hex.ToString();
}

Hexa for String:

string HexStringToString(string hexString) 
{
    if (hexString == null || (hexString.Length & 1) == 1) 
    {
        throw new ArgumentException();
    }
    var sb = new StringBuilder();
    for (var i = 0; i < hexString.Length; i += 2) {
        var hexChar = hexString.Substring(i, 2);
        sb.Append((char)Convert.ToByte(hexChar, 16));
    }
    return sb.ToString();
}

Original response: Stackoverflow English.

  • Yes, this I have ,it returns me a Hexadecimal, but after I try to convert this hexa to Normal Letters gives a problem, and as I am running as a Windows Service, it shows no error, simply for the service.

  • @Leandrokojima puts in your question how you are trying to do, puts code that is easier to help.

  • I’m sorry to ask this, but how do I put code here? first time I use Stackoverflow

  • @Leandrokojima here is comment, in question even, up there.

  • @Leandrokojima clicks "edit"

Browser other questions tagged

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