1
// This function converts from a hexadecimal representation to a string representation.
function hextostr($hex) {
  $string = "";
  foreach (explode("\n", trim(chunk_split($hex, 2))) as $h) {
    $string .= chr(hexdec($h));
  }

 return $string;
 }

How would I do the exact same thing in c#?

It is for a payment provider, that only provides sample code in php.

1
  • Just to be clear, hex representation is already a string. "Convert hex to string" could be changed to something more appropriate. Commented Aug 29, 2012 at 20:28

3 Answers 3

2

First we examine the PHP. I'm rustier with that than C#, but it is first breaking off chunks of two-characters, then parsing that as hexadecimal, then creating a character from that, and adding it to that produced.

If we are to assume that the string is always ASCII, and hence there is no encoding problem, we can do the same in C# as follows:

public static string HexToString(string hex)
{
  var sb = new StringBuilder();//to hold our result;
  for(int i = 0; i < hex.Length; i+=2)//chunks of two - I'm just going to let an exception happen if there is an odd-length input, or any other error
  {
    string hexdec = hex.Substring(i, 2);//string of one octet in hex
    int number = int.Parse(hexdec, NumberStyles.HexNumber);//the number the hex represented
    char charToAdd = (char)number;//coerce into a character
    sb.Append(charToAdd);//add it to the string being built
  }
  return sb.ToString();//the string we built up.
}

Alternatively, if we have to deal with another encoding, we can take a different approach. We'll use UTF-8 as our example, but it follows with any other encoding (the following will also work in the above case of ASCII-only, since it matches UTF-8 for that range):

public static string HexToString(string hex)
{
  var buffer = new byte[hex.Length / 2];
  for(int i = 0; i < hex.Length; i+=2)
  {
    string hexdec = hex.Substring(i, 2);
    buffer[i / 2] = byte.Parse(hexdec, NumberStyles.HexNumber);
  }
  return Encoding.UTF8.GetString(buffer);//we could even have passed this encoding in for greater flexibility.
}
Sign up to request clarification or add additional context in comments.

2 Comments

string hexdec is not used in the first of the two functions. Is that intentional, or should it be used on the following line?
It was a typo in the first function, that got copy-pasted into the second. Fixed.
0

According to this link:

public string ConvertToHex(string asciiString)
{ 
    string hex = "";
    foreach (char c in asciiString)
    {
        int tmp = c;
        hex += String.Format("{0:x2}", (uint)System.Convert.ToUInt32(tmp.ToString()));
    }
    return hex;
}

Comments

0

You try with this code

var input = "";
String.Format("{0:x2}", System.Convert.ToUInt32(input))

Comments

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.