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!

El Mac
  • 3,256
  • 7
  • 38
  • 58
  • 1
    You have two problems. First you must [convert the hexadecomal string to a byte array](http://stackoverflow.com/questions/311165/how-do-you-convert-byte-array-to-hexadecimal-string-and-vice-versa), after which you can call [`Encoding.ASCII.GetString(byteArray)`](http://stackoverflow.com/questions/18627304/convert-ascii-in-a-byte-array-to-string). – CodeCaster Feb 12 '14 at 10:42
  • First convert the hex string to [to bytes](http://stackoverflow.com/questions/311165/how-do-you-convert-byte-array-to-hexadecimal-string-and-vice-versa), then use `Encoding.ASCII` (or better, UTF8) to convert the bytes to a string. – CodesInChaos Feb 12 '14 at 10:42
  • System.Text.Encoding.ASCII.GetString(buf); – Vignesh Kumar A Feb 12 '14 at 10:45

3 Answers3

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

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

L.B
  • 114,136
  • 19
  • 178
  • 224
  • 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. – M. Mimpen Feb 12 '14 at 10:53
  • 1
    But then with `Encoding.ASCII` – H H Feb 12 '14 at 10:53
  • @L.B, quick question, why //HELL ? – yohannist Feb 12 '14 at 11:12
  • 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. – El Mac Feb 12 '14 at 12:41
  • @ElMac Are you using ASCII or UTF8? (Encoding.Unicode is also available) – L.B Feb 12 '14 at 12:43
  • The external hardware returns ASCII... I can't change that. 'ü' for example is FC (252) – El Mac Feb 12 '14 at 12:55
  • 2
    @ElMac it is surely not ASCII. try to use different encodings such as `Windows-1252`, `Windows-1254` etc. Usage: `Encoding.GetEncoding("Windows-1252").GetString(....)` – L.B Feb 12 '14 at 13:03
  • Well searching for answers I found out ISO 8859 could be it. But I don't know if there are other standards that could apply to it too. There are just so many and I lost the overview. – El Mac Feb 12 '14 at 13:05
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).

Community
  • 1
  • 1
Richard
  • 106,783
  • 21
  • 203
  • 265
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));
    }
jiten
  • 5,128
  • 4
  • 44
  • 73