Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8455563
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T12:19:03+00:00 2026-06-10T12:19:03+00:00

// This function converts from a hexadecimal representation to a string representation. function hextostr($hex)

  • 0
// 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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T12:19:04+00:00Added an answer on June 10, 2026 at 12:19 pm

    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.
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this function called ProperCase that takes a string, then converts the first
I have this function that converts all special chars to uppercase: function uc_latin1($str) {
I have used this function to convert string to bits. def a2bits(chars): return bin(reduce(lambda
This function works fine: std::string get_str() { return std::get<0>( make_tuple(std::string(hi)) ); } But if
this is from the source code of csv2rec in matplotlib how can this function
I have a function that reads lines from a log file, converts these lines
It's a small thing, really: I have this function that converts dict objects to
I have created user-defined function that converts a comma-delimited string into a table. I
I have a function that converts from my own implementation of a 3D vector
As I hack through our code base I just noticed this function. It converts

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.