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

  • SEARCH
  • Home
  • 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 5982815
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:08:28+00:00 2026-05-22T22:08:28+00:00

I was getting some odd behaviour out of a switch block today, specifically I

  • 0

I was getting some odd behaviour out of a switch block today, specifically I was reading a byte from a file and comparing it against certain hex values (text file encoding issue, no big deal). The code looked something like:

char BOM[3] = {0};
b_error = ReadFile (iNCfile, BOM, 3, &lpNumberOfBytesRead, NULL); 

switch ( BOM[0] ) {
case 0xef: {
    // Byte Order Marker Potentially Indicates UTF-8
    if ( ( BOM[1] == 0xBB ) && ( BOM[2] == 0xBF ) ) {
        iNCfileEncoding = UTF8;
    }
    break;
           }
}

Which didn’t work, although the debug looked ok. I realized that the switch was promoting the values to integers, and once that clicked in place I was able to match using 0xffffffef in the case statement. Of course the correct solution was to make BOM[] unsigned and now everything promotes and compares as expected.

Can someone briefly explain what was going on in the char -> int promotion that produced 0xffffffef instead of 0x000000ef?

  • 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-05-22T22:08:28+00:00Added an answer on May 22, 2026 at 10:08 pm

    “Can someone briefly explain what was
    going on in the char -> int promotion
    that produced 0xffffffef instead of
    0x000000ef?”

    Contrary to the four answers so far, it didn’t.

    Rather, you had a negative char value, which as a switch condition was promoted to the same negative int value as required by

    C++98 §6.4.2/2
    Integral promotions are performed.

    Then with your 32-bit C++ compiler 0xffffffef was interpreted as an unsigned int literal, because it’s too large for a 32-bit int, by

    C++98 2.13.1/2
    If it is octal or hexadecimal and has no suffix, it has the first of these types in
    which it can be represented: int, unsigned int, long int, unsigned long int.

    Now, for the case label,

    C++98 §6.4.2/2
    The integral constant-expression (5.19) is implicitly converted to the promoted
    type of the switch condition.

    In your case, with signed destination type, the result of the conversion is formally implementation-defined, by

    C++98 §4.7/3
    If the destination type is signed, the value is unchanged if it can be represented
    in the destination type (and bit-field width); otherwise, the value is
    implementation-defined.

    But in practice nearly all compilers use two’s complement representation with no trapping, and so the implementation defined conversion is in your case that the bitpattern 0xffffffef is interpreted as two’s complement specification of a negative value. You can calculate which value by 0xffffffef – 232, because we’re talking 32-bit representation here. Or, since this is just an 8-bit value that’s been sign extended to 32 bits, you can alternatively calculate it as 0xef – 28, where 0xef is the character code point.

    Cheers & hth.,

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using a Dictionary<TKey, TValue> and I'm getting some odd, though somewhat understandable behaviour
I've been getting some odd behavior using a foreach today. I have a dataset
I'm getting some very odd behavior and I can't figure it out. I've modified
I'm getting some strange behaviour from EF Code First when I add an object
So I'm getting some interesting behaviour from some filters stacked within a for loop.
I'm getting some objects from an external library and I need to store those
I'm getting some really wierd linking errors from a class I wrote. I am
I'm getting some weird behaviour recompiling some applications in 2009 that used widestrings at
I'm working on a school project and I'm getting some weird errors from Xcode.
I have a rails time-based query which has some odd timezone sensitive behaviour, even

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.