how to change a character in a string in java with ascii

import java.text.ParseException;
import java.util.Arrays;

/**
 * How to convert a String to ASCII bytes in Java
 * 
 * @author WINDOWS 8
 */

public class StringToASCII {

    public static void main(String args[]) throws ParseException {
        
        // converting character to ASCII value in Java
        char A = 'A';
        int ascii = A;
        System.out.println("ASCII value of 'A' is  : " + ascii);
        
        // you can explicitly cast also
        char a = 'a';
        int value = (int) a;
        System.out.println("ASCII value of 'a' is  : " + value);
        
        
        
        
        // converting String to ASCII value in Java
        try {
            String text = "ABCDEFGHIJKLMNOP";

            // translating text String to 7 bit ASCII encoding
            byte[] bytes = text.getBytes("US-ASCII");
            
            System.out.println("ASCII value of " + text + " is following");
            System.out.println(Arrays.toString(bytes));
            
        } catch (java.io.UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

}

Output
ASCII value of 'A' is  : 65
ASCII value of 'a' is  : 97
ASCII value of ABCDEFGHIJKLMNOP is following
[65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80]


Read more: https://javarevisited.blogspot.com/2015/07/how-to-convert-string-or-char-to-ascii-example.html#ixzz6k2vn7o4y

4.33
5
Gustafc 80 points

                                    // From Char to Ascii
char character = 'a';    
int ascii = (int) character;

// From Ascii to Char
int ascii = 65;
char character = (char) ascii;

4.33 (6 Votes)
0
4
2
Antal Egri 105 points

                                    // 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.

4 (2 Votes)
0
Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source