ASCII Code of Characters | String Operations with ASCII Code
ASCII Code of Characters | ASCII Code | ASCII Code of a to z | ASCII Code Table | Binary to ASCII Code | What is ASCII Code? | ASCII Code of 0 to 9
What is ASCII?
💡ASCII is a common data encoding type used for computer-to-computer electronic communication.
ASCII assigns standard numeric values to letters, numerals, punctuation marks, and other characters used in computers.
The ASCII (American Standard Code for Information Interchange)
code is a character encoding standard that represents text in computers and other devices that use text. In ASCII, each character is assigned a unique numeric code, ranging from 0 to 127, which corresponds to a specific character.
💡What is the purpose of ASCII codes?
The computer doesn’t understand English, Hindi, or any language. But computers do understand only 0 and 1. 0 means False & 1 means true.
So every char have an ASCII code, that further covert to a binary number (0, 1 format) and that the Computer use for processing.
💡What happens When we press a number key?
If you press 2 from the keyboard, then the keyboard sends the value 10 (binary format of 2) to the main memory which will further be evaluated in secondary memory, and then the output will be 2 in the monitor.
💡What happens when we press a Char key?
When we press “a”, the ASCII value will be 097 which is equivalent to 01100001 which is processed by Computer memory.
👉 NOTE- Numbers don’t have ASCII value, only Characters have ASCII value. And ASCII Code is of Single character only.
Example -
'a' have ASCII Code but 'ab' does not have ascii code.
'1' have ASCII Code but 1 (number) does not have.
All symbols have ASCII Code.
‘1’ – this is string & 1 – this is number.
💡How to find ASCII Code in JavaScript?
We use the Inbuilt String method charCodeAt to get the Char code of any character of a string.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/charCodeAt
💻 ASCII Code of English Capital Letters-
function asciiValuesOfCapsChar() {
console.log('ascii Values Of Capital Letters:');
let alphabets = 'ABCDEFGHIJKLMNOPQUERSTUVWXYZ';
for (let i = 0; i < alphabets.length; i++) {
console.log(alphabets[i], alphabets.charCodeAt(i));
}
}
A 65, B 66, C 67, D 68, E 69, F 70, G 71, H 72, I 73, J 74, K 75, L 76, M 77, N 78, O 79, P 80, Q 81, U 85, E 69, R 82, S 83, T 84, U 85, V 86, W 87, X 88, Y 89, Z 90
💻 ASCII Code of English Small Letters-
function asciiValuesOfSmallChar() {
console.log('ascii Values Of Small Letters:');
let alphabets = 'abcdefghijklmnopquerstuvwxyz';
for (let i = 0; i < alphabets.length; i++) {
console.log(alphabets[i], alphabets.charCodeAt(i));
}
}
asciiValuesOfSmallChar();
a 97, b 98, c 99, d 100, e 101, f 102, g 103, h 104, i 105, j 106, k 107, l 108, m 109, n 110, o 111, p 112, q 113, u 117, e 101, r 114, s 115, t 116, u 117, v 118, w 119, x 120, y 121, z 122
❓How to find the ASCII Code of a given Char?
function findAsciiOfChar() {
console.log('findAsciiOfChar :');
console.log('z', 'z'.charCodeAt())
console.log("a", 'a'.charCodeAt())
console.log('1', '1'.charCodeAt())
}
findAsciiOfChar();
💻 ASCII Code of Numbers In String Format. (‘0’, ‘1’, ‘2’…..’9′)
function asciiCodeOfNumberChars() {
console.log('asciiCodeOfNumberChars :');
for (let i = 0; i < 10; i++) {
console.log(i, i.toString().charCodeAt())
}
}
asciiCodeOfNumberChars();
'0' 48, '1' 49, '2' 50, '3' 51, '4' 52, '5' 53, '6' 54, '7' 55, '8' 56, '9' 57
ASCII Code is magic to do String Operations.
💡Observation between Capital Letters and Small Letters ASCII Codes-
A = 65 a = 97 Difference = 32 (97-65)
B = 66 b = 98 ...
C = 67 c = 99 ..
...
..
Z = 90 z = 122 Difference = 32 (122-90)
👉 The difference from ‘A’ to ‘a’ is 32. This means every Capital Letter and Small Letter has a difference of 32.
💡Binary representation of a Character
char ch = '9';
ASCII Code of '9' = 57
Char takes 1 Byte means 8 bits. So binary representaiton of '9' is 00111001
Lets Solve a Toggle String problem
Toggle every char of string, Cap to small and small to caps Without using any lowercase or uppercase method.
👀 Approach1 – Using ASCII Range of Capital Letters and Small Letters
As per the discussion, we know that Capital letters have an ASCII range of 65 to 90. And the difference between Capital letters and small letter has 32.
function toggleStringUsingAsciiRange(str) {
let ans = '';
for (let i = 0; i < str.length; i++) {
let code = str[i].charCodeAt();
// If char in range of Captial Letter's ascii values.
if (code >= 65 && code <= 90) { // A = 65 & Z = 90
code += 32; // convert to small
} else {
code -= 32; // convert to capital
}
ans += String.fromCharCode(code)
}
return ans;
}
toggleStringUsingAsciiRange('JSMount');
output > jsmOUNT
ASCII Code of Characters | ASCII Code | ASCII Code of a to z
👀 Approach 2 – Solve the same problem without using Range.
8 bit representation of Character-
32 32
A = 65 = 0 1 0 0 0 0 0 1 a = 97 = 0 1 1 0 0 0 0 1
B = 66 = 0 1 0 0 0 0 1 0 b = 98 = 0 1 1 0 0 0 1 0
C = 67 = 0 1 0 0 0 0 1 1 c = 99 = 0 1 1 0 0 0 1 1
...
..
Z = 90 = 0 1 0 1 1 0 1 0 z = 122 = 0 1 1 1 1 0 1 0
💡 Observations-
- All bits of Capital Letters and Small Letters have the same bit at their respective position instead of the 2^5 (32) position.
- In the Capital letter, 2^5 position has 0. In the Small letter, 2^5 position has 1.
- So If we have to convert Capital to Small or vice-versa then we have to toggle 2^5 bit.
- Capital to Small > 0 to 1. Small to Capital > 1 to 0
By using the XOR Binary operation, we can easily obtain the toggle value.
Let’s do XOR with 1-
1 ^ 1 = 0 0 ^ 1 = 0
11 ^ 11 = 00 00 ^ 11 = 00
And we have to toggle 2^5 bit position so we will do XOR with 10000
function toggleStringUsingXOR(str) {
let ans = '';
for (let i = 0; i < str.length; i++) {
let code = str[i].charCodeAt();
ans += String.fromCharCode(code ^ 32); // Or we can write same: code ^ (1 << 5)
}
console.log(ans);
}
toggleStringUsingXOR('ABCDabcd')
❓ Return uppercase array
function toUpper(A) {
for (let i = 0; i < A.length; i++) {
let code = A[i].charCodeAt();
if (code >= 97 && code <= 122) { // char code of a = 97 and z = 122.
code -= 32; // capital letters have -32 char code from upper case letter.
}
A[i] = String.fromCharCode(code);
}
return A;
}
toUpper(['Z', '*', 'z', ':', 'a', 'A']); // ['Z', '*', 'Z', ':', 'A', 'A']
Keep in mind that ASCII only covers the basic Latin alphabet and some common symbols. For characters beyond the ASCII range, other character encodings like UTF-8 are used, which can represent a wider range of characters from different languages and writing systems.
- Insert node in Linked list | Algorithm | JavaScript
- Insertion Sort in data structure | Algorithm with Examples
- Selection Sort Algorithm & K’th Largest Element in Array
- Quick Sort Algorithm with example | Step-by-Step Guide
- Dependency Inversion Principle with Example | Solid Principles
- Object-Oriented Programming | Solid Principles with Examples
- ASCII Code of Characters | String Operations with ASCII Code
- Negative Binary Numbers & 2’s Complement | Easy explanation
- Factors of a Number | JavaScript Program | Optimized Way
- LeetCode – Game of Life Problem | Solution with JavaScript
- Fibonacci series using Recursion | While loop | ES6 Generator
- JavaScript Coding Interview Question & Answers
- LeetCode – Coin Change Problem | Dynamic Programming | JavaScript
- HackerRank Dictionaries and Maps Problem | Solution with JavaScript
- React Redux Unit Testing of Actions, Reducers, Middleware & Store
- Micro frontends with Module Federation in React Application
- React Interview Question & Answers – Mastering React
- Top React Interview Question & Answer | React Routing
- React Interview Questions and Answers | React hooks
- Higher Order Component with Functional Component | React JS
- Top React Interview Questions and Answers | Must Know
- Interview Question React | Basics for Freshers and Seniors
- Cyber Security Fundamental Questions & Answers You must know
- Application & Web Security Interview Questions & Answers
- Top Scrum Master and Agile Question & Answers 2022
- Trapping Rain Water Leetcode Problem Solution
- Array Representation of Binary Tree | Full Tree & Complete Binary Tree
- Graphs in Data Structure, Types & Traversal with BFS and DFS, Algorithms
- Traversing 2 D array with BFS & DFS Algorithm in JavaScript
- Time Complexity & Calculations | All You should know
- Backspace String Compare Leetcode Problem & Solution
- Angular Interview Questions & Answers 2021 – Part 3 – JS Mount
- Why Angular is a Preferred Choice for Developers? Top Features
- Angular Interview Questions & Answers You should know Part 2
- Top 30 JavaScript Interview Questions and Answers for 2021
- React JS Stripe Payment Gateway Integration with Node | Step by Step Guide
- Create Year Month & Date dropdown List using JavaScript & JQuery
- Create Custom QR Code Component using QR Code Styling in React JS
- How to create a common Helper class or util file in React JS
- React Build Routing with Fixed Header and Navigation | React Router Tutorial
- React Create Dashboard Layout with Side Menu, Header & Content Area
- Web Application Security Best Practices | Top Tips to Secure Angular App
- HTML Form with Pure CSS & JavaScript | Custom Radio and Checkbox
- NgRx Top Interview Questions and Answers You should know
- Top 40 Awesome CSS Interview Questions & Answers You should know | CSS Tutorial
- Tips to Boost Angular App Performance | Web page Speed Optimization
- JavaScript Rotate 2D matrix 90 degrees clockwise | Top Interview Question
- HashTable Data Structure in JavaScript with Add Delete & Search Algorithms
- Trie Data Structure – Insert Search Delete & Print Program with JavaScript
- Top JavaScript Commonly asked Algorithms in Interview
- JavaScript String Permutation Program with nice explanation
- Angular NgModule, AOT, and DI, and EntryComponents with example
- Angular Dependency Injection Provider and Types of Provider
- TypeScript New & Latest Features You should know | JS Mount
- Rx JS Top Operators with examples | Rx JS interview questions
- What’s new in Angular 9 | Top updates in Angular 9 By JS mount
- What’s new in Angular 8 | Angular 8 Latest Feature By JS mount
- Prime NG Row Group with Subheader and Totals Row
- How to implement Reactive Form with Prime NG table
- Angular Unit Testing Spy, Router, Fake Async, tick, & Subscribe
- Angular Auto Tab Directive : Auto focus on next field
- VS Code Useful Extensions for Web Development
- Angular HTTP Request Testing with HttpClientTestingModule & HttpTestingController
- Dynamic Tooltip with Angular Pipe: Performance Orientation
- Konva JS Event Handling and Get Overlapped Shapes where clicked
- JavaScript Top Interview questions & Answers You should know
- Best Practices for Writing Angular Apps | Angular Guidelines
- Top 30 TypeScript Interview Questions & Answers You Must Know
- HR Interview Questions and Answers for Experienced
- Mastering JavaScript Interview Questions & Answers
ASCII Code of Characters | ASCII Code | ASCII Code of a to z | ASCII Code Table | Binary to ASCII Code | What is ASCII Code? | ASCII Code of 0 to 9