Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

string

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?

like image 223
H Bellamy Avatar asked Nov 14 '16 15:11

H Bellamy


2 Answers

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

like image 60
Richard Hodges Avatar answered Nov 19 '22 01:11

Richard Hodges


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) {
  //...
}
like image 45
ΦXocę 웃 Пepeúpa ツ Avatar answered Nov 19 '22 00:11

ΦXocę 웃 Пepeúpa ツ