logo

כיצד להדפיס ערך ASCII ב-Java

ASCII ראשי תיבות של American Standard Code for Information Interchange. זוהי ערכת תווים של 7 סיביות המכילה 128 (0 עד 127) תווים. הוא מייצג את הערך המספרי של דמות. לדוגמה, ה ערך ASCII שֶׁל א הוא 65 .

בחלק זה נלמד כיצד להדפיס ערך ASCII אוֹ קוד דרך א Java תכנית.

יש שתיים דרכים להדפיס ערך ASCII ב Java :

    הקצאת משתנה למשתנה int שימוש ב-Type-Casting

הקצאת משתנה למשתנה int

כדי להדפיס את ערך ASCII של תו, אנחנו לא צריכים להשתמש בשום שיטה או מחלקה. Java ממירה באופן פנימי את ערך התו לערך ASCII.

בואו נמצא את ערך ה-ASCII של דמות דרך a תוכנית Java .

בתוכנית הבאה, הקצינו שתי דמויות א ו ב בתוך ה ch1 ו ch2 משתנים, בהתאמה. כדי למצוא את ערך ASCII של א ו ב, הקצינו משתני ch1 ו-ch2 למשתנים שלמים asciivalue1 ו asciivalue2, בהתאמה. לבסוף, הדפסנו את המשתנה asciivalue1 ו asciivalue2 שבו מאוחסנים ערכי ASCII של התווים.

PrintAsciiValueExample1.java

מוסיף מלא
 public class PrintAsciiValueExample1 { public static void main(String[] args) { // character whose ASCII value to be found char ch1 = 'a'; char ch2 = 'b'; // variable that stores the integer value of the character int asciivalue1 = ch1; int asciivalue2 = ch2; System.out.println('The ASCII value of ' + ch1 + ' is: ' + asciivalue1); System.out.println('The ASCII value of ' + ch2 + ' is: ' + asciivalue2); } } 

תְפוּקָה:

 The ASCII value of a is: 97 The ASCII value of b is: 98 

דרך נוספת לכתוב את התוכנית לעיל היא:

PrintAsciiValueExample2.java

 public class PrintAsciiValueExample2 { public static void main(String[] String) { int ch1 = 'a'; int ch2 = 'b'; System.out.println('The ASCII value of a is: '+ch1); System.out.println('The ASCII value of b is: '+ch2); } } 

תְפוּקָה:

 The ASCII value of a is: 97 The ASCII value of b is: 98 

באופן דומה, אנו יכולים להדפיס את ערך ASCII של תווים אחרים (A, B, C, …., Z) וסמלים (!, @, $, * וכו').

.06 כשבר

שימוש ב-Type-Casting

יציקת סוג היא דרך להטיל משתנה לסוג נתונים אחר.

בתוכנית הבאה, הכרזנו על שני משתנים ch1 ו ch2 של סוג לְהַשְׁחִיר בעל הדמות א ו ב, בהתאמה. בשתי השורות הבאות, הטלנו סוג char לסוג int באמצעות (int) . לאחר ביצוע שתי השורות הללו, המשתנה ch1 ו ch2 מומרים למשתנה int ascii1 ו ascii2 , בהתאמה.

לבסוף, הדפסנו את המשתנה ascii1 ו ascii2 שבו מאוחסנים ערכי ASCII של התווים.

PrintAsciiValueExample3.java

אתרים כמו coomeet
 public class PrintAsciiValueExample3 { public static void main(String[] args) { //characters whose ASCII value to be found char ch1 = 'a'; char ch2 = 'b'; //casting or converting a charter into int type int ascii1 = (int) ch1; int ascii2 = (int) ch2; System.out.println('The ASCII value of ' + ch1 + ' is: ' + ascii1); System.out.println('The ASCII value of ' + ch1 + ' is: ' + ascii2); } } 

תְפוּקָה:

 The ASCII value of a is: 97 The ASCII value of b is: 98 

אם אנחנו לא רוצים להקצות תו, נוכל לקחת גם תו מהמשתמש.

PrintAsciiValueExample4.java

 import java.util.Scanner; public class PrintAsciiValueExample4 { public static void main(String args[]) { System.out.print('Enter a character: '); Scanner sc = new Scanner(System.in); char chr = sc.next().charAt(0); int asciiValue = chr; System.out.println('ASCII value of ' +chr+ ' is: '+asciiValue); } } 

פלט 1:

 Enter a character: P ASCII value of P is: 80 

פלט 2:

 Enter a character: G ASCII value of G is: 71 

התוכנית הבאה מדפיסה את ערך ASCII (0 עד 255) של כל התווים. בפלט, הצגנו כמה ערכים.

AsciiValueOfAllChracters.java

 public class AsciiValueOfAllChracters { public static void main(String[] args) { for(int i = 0; i <= 78 255; i++) { system.out.println(' the ascii value of ' + (char)i techcodeview.com img java-tutorial how-print-ascii-value-java.webp' alt="How to Print ASCII Value in Java"> <p>If we want to print the ASCII value of all the alphabets (A to Z), we can set the values in the loop and print them.</p> <p> <strong>AsciiValueAtoZ.java</strong> </p> <pre> public class AsciiValueAtoZ { public static void main(String[] args) { for(int i = 65; i <= 78 90; i++) { system.out.println(' the ascii value of ' + (char)i techcodeview.com img java-tutorial how-print-ascii-value-java-2.webp' alt="How to Print ASCII Value in Java"> <p>Similarly, we can print the ASCII value of <strong>a to z</strong> by changing the loop in the above code.</p> <pre> for(int i = 97; i <= 122; i++) < pre> <hr></=></pre></=></pre></=>