Monday, March 28, 2011

How to convert this sequence to this hash?

Hi there,

I'm consuming a webservice from my application in C# .net (2.0) that return values (as string) like this:

%23k.%0d%16%c4%8a%18%efG%28%b9c%12%b7%f1%e2%e7j%93

I know that the real value for that is:

236B2E0D16C48A18EF4728B96312B7F1E2E76A93

But I don't know how to convert from the returned value to the real value.

Any thoughts?

Anyway, thanks for your time. Cheers.

From stackoverflow
  • I'm not certain of this, but try using the System.Web.HttpUtility.UrlDecode method

    Matías : Tried, but no... Thanks anyway
  • Going down the line:

    If you encounter '%' copy the next two characters

    Otherwise take the ascii code of the character found and output as hex

    %23  => 23
    k    => 6B
    .    => 2E
    %0d  => 0D
    

    ASCII Table

    But in code I'm pretty sure this will work:

    char c = 'k';
    int ascii = (int)c;
    string hex = ascii.ToString("X2");
    
  • Thanks for the info Rob. If anyone wants the full method here it is:

    static private string convertInfoHash(string encodedHash)
    {
        string convertedHash = string.Empty;
        for (int i = 0; i < encodedHash.Length; i++)
        {
            if (encodedHash[i] == '%')
            {
                convertedHash += String.Format("{0}{1}",encodedHash[i + 1],encodedHash[i + 2]);
                i += 2;
            }
            else
            {
                int ascii = (int)encodedHash[i];
                convertedHash += ascii.ToString("X2");
    
            }
        }
    
        return convertedHash;
    }
    

0 comments:

Post a Comment