87

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

Jonas
  • 121,568
  • 97
  • 310
  • 388
farm ostrich
  • 5,881
  • 14
  • 55
  • 81

11 Answers11

165

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.

Maarten Bodewes
  • 90,524
  • 13
  • 150
  • 263
Sebastian Paaske Tørholm
  • 49,493
  • 11
  • 100
  • 118
  • 10
    I'm mildly embarrassed by how easy that was. – farm ostrich Apr 16 '11 at 16:54
  • 4
    This will convert unmappable characters like '\u00e0' (à) into '?'. It would be nicer to have a method that converts that into 'a'. – Arnout Engelen Jan 02 '12 at 13:11
  • 11
    For people using Java 7 or later, use the class [StandardCharsets](http://docs.oracle.com/javase/7/docs/api/java/nio/charset/StandardCharsets.html) which contains some constants for standard charsets. `byte[] b = s.getBytes(StandardCharsets.US_ASCII);` – Alexis C. May 15 '14 at 22:29
  • `getBytes(Charset)` introduced in [Java 6](https://docs.oracle.com/javase/6/docs/api/java/lang/String.html#getBytes(java.nio.charset.Charset)). – Dávid Horváth Jan 17 '22 at 22:35
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.

Jon
  • 9,156
  • 9
  • 56
  • 73
Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
  • sadly, `String.getBytes(Charset)` was not added until API 9 :( So if you want to target Froyo and above, you can't do that. – yincrash Sep 07 '12 at 19:15
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);
Jörn Horstmann
  • 33,639
  • 11
  • 75
  • 118
  • 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. http://i.stack.imgur.com/5WPD3.jpg – Royi Namir Oct 30 '14 at 09:03
  • @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). – Jörn Horstmann Oct 30 '14 at 11:55
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.

Arnout Engelen
  • 6,709
  • 1
  • 25
  • 36
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;
  }
thpitsch
  • 2,016
  • 2
  • 21
  • 27
  • 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 – Max Jun 21 '21 at 13:24
4
String s = "ASCII Text";
byte[] bytes = s.getBytes("US-ASCII");
Babar
  • 2,786
  • 3
  • 25
  • 35
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");
dain
  • 6,475
  • 1
  • 38
  • 47
  • 1
    This is actually a pretty good tip! On Android getBytes(...) does NOT work properly even on ICS+ – strange Mar 21 '13 at 01:35
  • I can't find EncodingUtils anywhere? – behelit Apr 15 '16 at 00:22
  • 1
    @behelit if you follow my link it redirects to this bit: http://developer.android.com/about/versions/marshmallow/android-6.0-changes.html#behavior-apache-http-client Basically saying that you need to manually include the Apache HTTP library as it's deprecated now. – dain Apr 16 '16 at 11:48
  • But if you're just looking for the docs, searching for "apache http encodingutils" gives some useful results like: https://hc.apache.org/httpcomponents-core-ga/httpcore/apidocs/org/apache/http/util/EncodingUtils.html – dain Apr 16 '16 at 11:51
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

halfer
  • 19,824
  • 17
  • 99
  • 186
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));
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);
   }
agiles
  • 1,711
  • 3
  • 17
  • 18
-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();
        }
    }
}
nhahtdh
  • 55,989
  • 15
  • 126
  • 162