Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Byte to Hex

1 view
Skip to first unread message

Robert

unread,
Jun 15, 2003, 11:19:59 PM6/15/03
to
Hi all

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


Michael Culley

unread,
Jun 15, 2003, 11:49:20 PM6/15/03
to
Robert,

Try
.PadLeft(2, '0')

--
Michael Culley


"Robert" <r...@hotmail.co.uk> wrote in message
news:ew1DrY7M...@tk2msftngp13.phx.gbl...

Robert

unread,
Jun 16, 2003, 12:58:28 AM6/16/03
to
Thanks


"Michael Culley" <mcu...@NOSPAMoptushome.com.au> wrote in message
news:uvJNxm7M...@tk2msftngp13.phx.gbl...

V. Jenks

unread,
Jun 16, 2003, 1:56:42 AM6/16/03
to
I recently had this question come to me as I needed to
convert a byte array into a 16 character hexidecimal
value and also be able to re-convert it back into a byte
array, here are those two functions:


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

>.
>

Phil Jenson

unread,
Jun 16, 2003, 6:47:20 AM6/16/03
to
To get a leading zero simply specify the number of digits you require in the formatting parameter, as in
 
    sHex = arrBytes[i].ToString("X2");
will give 03
 
    sHex = arrBytes[i].ToString("X4");
 
will give 0003

 
Phil...
 

Derek Tremblay

unread,
Jun 16, 2003, 9:25:29 AM6/16/03
to

sHex = arrBytes[i].ToString("X2");

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

0 new messages