classicspage
classicspage

Reputation: 161

Is there an equivalent to .NET's System.Text.Encoding in Java?

I'm looking for an equivalent to .NET's System.Text.Encoding in Java. For example in C# I can do this:

Encoding defaultEncoding = Encoding.Default;
byte[] encoded = defaultEncoding.GetBytes("Text to encode");

I would like to be able to do the same in Java.

Upvotes: 1

Views: 5742

Answers (1)

Chris Kannon
Chris Kannon

Reputation: 6091

The java string object has encoding support. getBytes() takes an argument of encoding type. Addendum: Oh and by the way, myStringVar.getBytes() will return platform default encoding of the bytes.

String myStringVar = "Text to get bytes for";
byte[] textBytes = myStringVar.getBytes("UTF-8");

Upvotes: 6

Related Questions