88

How to convert a Java String to an ASCII byte array?

11 Answers 11

168

Using the getBytes method, giving it the appropriate Charset (or Charset name).

Example:

String s = "Hello, there.";
byte[] b = s.getBytes(StandardCharsets.US_ASCII);

If more control is required (such as throwing an exception when a character outside the 7 bit US-ASCII is encountered) then CharsetDecoder can be used:

private static byte[] strictStringToBytes(String s, Charset charset) throws CharacterCodingException {
    ByteBuffer x  = charset.newEncoder().onMalformedInput(CodingErrorAction.REPORT).encode(CharBuffer.wrap(s));
    byte[] b = new byte[x.remaining()];
    x.get(b);
    return b;
 }

Before Java 7 it is possible to use: byte[] b = s.getBytes("US-ASCII");. The enum StandardCharsets, the encoder as well as the specialized getBytes(Charset) methods have been introduced in Java 7.

Sign up to request clarification or add additional context in comments.

4 Comments

I'm mildly embarrassed by how easy that was.
This will convert unmappable characters like '\u00e0' (à) into '?'. It would be nicer to have a method that converts that into 'a'.
For people using Java 7 or later, use the class StandardCharsets which contains some constants for standard charsets. byte[] b = s.getBytes(StandardCharsets.US_ASCII);
getBytes(Charset) introduced in Java 6.
23

If you are a user there is a handy Charsets class:

String s = "Hello, world!";
byte[] b = s.getBytes(Charsets.US_ASCII);

Apart from not hard-coding arbitrary charset name in your source code it has a much bigger advantage: Charsets.US_ASCII is of Charset type (not String) so you avoid checked UnsupportedEncodingException thrown only from String.getBytes(String), but not from String.getBytes(Charset).

In Java 7 there is equivalent StandardCharsets class.

1 Comment

sadly, String.getBytes(Charset) was not added until API 9 :( So if you want to target Froyo and above, you can't do that.
5
String s = "ASCII Text";
byte[] bytes = s.getBytes("US-ASCII");

Comments

5

There is only one character wrong in the code you tried:

Charset characterSet = Charset.forName("US-ASCII");
String string = "Wazzup";
byte[] bytes = String.getBytes(characterSet);
               ^

Notice the upper case "String". This tries to invoke a static method on the string class, which does not exist. Instead you need to invoke the method on your string instance:

byte[] bytes = string.getBytes(characterSet);

2 Comments

if so , can you please tell me how could it be that an hebrew letter is taken 1 byte (ascii encoding) , it even doesnt exists in the ascii. and it is not using default encodung since i specified manually. i.sstatic.net/5WPD3.jpg
@RoyiNamir: This might be better posted as a new question, but the reason is that character is not encodable in US-ASCII and the getBytes(Charset) method is specified to replace characters that can not be encoded. With US-ASCII, this replacement char is the question mark, so your byte array contains one element with the ASCII value of '?' (63).
5

The problem with other proposed solutions is that they will either drop characters that cannot be directly mapped to ASCII, or replace them with a marker character like ?.

You might desire to have for example accented characters converted to that same character without the accent. There are a couple of tricks to do this (including building a static mapping table yourself or leveraging existing 'normalization' defined for unicode), but those methods are far from complete.

Your best bet is using the junidecode library, which cannot be complete either but incorporates a lot of experience in the most sane way of transliterating Unicode to ASCII.

Comments

4

If you happen to need this in Android and want to make it work with anything older than FroYo, you can also use EncodingUtils.getAsciiBytes():

byte[] bytes = EncodingUtils.getAsciiBytes("ASCII Text");

4 Comments

This is actually a pretty good tip! On Android getBytes(...) does NOT work properly even on ICS+
I can't find EncodingUtils anywhere?
@behelit if you follow my link it redirects to this bit: developer.android.com/about/versions/marshmallow/… Basically saying that you need to manually include the Apache HTTP library as it's deprecated now.
But if you're just looking for the docs, searching for "apache http encodingutils" gives some useful results like: hc.apache.org/httpcomponents-core-ga/httpcore/apidocs/org/…
4

In my string I have Thai characters (TIS620 encoded) and German umlauts. The answer from agiles put me on the right path. Instead of .getBytes() I use now

  int len = mString.length(); // Length of the string
  byte[] dataset = new byte[len];
  for (int i = 0; i < len; ++i) {
     char c = mString.charAt(i);
     dataset[i]= (byte) c;
  }

1 Comment

Maybe a little bit late but this worked perfectly for me (Trying to convert German UTF-8 Special Characters to ASCII). Thank you very much! You made my day :D
0

Convert string to ascii values.

   String test = "ABCD";

   for ( int i = 0; i < test.length(); ++i ) {
   char c = test.charAt( i );
   int j = (int) c;
   System.out.println(j);
   }

Comments

0

I found the solution. Actually Base64 class is not available in Android. Link is given below for more information.

byte[] byteArray;                                                  
     byteArray= json.getBytes(StandardCharsets.US_ASCII);
    String encoded=Base64.encodeBytes(byteArray);
    userLogin(encoded);

Here is the link for Base64 class: http://androidcodemonkey.blogspot.com/2010/03/how-to-base64-encode-decode-android.html

Comments

0

To convert String to ASCII byte array:

String s1 = "Hello World!";
byte[] byteArray = s1.getBytes(StandardCharsets.US_ASCII);
// Now byteArray is [72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33]

To convert ASCII byte array to String:

String s2 = new String(byteArray, StandardCharsets.US_ASCII));

Comments

-2

Try this:

/**
 * @(#)demo1.java
 *
 *
 * @author 
 * @version 1.00 2012/8/30
 */

import java.util.*;

public class demo1 
{
    Scanner s=new Scanner(System.in);

    String str;
    int key;

    void getdata()
    {
        System.out.println ("plase enter a string");
        str=s.next();
        System.out.println ("plase enter a key");
        key=s.nextInt();
    }

    void display()
    {
        char a;
        int j;
        for ( int i = 0; i < str.length(); ++i )
        {

            char c = str.charAt( i );
            j = (int) c + key;
            a= (char) j;

            System.out.print(a);  
        }

        public static void main(String[] args)
        {
            demo1 obj=new demo1();
            obj.getdata();
            obj.display();
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.