C# "Hex" function equiv...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sensamecca
    New Member
    • Mar 2007
    • 2

    #1

    C# "Hex" function equiv...

    I need to convert single bytes to their Hex equivalent in C#. This means a 2 character representation in a string that is the hex number. This is done with the Hex function in VB to which there appears to be no equivalent in C#.

    So if I had an array of bytes and I was going through a loop (lets say byte[] b = new byte[4])

    ... Fill the byte array with various values

    and when doing something like

    for(int i=0; i<b.Length; i++)
    // convert it here

    End up with...

    "0a ff 00 1b"

    depending on what the values were that I loaded into the byte array.

    Anyone have some code to do this?

    The VB code I'm converting is:

    Public Function HexString(ByVal b() As Byte, ByVal l As Integer, ByVal offset As Integer) As String
    Dim i As Integer
    If l = 0 Then l = b.Length
    Dim str As String = ""
    For i = offset To l - 1 + offset
    str = str & " " & Hex(b(i))
    Next
    Return str
    End Function

    Thanks,
    D
  • sensamecca
    New Member
    • Mar 2007
    • 2

    #2
    Brahahahhs..

    byte[] b = new byte[4];

    String feh = b[i].ToString("X2") ;

    this seems to work great.

    Comment

    Working...