String to hex in C#


In this post I will be showing a simple C# method that can be used to convert a string(password) to hexadecimal



In mathematics and computing, hexadecimal (also base 16, or hex) is a positional numeral system with a radix, or base, of 16. It uses sixteen distinct symbols, most often the symbols 0–9 to represent values zero to nine, and A, B, C, D, E, F (or alternatively a–f) to represent values ten to fifteen. Hexadecimal numerals are widely used by computer systems designers and programmers. In computing, hexadecimal numerals are usually written with a prefix, "0x" (in reference to the abbreviated pronunciation of "hexadecimal"). Alternately, some authors denote hexadecimal values using a suffix or subscript. For example, one could write 0x2AF3 or 2AF316, depending on the choice of notation. (From Wikipedia)



The Bloc code below contains  a method (ToHex) that convert a string to Hexadecimal, ,white one string argument

 public string ToHex(string inp)  
     {  
       string outp = string.Empty;  
       char[] value = inp.ToCharArray();  
       foreach (char L in value)  
       {  
         int V = Convert.ToInt32(L);  
         outp+=string.Format("{0:x}",V);  
       }  
       return outp;  
     }  

To use the GetSHA1 method
 string result = = Tohex("Net4free");   
          SHA1 hash for Net4free is : 4e6574346672656520



Download



For more information on Hexadecimal: wikipedia.org