This could be shorter tight? (String to Hex)

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();
}
 
G

George Ter-Saakov

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.
 
E

Edwin Knoppert

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();
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top