getting string from utf8 in android - android

String strArr[]={"सांखà¥à¤¯à¤¯à¥‹à¤—",
"'करà¥à¤®à¤¯à¥‹à¤—",
"जà¥à¤žà¤¾à¤¨à¤•à¤°à¥à¤®à¤¸à¤‚नà¥à¤¯à¤¾à¤¸à¤¯à¥‹à¤—",
"करà¥à¤®à¤¸à¤‚नà¥à¤¯à¤¾à¤¸à¤¯à¥‹à¤—",
"आतà¥à¤®à¤¸à¤‚यमयोग",
"जà¥à¤žà¤¾à¤¨à¤µà¤¿à¤œà¥à¤žà¤¾à¤¨à¤¯à¥‹à¤—"};
I have UTF-8 code like this when I have converted into string am getting like:
सा�?�?्यय�?�?'�?र्मय�?�?�?्�?ान�?र्मस�?न्यासय�?�?�?र्मस�?न्यासय�?�?�?त्मस�?यमय�?�?
�?्�?ानवि�?्�?ानय�?�?सा�?�?्यय�?�?'�?र्मय�?�?�?्�?ान�?र्मस�?न्यासय�?�?�?र्मस�?
न्यासय�?�?�?त्मस�?यमय�?�?�?्�?ानवि�?्�?ानय�?�?सा�?�?्यय�?�?'�?र्मय�?�?�?्�?ान�?
र्मस�?न्यासय�?�?�?र्मस�?न्यासय�?�?�?त्मस�?यमय�?�?�?्�?ानवि�?्�?ानय�?�?सा�?�?्यय�?
�?'�?र्मय�?�?�?्�?ान�?र्मस�?न्यासय�?�?�?र्मस�?न्यासय�?�?�?त्मस�?यमय�?�?�?्�?ानवि�?्
�?ानय�?�?सा�?�?्यय�?�?'�?र्मय�?�?�?्�?ान�?र्मस�?न्यासय�?�?�?र्मस�?न्यासय�?�?�?
त्मस�?यमय�?�?�?्�?ानवि�?्�?ानय�?�?सा�?�?्यय�?�?'�?र्मय�?�?�?्�?ान�?र्मस�?न्यासय�?
�?�?र्मस�?न्यासय�?�?�?त्मस�?यमय�?�?�?्�?ानवि�?्�?ानय�?�?सा�?�?्यय�?�?'�?र्मय�?�?
�?्�?ान�?र्मस�?न्यासय�?�?�?र्मस�?न्यासय�?�?�?त्मस�?यमय�?�?�?्�?ानवि�?्�?ानय�?�?
please anyone help to get proper string value!
[EDIT]
Code:
public static String convertFromUTF8(String s) {
String out = null;
try {
out = new String(s.getBytes("ISO-8859-1"), "UTF-8");
} catch (java.io.UnsupportedEncodingException e) {
return null;
} return out;
}

assuming you are using eclipse:
right click your project > select properties
select "Resource" on the left
change text file encoding to UTF-8

Try this site http://www.string-functions.com/encodedecode.aspx
Try to find source charset
and end charset of your string
then replace this line in yuor code
out = new String(s.getBytes("CODE OF source charset"), "CODE OF end charset");

Related

showing special character in String in android

I am hitting server and getting some data in string format.
in this data there are some special character like - ' . but when i set that string in textview these special character convert into ? .
So how can i avoid this issue ? please help.
first try :
String t = "<![CDATA["+title+"]]>";
mTitle.setText(Html.fromHtml(text));
second try :
String base64 = Base64.encodeToString(getTitle().getBytes(), Base64.DEFAULT);
byte[] data = Base64.decode(base64, Base64.DEFAULT);
String text = new String(data, StandardCharsets.UTF_8);
mTitle.setText(text);
Try this:
tv.setText(news_item.getTitle().replaceAll("\u2019", "'"));
Refer this link for unicode encoding.

Can I remove the symbol new line from base 64 encoded key file?

I have a base 64 encoded key file. If I open it by Text Editor, I see 4 lines like this:
Then I copy the text and paste to Android Studio, I see the symbol "\n" is generated as below:
This pubic key doesn't work. So I tried :
Remove all "\n" symbol. Still doesn't work.
Replace the "\n" symbol with the space " ". Again doesn't work.
Could you please show me where I am wrong?
Rather than pasting the contents of the file into a string, why not just copy the file itself into your assets folder. For example:
public String readPublicKeyFromFile() {
String publicKeyString; = "";
try {
InputStream is = getAssets().open("public_key.txt");
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
// Convert the buffer into a string.
return new String(buffer);
} catch (IOException e) {
throw new RuntimeException(e);
}
return null;
}
Its android studio console character limitation that it shows long string in multiple lines.
Best way is to copy that string in any text editor(notepad) and make it single line string and then paste it to studio.
Another way is just delete that '\n' character from your string it will be single line string.
e.g.
private static final String = "abcdefgh" +
"ijklmnop" +
"qrstuvwxyz";
just remove '\n' character from your string.
If you creating the "publickey.txt" (base64) file, just use "Base64.NO_WRAP" flag for creating the file. This flag not allow the "\n" character.
By default it takes the "Base64.DEFAULT" flag, so every 64 characters after "\n" will be added automatically.
// for encoding the String with out \n
String base64Str=Base64.encode(your_string,Base64.NO_WRAP);
// for decoding
byte[] resByte=Base64.decode(base64Str,Base64.NO_WRAP);
// convert into String
String resStr=new String(resByte,"UTF-8");

URLEncoder: encoding string in android

I am constructing String in my android app and then pass it to URLEncoder.
String searchStr;
// then I get some searchStr from shared preferences
// i check it is correct
searchStr = searchStr + "/page/"+pageNumber+"";
Then I pass that searchStr to URL encoder:
try {
String url_params = URLEncoder.encode(params[0]);
String result = downloadURL("some url/" + url_params);
Log.d("key", url_params); // -> php/page/10
}catch (IOException){
Log.d("key", e.getMessage() );
}
So here i get IOException which shows me this message:
http://some url/php%2Fpage%2F10
If I copy paste it to browser it shows me Not found error with this message:
The requested url someurl/php/page/10 was not found on this server
Also if I change those strange sign %2F into / in browser I am able to get the page. So how can I construct proper string with / instead of %2F sign?
You don't encode the entire URL, only parts of it that come from "unreliable sources".
String query = URLEncoder.encode("apples oranges", "utf-8");
String url = "http://stackoverflow.com/search?q=" + query;

Android Encoding for this character set

I am getting the html text from the website. this site return the character which like is shown in the below figure. I tried to find the character set from site, it found <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
It show the output on device after set in the text view like:
I tried some coding but doesn't effect the text,which is shown below:
final Charset windowsCharset = Charset.forName("windows-1252");
final Charset utfCharset = Charset.forName("UTF-8");
final CharBuffer windowsEncoded = windowsCharset.decode(ByteBuffer
.wrap(ne.scrape_detail_article_text.getBytes()));
final byte[] utfEncoded = utfCharset.encode(windowsEncoded).array();
// System.out.println(new String(utfEncoded, utfCharset.displayName()));
String s = "" ;
try {
// String s = new String(utfEncoded, utfCharset.displayName());
//String s = new String(texttoencoding.getBytes("windows-1252"),"UTF-8");
s = URLEncoder.encode(texttoencoding, "windows-1252");
Log.e("LOG", "Encoded >> " + s);
} catch (UnsupportedEncodingException e) {
Log.e("utf8", "conversion", e);
}
TextviewToset.setText(Html.fromHtml(texttoencoding);
TextviewToset.setMovementMethod(LinkMovementMethod.getInstance());
Please Help me, how can I encode this text into UTF-8 And display in the textview?
Thanks in Advance
Looks like you are dealing with HTML-Entites here. Therefore you have to decode the HTML Entities via:
String text = HTML.fromHtml(yourText).toString();
This will give you the correct UTF-8 characters. The documentation for Html.fromHtml() is here

android: declare huge string in strings.xml

i have a very huge HTML string in my app.
When I use it in the code, everything is fine but when I try to declare it in strings.xml, I am getting some errors. Is there a way to make a simple copy of the string in strings.xml? Thank you
HTML and XML are the same basic language, I do not believe that you can store HTML in a string, why not save the html page and package it with the application?
Save the page as a html page in res > raw and then call this method
String html = Utils.readRawTextFile(ctx, R.raw.rawhtml);
public static String readRawTextFile(Context ctx, int resId)
{
InputStream inputStream = ctx.getResources().openRawResource(resId);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
int i;
try {
i = inputStream.read();
while (i != -1)
{
byteArrayOutputStream.write(i);
i = inputStream.read();
}
inputStream.close();
} catch (IOException e) {
return null;
}
return byteArrayOutputStream.toString();
}
Error may come at special characters like # double quote single quote etc. to overcome it prefix \ to it and your error get resolved
if you assign same string programmatically there also you will find the same issue
String mString= "your huge string with # error";
in this also you have to overcome be prefixing backslash
String mString= "your huge string with \# error";

Categories

Resources