I'm slowly switching over from VB6 to C# and need help converting an
encrypted RC4 string to hex. The function below works perfectly within my
vb6 app but I'm having difficulty finding a replacement written in C#.
Converting to C# is beyond my skillset at the moment. Where can I find the
same function in C#?
Thank you
Function StrToHex(Text As String, Optional Separator As String = " ") As
String
Dim a As Long
Dim Pos As Long
Dim Char As Byte
Dim PosAdd As Long
Dim ByteSize As Long
Dim ByteArray() As Byte
Dim ByteReturn() As Byte
Dim SeparatorLen As Long
Dim SeparatorChar As Byte
'Initialize the hex routine
If (Not m_InitHex) Then Call InitHex
'Initialize variables
SeparatorLen = Len(Separator)
'Create the destination bytearray, this
'will be converted to a string later
ByteSize = (Len(Text) * 2 + (Len(Text) - 1) * SeparatorLen)
ReDim ByteReturn(Byte Size - 1)
Call FillMemory(Byte Return(0), ByteSize, Asc(Separator))
'We convert the source string into a
'byte array to speed this up a tad
ByteArray() = StrConv(Text, vbFromUnicode)
'Now convert every character to
'it's equivalent HEX code
PosAdd = 2 + SeparatorLen
For a = 0 To (Len(Text) - 1)
ByteReturn(Pos) = m_ByteToHex(Byt eArray(a), 0)
ByteReturn(Pos + 1) = m_ByteToHex(Byt eArray(a), 1)
Pos = Pos + PosAdd
Next
'Convert the bytearray to a string
StrToHex = StrConv(ByteRet urn(), vbUnicode)
End Function
Private Sub InitHex()
Dim a As Long
Dim b As Long
Dim HexBytes() As Byte
Dim HexString As String
'The routine is initialized
m_InitHex = True
'Create a string with all hex values
HexString = String$(512, "0")
For a = 1 To 255
Mid$(HexString, 1 + a * 2 + -(a < 16)) = Hex(a)
Next
HexBytes = StrConv(HexStri ng, vbFromUnicode)
'Create the Str->Hex array
For a = 0 To 255
m_ByteToHex(a, 0) = HexBytes(a * 2)
m_ByteToHex(a, 1) = HexBytes(a * 2 + 1)
Next
'Create the Str->Hex array
For a = 0 To 255
m_HexToByte(m_B yteToHex(a, 0), m_ByteToHex(a, 1)) = a
Next
End Sub
encrypted RC4 string to hex. The function below works perfectly within my
vb6 app but I'm having difficulty finding a replacement written in C#.
Converting to C# is beyond my skillset at the moment. Where can I find the
same function in C#?
Thank you
Function StrToHex(Text As String, Optional Separator As String = " ") As
String
Dim a As Long
Dim Pos As Long
Dim Char As Byte
Dim PosAdd As Long
Dim ByteSize As Long
Dim ByteArray() As Byte
Dim ByteReturn() As Byte
Dim SeparatorLen As Long
Dim SeparatorChar As Byte
'Initialize the hex routine
If (Not m_InitHex) Then Call InitHex
'Initialize variables
SeparatorLen = Len(Separator)
'Create the destination bytearray, this
'will be converted to a string later
ByteSize = (Len(Text) * 2 + (Len(Text) - 1) * SeparatorLen)
ReDim ByteReturn(Byte Size - 1)
Call FillMemory(Byte Return(0), ByteSize, Asc(Separator))
'We convert the source string into a
'byte array to speed this up a tad
ByteArray() = StrConv(Text, vbFromUnicode)
'Now convert every character to
'it's equivalent HEX code
PosAdd = 2 + SeparatorLen
For a = 0 To (Len(Text) - 1)
ByteReturn(Pos) = m_ByteToHex(Byt eArray(a), 0)
ByteReturn(Pos + 1) = m_ByteToHex(Byt eArray(a), 1)
Pos = Pos + PosAdd
Next
'Convert the bytearray to a string
StrToHex = StrConv(ByteRet urn(), vbUnicode)
End Function
Private Sub InitHex()
Dim a As Long
Dim b As Long
Dim HexBytes() As Byte
Dim HexString As String
'The routine is initialized
m_InitHex = True
'Create a string with all hex values
HexString = String$(512, "0")
For a = 1 To 255
Mid$(HexString, 1 + a * 2 + -(a < 16)) = Hex(a)
Next
HexBytes = StrConv(HexStri ng, vbFromUnicode)
'Create the Str->Hex array
For a = 0 To 255
m_ByteToHex(a, 0) = HexBytes(a * 2)
m_ByteToHex(a, 1) = HexBytes(a * 2 + 1)
Next
'Create the Str->Hex array
For a = 0 To 255
m_HexToByte(m_B yteToHex(a, 0), m_ByteToHex(a, 1)) = a
Next
End Sub
Comment