0

I am looking for a smart way to convert a string of hex-byte-values into a string of 'real text' (ASCII Characters).

For example I have the word "Hello" written in Hexadecimal ASCII: 48 45 4C 4C 4F. And using some method I want to receive the ASCII text of it (in this case "Hello").

// I have this string (example: "Hello") and want to convert it to "Hello".
string strHexa = "48454C4C4F";

// I want to convert the strHexa to an ASCII string.
string strResult = ConvertToASCII(strHexa);

I am sure there is a framework method. If this is not the case of course I could implement my own method.

Thanks!

3

3 Answers 3

2
var str = Encoding.UTF8.GetString(SoapHexBinary.Parse("48454C4C4F").Value); //HELLO

PS: SoapHexBinary is in System.Runtime.Remoting.Metadata.W3cXsd2001 namespace

Sign up to request clarification or add additional context in comments.

8 Comments

Don't you think that using System.Runtime is a bit of farfetched to convert the hexadecimal string to a byte array? As a new programmer on your team I would blink twice before understanding.
But then with Encoding.ASCII
@L.B, quick question, why //HELL ?
Ok but I have one more problem. If I have letters like äöü they will get 'destroyed'. Is there a way to transform this input value from ASCII to unicode or something that can display special characters? I get the input string from a piece of hardware which I can't change.
@ElMac it is surely not ASCII. try to use different encodings such as Windows-1252, Windows-1254 etc. Usage: Encoding.GetEncoding("Windows-1252").GetString(....)
|
2

I am sure there is a framework method.

A a single framework method: No.

However the second part of this: converting a byte array containing ASCII encoded text into a .NET string (which is UTF-16 encoded Unicode) does exist: System.Text.ASCIIEncoding and specifically the method GetString:

string result = ASCIIEncoding.GetString(byteArray);

The First part is easy enough to do yourself: take two hex digits at a time, parse as hex and cast to a byte to store in the array. Seomthing like:

byte[] HexStringToByteArray(string input) {
  Debug.Assert(input.Length % 2 == 0, "Must have two digits per byte");
  var res = new byte[input.Length/2];

  for (var i = 0; i < input.Length/2; i++) {
    var h = input.Substring(i*2, 2);
    res[i] = Convert.ToByte(h, 16);
  }

  return res;
}

Edit: Note: L.B.'s answer identifies a method in .NET that will do the first part more easily: this is a better approach that writing it yourself (while in a, perhaps, obscure namespace it is implemented in mscorlib rather than needing an additional reference).

1 Comment

The Assert() is a nice touch, good to see it being used in a easy and proper way!
0
  StringBuilder sb = new StringBuilder();  
  for (int i = 0; i < hexStr.Length; i += 2)
    {
        string hs = hexStr.Substring(i, 2);
        sb.Append(Convert.ToByte(hs, 16));
    }

1 Comment

One question: why unsigned int as an intermediate type rather than byte?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.