Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7832131
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T12:03:26+00:00 2026-06-02T12:03:26+00:00

Here is a much more short version of my code: char token[256]; unsigned int

  • 0

Here is a much more short version of my code:

char token[256];
unsigned int maskval = 0;

//Token gets read in here.

printf("token: %s\n", token);

if (sscanf(token + 2, "%x", &maskval) == 1) {
     //do stuff
} else {
     printf("error: %d, %x\n", maskval, maskval);
}

As you can see I am passing hexadecimal strings to the function, reading them into token and using sscanf() to convert the string version of the hexadecimal value into a unsigned int. The string token always begins with “0x”, “0X”, “Ox”, or “OX”. The reason for this is because the user will be unable to tell the difference between a 0 or a O when inputting the value (vi displays them the same, graphical). The result I get if I pass it “0x00EE” is:

token: 0x00EE
error: 0

The problem I believe is whether I pass it something along the lines of “0x00EE” or “OxOOEE” (zero’s vs capital o’s). Both look identical in vi, and I am not sure if they are handled differently in this scenario. Hopefully I am just doing something really simple and stupid that I overlooked though (that’s usually the case). Also, I have tried using

maskval = strtol(token, NULL, 16);

instead of sscanf() but I get the exact same results.

Any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T12:03:28+00:00Added an answer on June 2, 2026 at 12:03 pm

    Here’s one mistake:

    char *token[256];
    

    This is a an array of 256 pointers to character (char *), not an array of characters.

    You meant:

    char token[256];
    

    There should be plenty of compiler warnings from this.

    Trying to parse a hexadecimal value from a string beginning something that is not a valid hexadecimal digit will of course fail, for both sscanf() and strtoul(). You almost sound as if you think the standard functions should consider 'O' to be hex just because it looks like the digit 0 for you. That is … not how they’ve been designed, which I think is fortunate. 🙂 You might want to look into changing your fonts.

    You could manually strip the non-conforming prefix. Assuming you always have the 2-character prefix, and then might have one, you could do:

    const char *number = token + 2;
    
    while(!isxdigit(*number))
      number++;
    /* Now number will point at the first hexadecimal
       digit in token, or '\0' if none were found.
    */
    if(sscanf(number, "%x", &maskval) == 1)
      printf("success, got %x\n", maskval);
    else
      printf("failed to parse '%s' as hexadecimal number\n", number);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Code is much more precise than English; Here's what I'd like to do: import
Here's a much-abbreviated sample of my code. <Grid> <Polygon Name=ply Grid.Column=2 Grid.Row=0 Grid.RowSpan=3 Fill=Orange
Here's the situation: I've got a class that is doing too much. It's mainly
Example here: http://jezebel.com/5896408/racist-hunger-games-fans-dont-care-how-much-money-the-movie-made Click on the 3x3 Tweets screencap they have up. I love
I've looked around at pretty much all the SMS posts here on SO and
Hibernate 3.5-Final is finally here and it offers the much anticipated JPA2 support, amongst
I'll post a link since theres to much to post here: http://hem.bredband.net/noor/canvas.htm My goal
To provide as much information as I can, here's a very basic example of
I tried searching here , but it couldn't help me much .. I want
It has been done to death pretty much, here on SO and around the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.