// 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.
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:
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):