Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Compare Hex to char in If Statement

Posted on 2007-11-19
15
Medium Priority
?
1,292 Views
Last Modified: 2010-05-18
I am trying to use a char to a hex in an if statement:

if (Convert.ToByte(ItemIndexNumber.Substring(ItemIndexNumber.Length - 1, 1)) < 0x41)
{
        //---//
}

But I keep getting "Input string was not in a correct format."
0
Comment
Question by:SaltyDawg
  • 9
  • 5
15 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20315199
the problem is in the conversion, not in the comparison:

Convert.ToByte(ItemIndexNumber.Substring(ItemIndexNumber.Length - 1, 1))

What does ItemIndexNumber contains? It should have some non-decimal number
0
 
LVL 1

Author Comment

by:SaltyDawg
ID: 20315260
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?
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20315436
For this value:
50009912000000001B

ItemIndexNumber.Substring(ItemIndexNumber.Length - 1, 1))
is:
0009912000000001B

I suspect to want to do this:
ItemIndexNumber.Substring(ItemIndexNumber.Length - 1, 0))
with result:
50009912000000001
 
anyway, this number is too big to convert to a Byte
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 1

Author Comment

by:SaltyDawg
ID: 20315458
Actually I'm was just trying to get "B" the last character.
0
 
LVL 1

Author Comment

by:SaltyDawg
ID: 20315679
I can manage to get "B" with:

if (Convert.ToInt32(ItemIndexNumber.Substring(ItemIndexNumber.Length - 1, 1)) < 0x41)

But again I can't get the conversion right. I've tried all the Ints and related Coversions.
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20315688
Sorry, just noticed. There are other issues:

"B" cannot be converted by:
Convert.ToByte(somenumber)
You should use:
Convert.ToByte(somenumber,16)
0
 
LVL 1

Author Comment

by:SaltyDawg
ID: 20315726
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(ItemIndexNumber.Substring(ItemIndexNumber.Length - 1, 1)) < 0x41)
{

}

0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 2000 total points
ID: 20315804
You should use the 2 argument version, to specify that the number is hexadecimal:

if (Convert.ToInt32(ItemIndexNumber.Substring(ItemIndexNumber.Length - 1, 1), 16) < 0x41)
{
}
0
 
LVL 7

Expert Comment

by:photowhiz
ID: 20316524
Why not do this:

if (ItemIndexNumber[ItemIndexNumber.Length - 1] < 'B')
0
 
LVL 1

Author Comment

by:SaltyDawg
ID: 20319355
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.
0
 
LVL 1

Author Comment

by:SaltyDawg
ID: 20319568
You should use the 2 argument version, to specify that the number is hexadecimal:

if (Convert.ToInt32(ItemIndexNumber.Substring(ItemIndexNumber.Length - 1, 1), 16) < 0x41)
{
}

Thanks Jaime, that makes sense now, and it works.
0
 
LVL 1

Author Comment

by:SaltyDawg
ID: 20319584
Look for my name again I'm sure I'll have more C# questions as I make this switch from C++ and Java.
0
 
LVL 1

Author Comment

by:SaltyDawg
ID: 20323029
FYI

I actually went through all that when all I had to do was this:

            if (Convert.ToChar(ItemIndexNumber.Substring(ItemIndexNumber.Length - 1, 1)) < 0x41)
            {
                is_probe = false;
            }
            else
            {
                is_probe = true;
            }

while ItemIndexNumber.Substring(ItemIndexNumber.Length - 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.
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20323096
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(ItemIndexNumber.Substring(ItemIndexNumber.Length - 1, 1)) < 0x41);
0
 
LVL 1

Author Comment

by:SaltyDawg
ID: 20323185
Wow thanks again, that works.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

927 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question