Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › ASCII and C programming small problem
New Posts  All Forums:Forum Nav:

ASCII and C programming small problem

post #1 of 8
Thread Starter 
The following code prints the letter 'A'. Can someone please explain why? Thanks in advance.
Code:
void main(){
int c=0x61;
printf("The variable 'c' contains %c",c&0x5f);
}

Edited by strangerfromisengard - 3/1/12 at 10:06pm
First build
(13 items)
 
Pandoras Box
(13 items)
 
NV57H
(6 items)
 
CPUMotherboardGraphicsRAM
Phenom II X4 965 BE 4.0GHz @ 1.385V ASRock 970 Extreme 3 Gigabyte 650 Ti boost Mushkin Enhanced Silverline 4GB DDR3 1600 MHz 
RAMHard DriveOptical DriveCooling
Mushkin Enhanced Silverline 4GB DDR3 1600 MHz Seagate Barracuda 160GB  Samsung Noctua NH-D14 
MonitorPowerCaseMouse
Acer X223W Corsair VX550 Cooler Master Elite 430 TT E-Sports Theron 
Mouse Pad
Steelseries QcK Portal 2 
CPUMotherboardGraphicsRAM
E7500 @2.93 GHz Gigabyte G41M-ES2L Gigabyte HD 4650 2x 2 GB TwinMOS @ 800MHz 
Hard DriveOSMonitorPower
Samsung HD502IJ 500GB Windows 7 Ultimate 32-bit Samsung SyncMaster 943 18.5" Stock 450W 
Case
some piece of junk... 
CPUMotherboardGraphicsRAM
i3-2310M HM65 Intel HD 3000 6 GB DDR3 
Hard DriveOS
750 GB Windows 7 Home Premium x64 
  hide details  
Reply
First build
(13 items)
 
Pandoras Box
(13 items)
 
NV57H
(6 items)
 
CPUMotherboardGraphicsRAM
Phenom II X4 965 BE 4.0GHz @ 1.385V ASRock 970 Extreme 3 Gigabyte 650 Ti boost Mushkin Enhanced Silverline 4GB DDR3 1600 MHz 
RAMHard DriveOptical DriveCooling
Mushkin Enhanced Silverline 4GB DDR3 1600 MHz Seagate Barracuda 160GB  Samsung Noctua NH-D14 
MonitorPowerCaseMouse
Acer X223W Corsair VX550 Cooler Master Elite 430 TT E-Sports Theron 
Mouse Pad
Steelseries QcK Portal 2 
CPUMotherboardGraphicsRAM
E7500 @2.93 GHz Gigabyte G41M-ES2L Gigabyte HD 4650 2x 2 GB TwinMOS @ 800MHz 
Hard DriveOSMonitorPower
Samsung HD502IJ 500GB Windows 7 Ultimate 32-bit Samsung SyncMaster 943 18.5" Stock 450W 
Case
some piece of junk... 
CPUMotherboardGraphicsRAM
i3-2310M HM65 Intel HD 3000 6 GB DDR3 
Hard DriveOS
750 GB Windows 7 Home Premium x64 
  hide details  
Reply
post #2 of 8
Quote:
Originally Posted by strangerfromisengard View Post

The following code prints the letter 'A'. Can someone please explain why? Thanks in advance.
Code:
void main(){
int c=0x61;
printf("The variable 'c' contains %c",c&0x5f);
}

Because 61 is the hexadecimal character code for the ASCII character 'A'.
post #3 of 8
Quote:
Originally Posted by Plan9 View Post

Because 61 is the hexadecimal character code for the ASCII character 'A'.

no its not. 61 is the hex code for lower case 'a'. 41 is the hex code for uppercase 'A'. http://www.asciitable.com/

the reason that lower case 'a' is printed as 'A' is because & 0x5f converts the character to uppercase.
Edited by travesty - 3/2/12 at 10:50am
Fractal Design
(15 items)
 
775 4 life
(15 items)
 
 
CPUMotherboardGraphicsRAM
Intel i7 2600K Biostar TP67XE NVidia GTX 570 Crucial Ballistix 
Hard DriveHard DriveCoolingOS
Crucial C300 RealSSD SDD Samsung F4 2TB Noctua NH-D14 Windows 7 Professional x64 
MonitorMonitorKeyboardPower
Asus VH202T 20'' 1600x900 Acer P244W 24" 1920 x 1080 Apple Keyboard with Numeric Keypad SeaSonic M12II 620W 
CaseMouseAudio
Fractal Design Define XL Titanium Grey Razor Abyssus Creative Sound Blaster X-FI Xtreme Gamer 
CPUMotherboardGraphicsRAM
Intel X3350 3.2Ghz @ 1.25v Gigabyte-GA-P35-DS3L (rev 2) XFX 4870 1GB 4GB OCZ Reaper PC2-6400 
RAMHard DriveHard DriveOptical Drive
2GB Corsair XMS2 PC2-6400 Crucial C300 64GB SSD 2TB Samsung Spinpoint F4 Sony Super Multi 
OSMonitorPowerCase
Windows 7 Professional x64 SP1 Asus VH202T 20'' 1600x900 SeaSonic M12II 620W Cooler Master Centurion 5 
Mouse
Razor Abyssus 
CPUMotherboardGraphicsRAM
Core i5-520M Lenovo 2522BF3 NVIDIA® Quadro® NVS3100M  Ramaxel Technology 4Gb DDR3 
Hard DriveOptical DriveOSMonitor
Samsung SSD 128GB 1.8" Micro SATA  hl-dt-st dvdram gu10n Windows 7 Enterprise (64-bit) 14.1" WXGA (1280x800) display, anti-glare, LED ... 
Power
9-cell plus Slice battery 
  hide details  
Reply
Fractal Design
(15 items)
 
775 4 life
(15 items)
 
 
CPUMotherboardGraphicsRAM
Intel i7 2600K Biostar TP67XE NVidia GTX 570 Crucial Ballistix 
Hard DriveHard DriveCoolingOS
Crucial C300 RealSSD SDD Samsung F4 2TB Noctua NH-D14 Windows 7 Professional x64 
MonitorMonitorKeyboardPower
Asus VH202T 20'' 1600x900 Acer P244W 24" 1920 x 1080 Apple Keyboard with Numeric Keypad SeaSonic M12II 620W 
CaseMouseAudio
Fractal Design Define XL Titanium Grey Razor Abyssus Creative Sound Blaster X-FI Xtreme Gamer 
CPUMotherboardGraphicsRAM
Intel X3350 3.2Ghz @ 1.25v Gigabyte-GA-P35-DS3L (rev 2) XFX 4870 1GB 4GB OCZ Reaper PC2-6400 
RAMHard DriveHard DriveOptical Drive
2GB Corsair XMS2 PC2-6400 Crucial C300 64GB SSD 2TB Samsung Spinpoint F4 Sony Super Multi 
OSMonitorPowerCase
Windows 7 Professional x64 SP1 Asus VH202T 20'' 1600x900 SeaSonic M12II 620W Cooler Master Centurion 5 
Mouse
Razor Abyssus 
CPUMotherboardGraphicsRAM
Core i5-520M Lenovo 2522BF3 NVIDIA® Quadro® NVS3100M  Ramaxel Technology 4Gb DDR3 
Hard DriveOptical DriveOSMonitor
Samsung SSD 128GB 1.8" Micro SATA  hl-dt-st dvdram gu10n Windows 7 Enterprise (64-bit) 14.1" WXGA (1280x800) display, anti-glare, LED ... 
Power
9-cell plus Slice battery 
  hide details  
Reply
post #4 of 8
Thread Starter 
Quote:
Originally Posted by travesty View Post

no its not. 61 is the hex code for lower case 'a'. 41 is the hex code for uppercase 'A'. http://www.asciitable.com/
the reason that lower case 'a' is printed as 'A' is because & ox5f converts the character to uppercase.

I figured that part out but why does it happen? What does 'ox5f' represent?
First build
(13 items)
 
Pandoras Box
(13 items)
 
NV57H
(6 items)
 
CPUMotherboardGraphicsRAM
Phenom II X4 965 BE 4.0GHz @ 1.385V ASRock 970 Extreme 3 Gigabyte 650 Ti boost Mushkin Enhanced Silverline 4GB DDR3 1600 MHz 
RAMHard DriveOptical DriveCooling
Mushkin Enhanced Silverline 4GB DDR3 1600 MHz Seagate Barracuda 160GB  Samsung Noctua NH-D14 
MonitorPowerCaseMouse
Acer X223W Corsair VX550 Cooler Master Elite 430 TT E-Sports Theron 
Mouse Pad
Steelseries QcK Portal 2 
CPUMotherboardGraphicsRAM
E7500 @2.93 GHz Gigabyte G41M-ES2L Gigabyte HD 4650 2x 2 GB TwinMOS @ 800MHz 
Hard DriveOSMonitorPower
Samsung HD502IJ 500GB Windows 7 Ultimate 32-bit Samsung SyncMaster 943 18.5" Stock 450W 
Case
some piece of junk... 
CPUMotherboardGraphicsRAM
i3-2310M HM65 Intel HD 3000 6 GB DDR3 
Hard DriveOS
750 GB Windows 7 Home Premium x64 
  hide details  
Reply
First build
(13 items)
 
Pandoras Box
(13 items)
 
NV57H
(6 items)
 
CPUMotherboardGraphicsRAM
Phenom II X4 965 BE 4.0GHz @ 1.385V ASRock 970 Extreme 3 Gigabyte 650 Ti boost Mushkin Enhanced Silverline 4GB DDR3 1600 MHz 
RAMHard DriveOptical DriveCooling
Mushkin Enhanced Silverline 4GB DDR3 1600 MHz Seagate Barracuda 160GB  Samsung Noctua NH-D14 
MonitorPowerCaseMouse
Acer X223W Corsair VX550 Cooler Master Elite 430 TT E-Sports Theron 
Mouse Pad
Steelseries QcK Portal 2 
CPUMotherboardGraphicsRAM
E7500 @2.93 GHz Gigabyte G41M-ES2L Gigabyte HD 4650 2x 2 GB TwinMOS @ 800MHz 
Hard DriveOSMonitorPower
Samsung HD502IJ 500GB Windows 7 Ultimate 32-bit Samsung SyncMaster 943 18.5" Stock 450W 
Case
some piece of junk... 
CPUMotherboardGraphicsRAM
i3-2310M HM65 Intel HD 3000 6 GB DDR3 
Hard DriveOS
750 GB Windows 7 Home Premium x64 
  hide details  
Reply
post #5 of 8
Quote:
Originally Posted by strangerfromisengard View Post

I figured that part out but why does it happen? What does 'ox5f' represent?

an equivalent way to say c&0x5f would be to say:
Code:
c &= 0x5f; // bitwise AND

have a look here for info on bitwise operations.

to understand the operation, you must convert from ASCII to hex to binary.

'a' == 0x61 == 01100001
'_' == 0x5F == 01011111

then, logical AND the binary operands together and the result is 01000001 ==> 0x41 ==> 'A'

the bitwise AND operation with 0x5F will convert any lower case letter to uppercase. this happens because '5' and 'F' in binary are 0101 and 1111 respectively. these binary numbers act as masks. lower-case ASCII characters are in the range 0x61 - 0x7A. uppercase ASCII characters are 0x41 - 0x5A.

6 AND 5 ==> 4. 7 AND 5 ==> 5.
0110 (6)
0101 (5)
0100 (4)


bitwise AND with 'F' is an identity.

6 AND 'F' ==> 6
0110 (6)
1111 (F)
0110 (6)
Edited by travesty - 3/2/12 at 11:30am
Fractal Design
(15 items)
 
775 4 life
(15 items)
 
 
CPUMotherboardGraphicsRAM
Intel i7 2600K Biostar TP67XE NVidia GTX 570 Crucial Ballistix 
Hard DriveHard DriveCoolingOS
Crucial C300 RealSSD SDD Samsung F4 2TB Noctua NH-D14 Windows 7 Professional x64 
MonitorMonitorKeyboardPower
Asus VH202T 20'' 1600x900 Acer P244W 24" 1920 x 1080 Apple Keyboard with Numeric Keypad SeaSonic M12II 620W 
CaseMouseAudio
Fractal Design Define XL Titanium Grey Razor Abyssus Creative Sound Blaster X-FI Xtreme Gamer 
CPUMotherboardGraphicsRAM
Intel X3350 3.2Ghz @ 1.25v Gigabyte-GA-P35-DS3L (rev 2) XFX 4870 1GB 4GB OCZ Reaper PC2-6400 
RAMHard DriveHard DriveOptical Drive
2GB Corsair XMS2 PC2-6400 Crucial C300 64GB SSD 2TB Samsung Spinpoint F4 Sony Super Multi 
OSMonitorPowerCase
Windows 7 Professional x64 SP1 Asus VH202T 20'' 1600x900 SeaSonic M12II 620W Cooler Master Centurion 5 
Mouse
Razor Abyssus 
CPUMotherboardGraphicsRAM
Core i5-520M Lenovo 2522BF3 NVIDIA® Quadro® NVS3100M  Ramaxel Technology 4Gb DDR3 
Hard DriveOptical DriveOSMonitor
Samsung SSD 128GB 1.8" Micro SATA  hl-dt-st dvdram gu10n Windows 7 Enterprise (64-bit) 14.1" WXGA (1280x800) display, anti-glare, LED ... 
Power
9-cell plus Slice battery 
  hide details  
Reply
Fractal Design
(15 items)
 
775 4 life
(15 items)
 
 
CPUMotherboardGraphicsRAM
Intel i7 2600K Biostar TP67XE NVidia GTX 570 Crucial Ballistix 
Hard DriveHard DriveCoolingOS
Crucial C300 RealSSD SDD Samsung F4 2TB Noctua NH-D14 Windows 7 Professional x64 
MonitorMonitorKeyboardPower
Asus VH202T 20'' 1600x900 Acer P244W 24" 1920 x 1080 Apple Keyboard with Numeric Keypad SeaSonic M12II 620W 
CaseMouseAudio
Fractal Design Define XL Titanium Grey Razor Abyssus Creative Sound Blaster X-FI Xtreme Gamer 
CPUMotherboardGraphicsRAM
Intel X3350 3.2Ghz @ 1.25v Gigabyte-GA-P35-DS3L (rev 2) XFX 4870 1GB 4GB OCZ Reaper PC2-6400 
RAMHard DriveHard DriveOptical Drive
2GB Corsair XMS2 PC2-6400 Crucial C300 64GB SSD 2TB Samsung Spinpoint F4 Sony Super Multi 
OSMonitorPowerCase
Windows 7 Professional x64 SP1 Asus VH202T 20'' 1600x900 SeaSonic M12II 620W Cooler Master Centurion 5 
Mouse
Razor Abyssus 
CPUMotherboardGraphicsRAM
Core i5-520M Lenovo 2522BF3 NVIDIA® Quadro® NVS3100M  Ramaxel Technology 4Gb DDR3 
Hard DriveOptical DriveOSMonitor
Samsung SSD 128GB 1.8" Micro SATA  hl-dt-st dvdram gu10n Windows 7 Enterprise (64-bit) 14.1" WXGA (1280x800) display, anti-glare, LED ... 
Power
9-cell plus Slice battery 
  hide details  
Reply
post #6 of 8
Thread Starter 
Thanks a ton! It all makes sense now! biggrin.gif
First build
(13 items)
 
Pandoras Box
(13 items)
 
NV57H
(6 items)
 
CPUMotherboardGraphicsRAM
Phenom II X4 965 BE 4.0GHz @ 1.385V ASRock 970 Extreme 3 Gigabyte 650 Ti boost Mushkin Enhanced Silverline 4GB DDR3 1600 MHz 
RAMHard DriveOptical DriveCooling
Mushkin Enhanced Silverline 4GB DDR3 1600 MHz Seagate Barracuda 160GB  Samsung Noctua NH-D14 
MonitorPowerCaseMouse
Acer X223W Corsair VX550 Cooler Master Elite 430 TT E-Sports Theron 
Mouse Pad
Steelseries QcK Portal 2 
CPUMotherboardGraphicsRAM
E7500 @2.93 GHz Gigabyte G41M-ES2L Gigabyte HD 4650 2x 2 GB TwinMOS @ 800MHz 
Hard DriveOSMonitorPower
Samsung HD502IJ 500GB Windows 7 Ultimate 32-bit Samsung SyncMaster 943 18.5" Stock 450W 
Case
some piece of junk... 
CPUMotherboardGraphicsRAM
i3-2310M HM65 Intel HD 3000 6 GB DDR3 
Hard DriveOS
750 GB Windows 7 Home Premium x64 
  hide details  
Reply
First build
(13 items)
 
Pandoras Box
(13 items)
 
NV57H
(6 items)
 
CPUMotherboardGraphicsRAM
Phenom II X4 965 BE 4.0GHz @ 1.385V ASRock 970 Extreme 3 Gigabyte 650 Ti boost Mushkin Enhanced Silverline 4GB DDR3 1600 MHz 
RAMHard DriveOptical DriveCooling
Mushkin Enhanced Silverline 4GB DDR3 1600 MHz Seagate Barracuda 160GB  Samsung Noctua NH-D14 
MonitorPowerCaseMouse
Acer X223W Corsair VX550 Cooler Master Elite 430 TT E-Sports Theron 
Mouse Pad
Steelseries QcK Portal 2 
CPUMotherboardGraphicsRAM
E7500 @2.93 GHz Gigabyte G41M-ES2L Gigabyte HD 4650 2x 2 GB TwinMOS @ 800MHz 
Hard DriveOSMonitorPower
Samsung HD502IJ 500GB Windows 7 Ultimate 32-bit Samsung SyncMaster 943 18.5" Stock 450W 
Case
some piece of junk... 
CPUMotherboardGraphicsRAM
i3-2310M HM65 Intel HD 3000 6 GB DDR3 
Hard DriveOS
750 GB Windows 7 Home Premium x64 
  hide details  
Reply
post #7 of 8
That's unnecessarily complicated, though. You could just subtract 0x20. Using AND probably makes sure it stays a valid character if you use a number outside of the range, but who cares?
Akiyama Mio
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6420 @ stock, 0.98v Asus P5N-E SLI Gainward GTX 460 1GB @ 800/1600/1900 2x2GB Kingston @ 800MHz 5-5-5-15 2T 
Hard DriveOptical DriveOSMonitor
WD 250GB, 320GB SATA/3, 16MB Cache, Seagate 1TB LG GSA-H62N 18x SATA Ubuntu 9.10 x86 & Win7 x86 Asus VW222U 
KeyboardPowerCase
Logitech Classic Corsair 650HX NZXT Apollo Black 
  hide details  
Reply
Akiyama Mio
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6420 @ stock, 0.98v Asus P5N-E SLI Gainward GTX 460 1GB @ 800/1600/1900 2x2GB Kingston @ 800MHz 5-5-5-15 2T 
Hard DriveOptical DriveOSMonitor
WD 250GB, 320GB SATA/3, 16MB Cache, Seagate 1TB LG GSA-H62N 18x SATA Ubuntu 9.10 x86 & Win7 x86 Asus VW222U 
KeyboardPowerCase
Logitech Classic Corsair 650HX NZXT Apollo Black 
  hide details  
Reply
post #8 of 8
Quote:
Originally Posted by travesty View Post

no its not. 61 is the hex code for lower case 'a'. 41 is the hex code for uppercase 'A'. http://www.asciitable.com/
the reason that lower case 'a' is printed as 'A' is because & 0x5f converts the character to uppercase.

oh yeah, so it is. Reading fail. laughingsmiley.gif
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › ASCII and C programming small problem