Converting snippett from VB.Net to C#

S

Steve Graddy

I am having a problem converting one line of code in this VB.Net Function.
I have marked the line below in its context. In the converted C# code I am
getting
a error stating "Input string was not in a correct format". Is there anyone
out there
that can shed some light on fixing this line.. I have included the entire
function so
that you may see the context.

The VB.Net line is : lChar = CInt("&H" & Mid(sMD5, (lCount * 2) - 1, 2))
Mod 32

The Converted C# is : lChar = Convert.ToInt32("&H" + Strings.Mid(sMD5,
(lCount * 2) - 1, 2)) % 32;

Thanks for any help sent this way...

Steve Graddy
(e-mail address removed)

********** CODE SNIPPETT
****************************************************
Private Const VALID_CHARS As String = "0123456789ABCDEFGHJKLMNPQRTUVWXY"
Private Const RANDOM_LOWER As Integer = 0
Private Const RANDOM_UPPER As Integer = 31

Public Function GenerateKey(ByRef sAppChars As String) As String

Dim lChar As Integer
Dim lCount As Integer
Dim sInitialChars As String
Dim oMD5 As CMD5
Dim sMD5 As String
Dim sKey As String


Randomize()


' We first generate 9 random characters that are members of VALID_CHARS
sInitialChars = ""

For lCount = 1 To 9
lChar = Int((RANDOM_UPPER - RANDOM_LOWER + 1) * Rnd() + RANDOM_LOWER)
sInitialChars = sInitialChars & Mid(VALID_CHARS, lChar + 1, 1)
Next


' We now get an MD5 of our initial chars plus out application chars
' The application chars should be different for each application to
' ensure that a key for one of our applications is not valid on another
' of our applications. If hackers know we are using this method for
' generating our keys we should ensure that the application character
' are very long to help prevent cracking.

oMD5 = New CMD5
sMD5 = oMD5.MD5(sInitialChars & sAppChars)
oMD5 = Nothing


' We now take each byte-pair from the MD5, convert it back to a byte
' value from the hex code, do a MOD 32, and then select the appropriate
' character from our VALID_CHARS

sKey = sInitialChars


For lCount = 1 To 16
lChar = CInt("&H" & Mid(sMD5, (lCount * 2) - 1, 2)) Mod 32 <--- This
is the line that I can not seem to convert to C#
sKey = sKey & Mid(VALID_CHARS, lChar + 1, 1)
Next

GenerateKey = sKey

End Function
****************************************************************************
*************
 
S

Sherif ElMetainy

Hello
Try this
lChar = Convert.ToInt32(Strings.Mid(sMD5, (lCount * 2) - 1, 2), 16) % 32

The second parameter to ToInt32 is telling .NET that the number is base 16.
The hex specifier is 0x not &H, but you don't need it if you specify base
16.

Best regards,

Sherif
 
N

Nicholas Paldino [.NET/C# MVP]

Steve,

I don't believe that ToInt32 will support the &H prepended to the
string. I believe instead, you should use 0x (to indicate that it is
represented in hexidecimal format).

Hope this helps.
 
J

Jeffrey Tan[MSFT]

Hi Steve,

Does the community's reply make sense to you?

Please feel free to feedback, thanks.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top