Why would the character 'A' be compared with 0x41?

C++String

C++ Problem Overview


I was looking at some C++ code and found the following construct:

if('A' == 0x41) {
  // ...
} else if('A' == 0xc1) {
  // ...
} else {
  // ...
}

I get a Visual Studio warning saying:

> Warning C4127 conditional expression is constant.

Visual Studio is clearly right - surely 'A' is defined to be 0x41. Why is the author writing this code, given that two out of the three branches are dead code?

C++ Solutions


Solution 1 - C++

0xc1 is the EBCDIC character set code for A. The author is testing for such a machine.

http://www.ibm.com/support/knowledgecenter/en/SSGH4D_15.1.3/com.ibm.xlf1513.aix.doc/language_ref/asciit.html

Solution 2 - C++

At first sight might look like that is dead code but 'A' == 0x41 not always will return true..

what the developer tried to do here is lazily find what encoding is the machine implementing ASCII or any variant of EBCDIC

as @Richard suggested Capital a is mapped to 0xc1 in the International - Extended Binary Coded Decimal Interchange Code see table below in the 2 branch of the if else...

enter image description here

another different value could be found by ASCII for exmaple:

enter image description here

he could as well have done:

if('p' == 0x70) {
  // ...
} else if('p' == 0x97) {
  //...
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionH BellamyView Question on Stackoverflow
Solution 1 - C++Richard HodgesView Answer on Stackoverflow
Solution 2 - C++ΦXocę 웃 Пepeúpa ツView Answer on Stackoverflow