SaltyDawg
asked on
Compare Hex to char in If Statement
I am trying to use a char to a hex in an if statement:
if (Convert.ToByte(ItemIndexN umber.Subs tring(Item IndexNumbe r.Length - 1, 1)) < 0x41)
{
//---//
}
But I keep getting "Input string was not in a correct format."
if (Convert.ToByte(ItemIndexN
{
//---//
}
But I keep getting "Input string was not in a correct format."
ASKER
Yes, I've just determined the problem is not the comparison, so I've got that much. But as you said its in the conversion and I can't figure that out.
ItemIndexNumber contains a string value (example "50009912000000001B")
What do you mean non-decimal number?
ItemIndexNumber contains a string value (example "50009912000000001B")
What do you mean non-decimal number?
For this value:
50009912000000001B
ItemIndexNumber.Substring( ItemIndexN umber.Leng th - 1, 1))
is:
0009912000000001B
I suspect to want to do this:
ItemIndexNumber.Substring( ItemIndexN umber.Leng th - 1, 0))
with result:
50009912000000001
anyway, this number is too big to convert to a Byte
50009912000000001B
ItemIndexNumber.Substring(
is:
0009912000000001B
I suspect to want to do this:
ItemIndexNumber.Substring(
with result:
50009912000000001
anyway, this number is too big to convert to a Byte
ASKER
Actually I'm was just trying to get "B" the last character.
ASKER
I can manage to get "B" with:
if (Convert.ToInt32(ItemIndex Number.Sub string(Ite mIndexNumb er.Length - 1, 1)) < 0x41)
But again I can't get the conversion right. I've tried all the Ints and related Coversions.
if (Convert.ToInt32(ItemIndex
But again I can't get the conversion right. I've tried all the Ints and related Coversions.
Sorry, just noticed. There are other issues:
"B" cannot be converted by:
Convert.ToByte(somenumber)
You should use:
Convert.ToByte(somenumber, 16)
"B" cannot be converted by:
Convert.ToByte(somenumber)
You should use:
Convert.ToByte(somenumber,
ASKER
I was not sure if Convert.ToByte is what I was supposed to use, can you clarify that?
Whatever I need to make this if statement work:
if (Convert.ToInt32(ItemIndex Number.Sub string(Ite mIndexNumb er.Length - 1, 1)) < 0x41)
{
}
Whatever I need to make this if statement work:
if (Convert.ToInt32(ItemIndex
{
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Why not do this:
if (ItemIndexNumber[ItemIndex Number.Len gth - 1] < 'B')
if (ItemIndexNumber[ItemIndex
ASKER
photowhiz,
Wouldn't that make it an array? What I am trying to do is find the value for "B" then do a comparison statement with the hex value of 0x41.
Wouldn't that make it an array? What I am trying to do is find the value for "B" then do a comparison statement with the hex value of 0x41.
ASKER
You should use the 2 argument version, to specify that the number is hexadecimal:
if (Convert.ToInt32(ItemIndex Number.Sub string(Ite mIndexNumb er.Length - 1, 1), 16) < 0x41)
{
}
Thanks Jaime, that makes sense now, and it works.
if (Convert.ToInt32(ItemIndex
{
}
Thanks Jaime, that makes sense now, and it works.
ASKER
Look for my name again I'm sure I'll have more C# questions as I make this switch from C++ and Java.
ASKER
FYI
I actually went through all that when all I had to do was this:
if (Convert.ToChar(ItemIndexN umber.Subs tring(Item IndexNumbe r.Length - 1, 1)) < 0x41)
{
is_probe = false;
}
else
{
is_probe = true;
}
while ItemIndexNumber.Substring( ItemIndexN umber.Leng th - 1, 1) = "A"
I was orginally trying to compare 0x41 to "A" as a string, but I was getting errors because I could only compare 0x41 to 'A' as a string.
I actually went through all that when all I had to do was this:
if (Convert.ToChar(ItemIndexN
{
is_probe = false;
}
else
{
is_probe = true;
}
while ItemIndexNumber.Substring(
I was orginally trying to compare 0x41 to "A" as a string, but I was getting errors because I could only compare 0x41 to 'A' as a string.
indeed it is 'A' as a character, as far as Convert.ToChar() returns a character too.
I am not sure what are you trying to do but your code could be simpler:
is_probe = !(Convert.ToChar(ItemIndex Number.Sub string(Ite mIndexNumb er.Length - 1, 1)) < 0x41);
I am not sure what are you trying to do but your code could be simpler:
is_probe = !(Convert.ToChar(ItemIndex
ASKER
Wow thanks again, that works.
Convert.ToByte(ItemIndexNu
What does ItemIndexNumber contains? It should have some non-decimal number