Convert Character to Ascii Numeric Value in Java

Convert character to ASCII numeric value in java

Very simple. Just cast your char as an int.

char character = 'a';    
int ascii = (int) character;

In your case, you need to get the specific Character from the String first and then cast it.

char character = name.charAt(0); // This gives the character 'a'
int ascii = (int) character; // ascii is now 97.

Though cast is not required explicitly, but its improves readability.

int ascii = character; // Even this will do the trick.

How can I convert a char to int in Java?

The ASCII table is arranged so that the value of the character '9' is nine greater than the value of '0'; the value of the character '8' is eight greater than the value of '0'; and so on.

So you can get the int value of a decimal digit char by subtracting '0'.

char x = '9';
int y = x - '0'; // gives the int value 9

How to convert ASCII code (0-255) to its corresponding character?

Character.toString ((char) i);

How to cast string to ascii value in java?

You Can Try This:

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);

System.out.println("Testing Scanner, write something: ");
String testi = scan.nextLine();
char[] ascii1 = testi.toCharArray();

for(char ch:ascii1){
System.out.println((int)ch+" ");
}

System.out.println("Testing Scanner, write something: ");
String testi2 = scan.nextLine();
char[] ascii2 = testi2.toCharArray();

for(char ch:ascii2){
System.out.println((int)ch+" ");
}

scan.close();
}

Fastest way to convert numeric chars to int

  1. The two versions are not equivalent:

    • The Character.getNumericalValue(...) methods work for a variety of characters that represent digits or numbers, and it will return -1 or -2 in cases where the character doesn't represent a non-negative integer.
    • The num - '0' approach only gives the correct answer for the codepoints that correspond to the ASCII characters '0' through '9'. For all other codepoints or codeunits, it gives a meaningless value.
  2. The num - '0' version will be faster. This is clear from looking at the source code for getNumericalValue(...).

  3. While the difference is significant in relative terms, it is very small in absolute terms.


I concur with the comments that say that this is most likely a premature optimization.

It is also an incorrect optimization in some contexts.



I use it a lot so was wondering if I was using the most efficient one.

This is definitely premature optimization :-)

The number of times you write a particular code sequence is unrelated to performance of the code sequence when is executed. A section of code is only worth optimizing if the time spent executing it makes a significant difference to your entire application.

Java Transformer converts Chinese character to ASCII value

Change UTF-8 to UTF-16. Since you're making a String (which is code-page agnostic) this has no ill effect on the encoding. This however adds code-page declaration and sometimes a BOM (Byte-Order-Mark) in the XML header. You can optionally leave the header out and attach your own.

    String value= "かな〜"; // (I don't see your character so I added some of my own)
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = builder.newDocument();
Element root = doc.createElement("value");
root.setAttribute("attribute", value);
doc.appendChild(root);
DOMSource source = new DOMSource(doc);

ByteArrayOutputStream outStream = null;
Transformer transformer = TransformerFactory.newInstance().newTransformer();
StreamResult htmlStreamResult = new StreamResult( new ByteArrayOutputStream() );
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-16");
// transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); // optional
transformer.transform(source, htmlStreamResult);
outStream = (ByteArrayOutputStream) htmlStreamResult.getOutputStream();
String outPut = outStream.toString( "UTF-16" );
System.out.println(outPut);

Output:

<?xml version="1.0" encoding="UTF-16" standalone="no"?><value attribute="かな〜"/>


Related Topics



Leave a reply



Submit