Le réseau pour les développeurs > Forums - Accueil > Visual C# General > Cannot use a 0xHEX int value from a parsed string
Poser une questionPoser une question
 

TraitéeCannot use a 0xHEX int value from a parsed string

  • mardi 3 novembre 2009 16:50_Yoshi_ Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     A du code
    I've got a problem..
    I want to have some 0xHEX value, with is user defined.
    The HEX code is a Color object with is ColorTranslated to a Html code (Hex).
    Before I use it, I will save the Html output into a string, so I can use it later (Have to be a string for the server).
    Thats Html thing, does not contain the 0x part..
    I tried:
    --- c[2] is the HEX string value ---
    1:
    
    int.Parse("0x"+c[2]);
    
    2:
    
    Convert.ToInt32("0x"+c[2]);
    
    3:
    
    Convert.ToInt32("0x"+c[2], 16);
    
    4:
    
    Convert.ToInt32(c[2]);
    
    5:
    
    Convert.ToInt32(c[2], 16);
    
    Ouch, giveup.
    

    Note: I will HAVE to use 0xHEXHEX  becouse that's the only supported method.

    How can I fix this?

Réponses

  • jeudi 5 novembre 2009 23:33David M MortonMVP, ModérateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     Traitée
    int value = ColorTranslator.FromHtml(htmlString).ToArgb();

    to convert back:

    string html = ColorTranslator.ToHtml(Color.FromArgb(value));

    If you're going to put it on HTML, it makes no difference whether it shows up as "#FF0000" or "Red".  The browser will know the color by name and translate accordingly. 

    Do me a favor before you post anything else. 

    Look up System.Drawing.Color on MSDN.  Go to the "members" link on the left, that will show you all of the members of color.  Read all the summaries of the methods.  

    Next, do the same for System.Drawing.ColorTranslator. 

    This is the kind of problem that comes up all the time in programming.  Your job is to figure out how to put those methods together and get the values in the format you need.  It's all there for you, but you need to learn to hunt it down.  I bet you can.  :)

    Coding Light - Illuminated Ideas and Algorithms in Software
    Coding Light WikiLinkedInForumsBrowser
    • Marqué comme réponse_Yoshi_ vendredi 6 novembre 2009 13:00
    •  

Toutes les réponses

  • mardi 3 novembre 2009 16:55David M MortonMVP, ModérateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
  • mardi 3 novembre 2009 17:09_Yoshi_ Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    Error    1    The name 'NumberStyles' does not exist in the current context    ...    337    89    ...

    What's wrong...
  • mardi 3 novembre 2009 17:10David M MortonMVP, ModérateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    You need "using System.Globalization;" at the top of your code.

    Put your cursor in the word "NumberStyles".  Press Ctrl+. (ctrl+<period>).  Press enter.
    Coding Light - Illuminated Ideas and Algorithms in Software
    Coding Light WikiLinkedInForumsBrowser
  • mardi 3 novembre 2009 17:24_Yoshi_ Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    The last step won't do anything..
    And I've got 3 errors instead of one now.. I think it's about the last step :).
  • mardi 3 novembre 2009 17:26David M MortonMVP, ModérateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    Those weren't steps.  They were two methods to get at the same thing.

    Post your code and the errors you're getting.
    Coding Light - Illuminated Ideas and Algorithms in Software
    Coding Light WikiLinkedInForumsBrowser
  • mardi 3 novembre 2009 18:56Tergiver Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    It sounds like you manually added "using System.Globalization", then put the cursor on "NumberStyles", pressed Ctrl+. and pressed Enter.

    Doing that (in that order) would result in a line-break in the middle of the identifier "NumberStyles", which would indeed produce errors.

    It was a one or the other thing.


    The full-name of NumberStyles is "System.Globalization.NumberStyles". This is quite onerous to type, so we add:

    using System.Globalization;

    to the top of the source file. Now when the compiler sees "NumberStyles" (an incomplete type name), it looks at all the using statements at the top of the file and tries pre-pending each of them to see if one will result in a complete type name (that's a bit of a simplification).

    That's the purpose of the 'using' statement. It allows us to abbreviate type names. Most C# file have:

    using System;

    at the top. That's because a great number of the types we use are actually abbreviations for System.Something.

    Anyhow, there are two ways to resolve incomplete type names (where you know the type is in a referenced assembly, but didn't specify the full type name and the compiler complains that 'SuchAndSuch' does not exist in the current context):

    1) Add a using statement at the top by typing it. This requires that you know the full type name to begin with.
    2) Move the cursor to the abbreviated name and notice that a red underline appears under the last letter. That underline is actually a little widget you can hover the mouse over. Doing so gives you a drop-down button. Clicking on the button opens a menu of choices. In the simplest case there will be two options. I'll use the NumberStyles example here:

    "using System.Globalization;"
    "System.Globalization.NumberStyles"

    Selecting the first option will add a using statement to the top of the file. Now you can abbreviate all the types in that namespace.
    Selecting the second option will change the text "NumberStyles" to "System.Globalization.NumberStyles", giving the full type name in that location only.


    David told you to press Ctrl+.  This is a keyboard shortcut that allows you to invoke the drop-down without using the mouse. Very handy when you have both hands on the keyboard and are typing fast.

  • mardi 3 novembre 2009 19:33_Yoshi_ Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    As I see now, the using will work fine, because it finds it.
    But it will NOT work in combination with the Convert.ToInt32...
  • mardi 3 novembre 2009 19:35David M MortonMVP, ModérateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    As I see now, the using will work fine, because it finds it.
    But it will NOT work in combination with the Convert.ToInt32...

    Sorry, I apologize profusely.  It should have been

    Int32.Parse(c[2], NumberStyles.HexNumber);

    Not

    Convert.ToInt32(c[2], NumberStyles.HexNumber);

    My fault.
    Coding Light - Illuminated Ideas and Algorithms in Software
    Coding Light WikiLinkedInForumsBrowser
  • mardi 3 novembre 2009 20:51_Yoshi_ Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    Aha :P.

    I will try tomorrow when I'm back on :).
  • mardi 3 novembre 2009 22:49Tergiver Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    I missed that too. I even wrote a little app (well, added a line of code to something I was working on) to see what happens if you press Ctrl+. and Enter when the type name is already resolved. However, I subconsciously used int.Parse rather than Convert.ToInt32.
  • mercredi 4 novembre 2009 19:25_Yoshi_ Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    Fine:

    System.FormatException was unhandled
      Message="De indeling van de invoertekenreeks is onjuist."
      Source="mscorlib"
      StackTrace:
    ......................



    I use: Int32.Parse(c[2], NumberStyles.HexNumber)

    What now?
  • mercredi 4 novembre 2009 19:26David M MortonMVP, ModérateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
  • mercredi 4 novembre 2009 19:48Tergiver Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     A du code
    Use int.TryParse instead.

    int value;
    if (int.TryParse(c[2], NumberStyles.HexNumber, CultureInfo.CurrentCulture, out value))
    {
        // value contains a valid number
    }
    else
    {
        // input string was not a valid hexidecimal number, deal with the error
    }
    
    
  • jeudi 5 novembre 2009 16:41_Yoshi_ Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    I don't think tryparse will work in case.
    First the user is able to select a simple color from the standart mixer. That color will be 'stored' in a button's FlatStyle.BorderColor prop..
    Later, the user press a button.
    Then I get the translate using the ColorTranslator - the BorderColor to some 'hex' code and ToString() it.

    I will try to print the value of c[2]...
  • jeudi 5 novembre 2009 16:55_Yoshi_ Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    Ok, this is the horrible result.
    I use this code:

    ColorTranslator.ToHtml(selectcolor.FlatAppearance.BorderColor).ToString()

    with will result in a print of the color's name, like Red, Maroon etc.. But I want a color value :Q.
  • jeudi 5 novembre 2009 17:06David M MortonMVP, ModérateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    I think you're going about this all wrong.  Skip the whole Int.Parse thing completely.  If you want to store the value as an integer use this:

    int colorValue = selectcolor.FlatAppearance.BorderColor.ToArgb();

    To get it back:

    Color color = Color.FromArgb(colorValue);

    To convert it to an HTML representation:

    string htmlColor = ColorTranslator.ToHtml(color);

    You can also use the ColorTranslator methods that use ToWin32 and FromWin32.

    Experiment a bit.  Int32.Parse and Convert.ToInt32 should have nothing to do with this.  You have more than enough methods at your disposal to avoid these.
    Coding Light - Illuminated Ideas and Algorithms in Software
    Coding Light WikiLinkedInForumsBrowser
  • jeudi 5 novembre 2009 17:18_Yoshi_ Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    Yes, that worked also before, but now I HAVE TO convert it to a string to recover it back. I'm never able to store int's or other values in my program..
    I think I still need that then.

    And on some really scary way (I didn't did anything actually, but it worked after 2 times :Q), now I get printed #0A76A8 :D.

    Still I get an error at this part: Int32.Parse(c[2],NumberStyles.HexNumber).. It said that the number collection is incorrect.
  • jeudi 5 novembre 2009 17:21_Yoshi_ Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    --- edit ---
    Problem.. :(

    Sometimes it will return a word, and sometimes it returns a real hex code.. What to do now?
  • jeudi 5 novembre 2009 23:33David M MortonMVP, ModérateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     Traitée
    int value = ColorTranslator.FromHtml(htmlString).ToArgb();

    to convert back:

    string html = ColorTranslator.ToHtml(Color.FromArgb(value));

    If you're going to put it on HTML, it makes no difference whether it shows up as "#FF0000" or "Red".  The browser will know the color by name and translate accordingly. 

    Do me a favor before you post anything else. 

    Look up System.Drawing.Color on MSDN.  Go to the "members" link on the left, that will show you all of the members of color.  Read all the summaries of the methods.  

    Next, do the same for System.Drawing.ColorTranslator. 

    This is the kind of problem that comes up all the time in programming.  Your job is to figure out how to put those methods together and get the values in the format you need.  It's all there for you, but you need to learn to hunt it down.  I bet you can.  :)

    Coding Light - Illuminated Ideas and Algorithms in Software
    Coding Light WikiLinkedInForumsBrowser
    • Marqué comme réponse_Yoshi_ vendredi 6 novembre 2009 13:00
    •  
  • vendredi 6 novembre 2009 12:59_Yoshi_ Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    Wow..
    I just think I switched Argb with Html.
    With converting it to Argb it WILL work properly always :D.

    The summaries are very useful, even that whole libary of stuff O_O. Thanks.

    Still one problem releases that all colors are switched (red is switched with blue etc.), but I think I'm able to solve that myself. Thank you.