0

I found this: <form action="https://www.quadrigacx.com/authenticate" class="form-horizontal" role="form" method="post" accept-charset="utf-8"> and I assume that the accept-charset="utf-8" means I must convert the input values to utf-8. If that is the case then how do I do it? I have tried: String gcText = new String(googleCode.getText().getBytes("ISO-8859-1"),"UTF-8");String ecText = new String(emailCode.getText().getBytes("ISO-8859-1"),"UTF-8"); but it did not work. Any help appreciated, thanks!

sandra burgle
  • 47
  • 1
  • 8
  • Please [edit] the code in the question to make it clear that it should not be copied, hopefully preventing another victim of this misformation. – Tom Blodget Nov 28 '17 at 05:15

1 Answers1

1

hello please try this as

String gcText = new String(googleCode.getText().getBytes("UTF-8"),"UTF-8");
String ecText = new String(emailCode.getText().getBytes("UTF-8"),"UTF-8");

A string needs no encoding. It is simply a sequence of Unicode characters.

You need to encode when you want to turn a String into a sequence of bytes. The charset the you choose (UTF-8, cp1255, etc.) determines the Character->Byte mapping. Note that a character is not necessarily translated into a single byte. In most charsets, most Unicode characters are translated to at least two bytes.

Encoding of a String is carried out by:

 String s1 = "some text";
 byte[] bytes = s1.getBytes("UTF-8"); // Charset to encode into

You need to decode when you have а sequence of bytes and you want to turn them into a String. When yоu dо that you need to specify, again, the charset with which the bytеs were originally encoded (otherwise you'll end up with garblеd tеxt). Decoding:

String s2 = new String(bytes, "UTF-8"); // Charset with which bytes were encoded 
Vikas Suryawanshi
  • 522
  • 1
  • 5
  • 12
  • Hi, thank you for the response! I will try this tomorrow when I get home, thank you! – sandra burgle Nov 24 '17 at 05:57
  • This helped me understand strings and bytes better thanks! [Here is solution for anybody who also stuck on similar problem](https://stackoverflow.com/questions/47403851/logging-into-a-website-jsoup) – sandra burgle Nov 25 '17 at 01:50