Hex Conversion and Calculations

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am trying to create an encryption routine (although VERY basic I know), but
I am getting a "input string was not in a correct format" error and not sure
what I am doing wrong. All my variables are int. iNumber is the value
passed into the function. The error is happening on the first line of the
code posted below.

int iHex1 = (int)System.Convert.ToUInt32("&H10000000");
int iHex2 = (int)System.Convert.ToUInt32("&H100000");
..
..
..
iXORValue = iNumber ^ iKey;
iCheckSumTemp = iXORValue / iHex1;
iCheckSum = iCheckSumTemp % iHex1;
..
..
..
return iXORValue.ToString() + "-" + iCheckSum.ToString();

Thanks for any help
Andy
 
Andy said:
I am trying to create an encryption routine (although VERY basic I know), but
I am getting a "input string was not in a correct format" error and not sure
what I am doing wrong. All my variables are int. iNumber is the value
passed into the function. The error is happening on the first line of the
code posted below.

int iHex1 = (int)System.Convert.ToUInt32("&H10000000");
int iHex2 = (int)System.Convert.ToUInt32("&H100000");

Here's the error! Instead you should have:

int iHex1 = (int)System.Convert.ToUInt32("0xH10000000",16);
int iHex2 = (int)System.Convert.ToUInt32("0xH100000",16);

First, in c# you start a hex number from 0x, and second, you need to specify the number base,
which is 16.

God luck!
Andrey
 
int iHex1 = (int)System.Convert.ToUInt32("0xH10000000",16);
int iHex2 = (int)System.Convert.ToUInt32("0xH100000",16);
^
Get rid of the H too.

First, in c# you start a hex number from 0x,

Right, but that doesn't really matter here since you're passing in a
string. So the calling language is irrelevant, what matters is that
the Convert APIs expect a 0x prefix.



Mattias
 
Back
Top