3

I have an C# function which i want to translate in Java code. I have a problem here:

Encoding enc = Encoding.GetEncoding("Windows-1252");

bytZeichenBenutzer = enc.GetBytes(strBenutzer.Substring(intLoopCount, 1).ToCharArray());

How to do that in Java? I can't find anything similar only stuff that works with UTF-8.

3 Answers 3

5

You can use the getBytes(String) or getBytes(Charset) methods:

String myString = getMyStringFromSomeWhere();
byte[] utf8Bytes = myString.getBytes("UTF-8");
// or
Charset myCharset = Charset.forName("Windows-1252");
byte[] windowsBytes = myString.getBytes(myCharset);
Sign up to request clarification or add additional context in comments.

Comments

0
String s = "hhh"; 
try {   
  s.getBytes("Windows-1252"); 
} catch(UnsupportedEncodingException e) { 
  e.printStackTrace();  
}

Comments

0

You can do:

byte[] a = "some string".getBytes("Windows-1252");

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.