How to convert character to ASCII in Java
Written on December 1, 2009 at 6:03 am by
mkyong
In Java, convert the character to ASCII is quite easy, it just convert the char to int.
Convert character to ASCII
int ascii = (int) character;
Convert ASCII to character
char character = (char)ascii;
Example in Java
package com.mkyong.common; /** * Character Utility class * @author mkyong * */ public class CharUtils { /** * Convert the characters to ASCII value * @param character character * @return ASCII value */ public static int CharToASCII(final char character){ return (int)character; } /** * Convert the ASCII value to character * @param ascii ascii value * @return character value */ public static char ASCIIToChar(final int ascii){ return (char)ascii; } }
Unit Test
package com.mkyong.common; import org.testng.Assert; import org.testng.annotations.*; /** * Character Utils Testing * @author mkyong * */ public class CharUtilsTest { @DataProvider public Object[][] ValidDataProvider() { return new Object[][]{ { 'A', 65 },{ 'a', 97 }, { 'B', 66 },{ 'b', 98 }, { 'C', 67 },{ 'c', 99 }, { 'D', 68 },{ 'd', 100 }, { 'Z', 90 },{ 'z', 122 }, { '1', 49 },{ '9', 57 }, }; } @Test(dataProvider = "ValidDataProvider") public void CharToASCIITest(final char character, final int ascii) { int result = CharUtils.CharToASCII(character); Assert.assertEquals(result, ascii); } @Test(dataProvider = "ValidDataProvider") public void ASCIIToCharTest(final char character, final int ascii) { char result = CharUtils.ASCIIToChar(ascii); Assert.assertEquals(result, character); } }
Unit Test Result
PASSED: CharToASCIITest(A, 65)
PASSED: CharToASCIITest(a, 97)
PASSED: CharToASCIITest(B, 66)
PASSED: CharToASCIITest(b, 98)
PASSED: CharToASCIITest(C, 67)
PASSED: CharToASCIITest(c, 99)
PASSED: CharToASCIITest(D, 68)
PASSED: CharToASCIITest(d, 100)
PASSED: CharToASCIITest(Z, 90)
PASSED: CharToASCIITest(z, 122)
PASSED: CharToASCIITest(1, 49)
PASSED: CharToASCIITest(9, 57)
PASSED: ASCIIToCharTest(A, 65)
PASSED: ASCIIToCharTest(a, 97)
PASSED: ASCIIToCharTest(B, 66)
PASSED: ASCIIToCharTest(b, 98)
PASSED: ASCIIToCharTest(C, 67)
PASSED: ASCIIToCharTest(c, 99)
PASSED: ASCIIToCharTest(D, 68)
PASSED: ASCIIToCharTest(d, 100)
PASSED: ASCIIToCharTest(Z, 90)
PASSED: ASCIIToCharTest(z, 122)
PASSED: ASCIIToCharTest(1, 49)
PASSED: ASCIIToCharTest(9, 57)
===============================================
com.mkyong.common.CharUtilsTest
Tests run: 24, Failures: 0, Skips: 0
===============================================
===============================================
mkyong
Total tests run: 24, Failures: 0, Skips: 0
===============================================Reference
This article was posted in Java category.
Oracle Magazine - Free Magazine
Oracle Magazine contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for Java's developers and DBAs, and more.
Securing & Optimizing Linux: The Hacking Solution - Free Guide
A comprehensive collection of Linux security products and explanations in the most simple and structured manner on how to safely and easily configure and run many popular Linux-based applications and services.
The Windows 7 Guide: From Newbies to Pros - Free Guide
In this 46 page guide you will be introduced to Windows 7 and what it has to offer. This guide will go over the software compatible issues, you will learn about the new taskbar, how to use and customize Windows Aero, what Windows 7 Libraries are all about, what software is included in Windows 7, and how easy networking is with Windows 7 along with other topics.
All Java Tutorials
- Java Core Technology - Java RegEx, Java XML, Java I/O, Java Misc
- J2EE Frameworks - Hibernate, Spring 2.5, Spring MVC, Struts 1.x, Struts 2.x
- Build Tools - Maven, Archiva
- Unit Test - jUnit, TestNG
- Client Scripts - jQuery