Newbie question here.
I want to covert single bytes from an array into hex.
sHex = arrBytes[i].ToString("X");
gives me the hex string but I want it formatted like "03" not "3". Is there
an easy way of doing this or do I have to test the length of sHex and add
the preceding zero myself?
Thanks in advance
Rob
Try
.PadLeft(2, '0')
--
Michael Culley
"Robert" <r...@hotmail.co.uk> wrote in message
news:ew1DrY7M...@tk2msftngp13.phx.gbl...
"Michael Culley" <mcu...@NOSPAMoptushome.com.au> wrote in message
news:uvJNxm7M...@tk2msftngp13.phx.gbl...
/// <summary>
/// Converts binary OID value into a readable string.
/// </summary>
/// <param name="binValue">binary value to
convert</param>
/// <returns>simple converted string from binary
value</returns>
public static string BinToString(object binValue)
{
char[] hexCode =
{'0','1','2','3','4','5','6','7','8','9','A','B','C','D','
E','F'};
byte[] oid = (byte[])binValue;
StringBuilder sb = new StringBuilder();
foreach (byte b in oid)
{
sb.Append(Convert.ToString(hexCode[b >> 4]));
sb.Append(Convert.ToString(hexCode[b & 0xF]));
}
return "0x" + sb.ToString();
}
/// <summary>
/// Converts binary OID value into a readable string.
/// </summary>
/// <param name="binValue">binary value to
convert</param>
/// <returns>simple converted string from binary
value</returns>
public static object StringToBin(string strValue)
{
//remove 0x from OID
string trimOID = strValue.Substring(2, 16);
byte bTemp;
int iTemp;
int iCnt = 0;
byte[] hexArray = new byte[8];
//convert every 2 hex characters to byte
for (int a=0; a<=trimOID.Length -1; a++)
{
if ((a % 2) == 0)
{
iTemp = int.Parse(trimOID.Substring(a, 2),
NumberStyles.AllowHexSpecifier);
bTemp = Convert.ToByte(iTemp);
hexArray[iCnt] = bTemp;
iCnt++;
}
}
You'll need a 'using' reference to the
System.Globalization namespace to use NumberStyles
enumerator.
>.
>
"Robert" <r...@hotmail.co.uk> wrote in message news:ew1DrY7M...@tk2msftngp13.phx.gbl...
or
string str = "F";
result = ((byte)str[0]).ToString("X2");
------------------------------------------------------
X format explanation
X2 = 00
X4 = 0000
etc...
"Robert" <r...@notmail.com> a écrit dans le message de news:
#1MtsP8M...@TK2MSFTNGP11.phx.gbl...