|
1
.
|
/// <summary>
/// pads a string to the left with the specified total
/// </summary>
/// <param name="inString"></param>
/// <param name="totalWidth"></param>
/// <param name="paddingString"></param>
/// <returns></returns>
public static string PadLeft( string inString, int repeat, string paddingString )
{
string temp = "";
for ( int i=0; i < repeat; i ++ )
{
temp += paddingString;
}
inString = temp + inString;
return inString;
}
|
|
View Comments (2)
Add Rating
|
|
2
.
|
/// Merges a class with a template text, data place holders are wrapped around{}
public static string DataMergeTemplate(string template, object ClassObject)
{
Type t = ClassObject.GetType();
string newTemplate = template;
System.Reflection.FieldInfo[] finfos = t.GetFields();
foreach (System.Reflection.FieldInfo fifo in finfos)
{
string v = fifo.GetValue(ClassObject).ToString();
newTemplate = newTemplate.Replace("{" + fifo.Name.ToLower() + "}", v);
}
foreach ( System.Reflection.PropertyInfo pifo in t.GetProperties() )
{
string v = pifo.GetValue(ClassObject, null).ToString();
if (pifo.Name != null)
{
newTemplate = newTemplate.Replace("{" + pifo.Name.ToLower() + "}", v);
}
}
return newTemplate;
}
|
|
View Comments (0)
Add Rating
|
|
3
.
|
public static int HexToInt(String hexstr)
// This method converts a hexvalues string as 80FF into a integer.
// Note that you may not put a '#' at the beginning of string! There
// is not much error checking in this method. If the string does not
// represent a valid hexadecimal value it returns 0.
{
int counter,hexint;
char[] hexarr;
hexint=0;
hexstr=hexstr.ToUpper();
hexarr=hexstr.ToCharArray();
for (counter=hexarr.Length-1;counter>=0;counter--)
{
if ((hexarr[counter]>='0') && (hexarr[counter]<='9'))
{
hexint+=(hexarr[counter]-48)*((int)(Math.Pow(16,hexarr.Length-1-counter)));
}
else
{
if ((hexarr[counter]>='A') && (hexarr[counter]<='F'))
{
hexint+=(hexarr[counter]-55)*((int)(Math.Pow(16,hexarr.Length-1-counter)));
}
else
{
hexint=0;
break;
}
}
}
return hexint;
}
|
|
View Comments (1)
Add Rating
|
|
4
.
|
public static String IntToHex(int hexint)
// This method converts a integer into a hexadecimal string representing the
// int value. The returned string will look like this: 55FF. Note that there is
// no leading '#' in the returned string!
{
int counter,reminder;
String hexstr;
counter=1;
hexstr="";
while ( hexint + 15 > Math.Pow( 16, counter - 1 ) )
{
reminder = (int)( hexint % Math.Pow( 16, counter ) );
reminder = (int)( reminder / Math.Pow( 16, counter - 1 ) );
if (reminder<=9)
{
hexstr = hexstr + (char)( reminder + 48 );
}
else
{
hexstr = hexstr + (char) ( reminder + 55 );
}
hexint -= reminder;
counter ++;
}
return ReverseString( hexstr );
}
public static String IntToHex(int hexint,int length)
// This version of the IntToHex method returns a hexadecimal string representing the
// int value in the given minimum length. If the hexadecimal string is shorter then the
// length parameter the missing characters will be filled up with leading zeroes.
// Note that the returend string though is not truncated if the value exeeds the length!
{
String hexstr,ret;
int counter;
hexstr=IntToHex(hexint);
ret="";
if (hexstr.Length<length)
{
for (counter=0;counter<(length-hexstr.Length);counter++)
{
ret=ret+"0";
}
}
return ret+hexstr;
}
|
|
View Comments (0)
Add Rating
|
|
5
.
|
/// <summary>
/// Encodes non-US-ASCII characters in a string, good for encoding file names for download
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public static string ToHexString(string s)
{
char[] chars = s.ToCharArray();
StringBuilder builder = new StringBuilder();
for(int index=0; index<chars.Length; index++)
{
bool needToEncode = NeedToEncode(chars[index]);
if(needToEncode)
{
string encodedString = ToHexString(chars[index]);
builder.Append(encodedString);
}
else
{
builder.Append(chars[index]);
}
}
return builder.ToString();
}
/// <summary>
/// Determines if the character needs to be encoded.
/// </summary>
/// <param name="chr"></param>
/// <returns></returns>
private static bool NeedToEncode(char chr)
{
string reservedChars = "$-_.+!*'(),@=&";
if(chr>127)
return true;
if(char.IsLetterOrDigit(chr) || reservedChars.IndexOf(chr)>=0)
return false;
return true;
}
/// <summary>
/// Encodes a non-US-ASCII character.
/// </summary>
/// <param name="chr"></param>
/// <returns></returns>
private static string ToHexString(char chr)
{
UTF8Encoding utf8 = new UTF8Encoding();
byte[] encodedBytes = utf8.GetBytes(chr.ToString());
StringBuilder builder = new StringBuilder();
for(int index=0; index<encodedBytes.Length; index++)
{
builder.AppendFormat("%{0}", Convert.ToString(encodedBytes[index], 16));
}
return builder.ToString();
}
|
|
View Comments (0)
Add Rating
|