ASCII es un acrónimo que significa Código Estándar Estadounidense para el Intercambio de Información. En ASCII, se otorga un valor numérico específico a diferentes caracteres y símbolos, para que las computadoras los almacenen y manipulen, y mientras almacena y manipula el dispositivo electrónico siempre funciona con el valor binario del número ASCII dado. Como es imposible hacer eso en la forma original.
Enfoques: hay 4 formas de imprimir el valor ASCII o el código de un carácter específico que se enumeran a continuación para resumir el concepto seguido de un ejemplo de Java para la parte de implementación.
Java
// Java program to print ASCII Value of Character // by assigning variable to integer public class GFG { // Main driver method public static void main(String[] args) { // Character whose ASCII is to be computed char ch = '}'; // Creating a new variable of type int // and assigning the character value. int ascii = ch; /* Java stores the ascii value there itself*/ // Printing the ASCII value of above character System.out.println("The ASCII value of " + ch + " is: " + ascii); } }
Java
// Java program to print ASCII Value of Character // using type-casting // Importing java generic libraries import java.util.*; public class GFG { // Main driver method public static void main(String[] args) { // Character whose ASCII is to be computed char ch = '}'; // Typecasting the character to int and // printing the same System.out.println("The ASCII value of " + ch + " is: " + (int)ch); } }
Java
// Java program to print ASCII Value of Character // using format specifier // Importing format library import java.util.Formatter; public class GFG { // Main driver method public static void main(String[] args) { // Character whose ASCII is to compute char character = '}'; // Initializing the format specifier Formatter formatSpecifier = new Formatter(); // Converting the character to integer and // ASCII value is stored in the format specifier formatSpecifier.format("%d", (int)character); // Print the corresponding ASCII value System.out.println( "The ASCII value of the character ' " + character + " ' is " + formatSpecifier); } }
Java
// Java program to print ASCII Value of Character // by generating bytes. // Importing I/O library import java.io.UnsupportedEncodingException; public class GFG { // Main driver method public static void main(String[] args) { // Try block to check exception try { // Character is initiated as a string String sp = "}"; // An array of byte type is created // by using getBytes method byte[] bytes = sp.getBytes("US-ASCII"); /*This is the ASCII value of the character / present at the '0'th index of above string.*/ // Printing the element at '0'th index // of array(bytes) using charAt() method System.out.println("The ASCII value of " + sp.charAt(0) + " is " + bytes[0]); } // Catch block to handle exception catch (UnsupportedEncodingException e) { // Message printed for exception System.out.println("OOPs!!!UnsupportedEncodingException occurs."); } } }
Publicación traducida automáticamente
Artículo escrito por akashkumarsen4 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA