This could be shorter tight? (String to Hex)

  • Thread starter Thread starter Edwin Knoppert
  • Start date Start date
E

Edwin Knoppert

public static String StringToHex(String sString)
{
if (sString.Length == 0) { return ""; }
StringBuilder sb = new StringBuilder(sString.Length * 2);
Char[] chars = sString.ToCharArray();
String T = "";
foreach (Char c in chars)
{
T = "0" + Convert.ToString(c, 16).ToUpper();
sb.Append(T.Substring (T.Length -2 ));
}
return sb.ToString();
}
 
Sorry, did not understand from your code what is this function suppose to
do.
Please tell us what you want to get out of the function with name
StringToHex



George.
 
Don't let the unicode string fool you.
Just wanted to convert a string (with ansi chars) to two-byte hex notation.
Using a string for in and out is simply convienant.
The VB part has an Hex() function, c has value converters.


George Ter-Saakov said:
Sorry, did not understand from your code what is this function suppose to
do.
Please tell us what you want to get out of the function with name
StringToHex



George.

Edwin Knoppert said:
public static String StringToHex(String sString)
{
if (sString.Length == 0) { return ""; }
StringBuilder sb = new StringBuilder(sString.Length * 2);
Char[] chars = sString.ToCharArray();
String T = "";
foreach (Char c in chars)
{
T = "0" + Convert.ToString(c, 16).ToUpper();
sb.Append(T.Substring (T.Length -2 ));
}
return sb.ToString();
}
 
Back
Top