showing special character in String in android - 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.

Related

how to converting byte array to string?

I have a byte array and i want to convert it to arabic string in android.
byte[] data = new byte[] {(byte)79,(byte)144,(byte)2,(byte)0,(byte)0,(byte)166,(byte)6,(byte)0,(byte)0,(byte)0,(byte)0,(byte)0,(byte)128,(byte)47,(byte)60,(byte)70,(byte)79,(byte)78,(byte)84,(byte)32,(byte)67,(byte)79,(byte)76,(byte)79,(byte)82,(byte)61,(byte)34,(byte)35,(byte)57,(byte)48,(byte)57,(byte)48,(byte)57,(byte)48,(byte)34,(byte)62,(byte)199,(byte)157,(byte)32,(byte)98,(byte)97,(byte)110,(byte)194,(byte)180,(byte)64,(byte)32,(byte)8,(byte)192,(byte)100,(byte)199,(byte)157,(byte)110,(byte)60,(byte)47,(byte)198,(byte)8,(byte)62,(byte)47,(byte)60,(byte)47,(byte)112,(byte)62,(byte)174,(byte)11,(byte)48,(byte)48,(byte)55,(byte)48,(byte)48,(byte)184,(byte)118,(byte)116,(byte)46,(byte)60,(byte)47,(byte)102,(byte)111,(byte)110,(byte)116,(byte)62,(byte)10,(byte)9,(byte)0,(byte)0,(byte)128,(byte)160,(byte)112,(byte)32,(byte)97,(byte)108,(byte)105,(byte)103,(byte)110,(byte)61,(byte)114,(byte)105,(byte)103,(byte)104,(byte)116,(byte)32,(byte)100,(byte)105,(byte)114,(byte)61,(byte)114,(byte)116,(byte)108,(byte)62,(byte)60,(byte)70,(byte)8,(byte)32,(byte)99,(byte)111,(byte)108,(byte)111,(byte)64,(byte)101,(byte)32,(byte)0,(byte)0,(byte)128,(byte)100,(byte)62,(byte)226,(byte)151,(byte)143,(byte)82,(byte)13,(byte)32,(byte)216,(byte)177,(byte)217,(byte)135,(byte)216,(byte)167,(byte)32,(byte)218,(byte)169,(byte)216,(byte)177,(byte)216,(byte)175,(byte)217,(byte)134,(byte)216,(byte)140,(byte)32,(byte)216,(byte)175,(byte)216,(byte)179,(byte)216,(byte)170,(byte)17,(byte)128,(byte)85,(byte)140,(byte)134,(byte)4,(byte)180,(byte)219,(byte)140,(byte)18,(byte)5,(byte)217,(byte)133,(byte)216,(byte)170,(byte)217,(byte)136,(byte)217,(byte)130,(byte)217,(byte)129,(byte)166,(byte)10,(byte)72,(byte)132,(byte)170,(byte)14,(byte)181,(byte)57,(byte)1,(byte)129,(byte)44,(byte)184,(byte)216,(byte)177,(byte)26,(byte)6,(byte)234,(byte)37,(byte)108,(byte)101,(byte)102,(byte)177,(byte)0,(byte)1,(byte)134,(byte)142,(byte)37,(byte)108,(byte)116,(byte)114,(byte)170,(byte)37,(byte)78,(byte)52,(byte)99,(byte)69,(byte)3,(byte)100,(byte)111,(byte)32,(byte)110,(byte)111,(byte)116,(byte)32,(byte)97,(byte)17,(byte)4,(byte)100,(byte)111,(byte)110,(byte)32,(byte)104,(byte)111,(byte)112,(byte)101,(byte)131,(byte)149,(byte)112,(byte)0,(byte)198,(byte)18,(byte)97,(byte)48,(byte)52,(byte)49,(byte)192,(byte)97,(byte)12,(byte)145,(byte)34,(byte)62,(byte)216,(byte)167,(byte)217,(byte)133,(byte)198,(byte)48,(byte)149,(byte)3,(byte)141,(byte)3,(byte)216,(byte)167,(byte)216,(byte)178,(byte)86,(byte)55,(byte)145,(byte)2,(byte)175,(byte)217,(byte)135,(byte)79,(byte)157,(byte)0,(byte)3,(byte)144,(byte)81,(byte)0,(byte)116,(byte)104,(byte)101,(byte)121,(byte)86,(byte)40,(byte)101,(byte)100,(byte)32,(byte)60,(byte)105,(byte)114,(byte)0,(byte)0,(byte)3,(byte)160,(byte)32,(byte)97,(byte)116,(byte)116,(byte)101,(byte)109,(byte)112,(byte)116,(byte)32,(byte)116,(byte)111,(byte)32,(byte)101,(byte)115,(byte)99,(byte)97,(byte)131,(byte)28,(byte)91,(byte)0,(byte)161,(byte)2,(byte)218,(byte)169,(byte)217,(byte)136,(byte)216,(byte)180,(byte)216,(byte)180,(byte)32,(byte)216,(byte)168,(byte)70,(byte)47,(byte)219,(byte)93,(byte)70,(byte)131,(byte)196,(byte)181,(byte)5,(byte)129,(byte)70,(byte)2,(byte)101,(byte)5,(byte)62,(byte)104,(byte)175,(byte)131,(byte)157,(byte)1,(byte)1,(byte)217,(byte)130,(byte)165,(byte)4,(byte)37,(byte)7,(byte)175,(byte)216,(byte)176,(byte)133,(byte)1,(byte)180,(byte)169,(byte)7,(byte)74,(byte)115,(byte)170,(byte)216,(byte)177,(byte)218,(byte)169,(byte)3,(byte)155,(byte)227,(byte)0,(byte)104,(byte)101,(byte)162,(byte)72,(byte)104,(byte)105,(byte)115,(byte)132,(byte)32,(byte)1,(byte)8,(byte)232,(byte)109,(byte)112,(byte)97,(byte)110,(byte)105,(byte)77,(byte)7,(byte)105,(byte)110,(byte)198,(byte)76,(byte)32,(byte)109,(byte)105,(byte)100,(byte)100,(byte)108,(byte)101,(byte)32,(byte)111,(byte)102,(byte)138,(byte)3,(byte)106,(byte)111,(byte)117,(byte)114,(byte)110,(byte)101,(byte)121,(byte)3,(byte)155,(byte)154,(byte)0,(byte)175,(byte)105,(byte)4,(byte)205,(byte)4,(byte)192,(byte)225,(byte)3,(byte)194,(byte)179,(byte)216,(byte)183,(byte)32,(byte)216,(byte)179,(byte)6,(byte)75,(byte)161,(byte)4,(byte)10,(byte)4,(byte)170,(byte)32,(byte)216,(byte)174,(byte)36,(byte)86,(byte)129,(byte)166,(byte)53,(byte)3,(byte)30,(byte)157,(byte)1,(byte)238,(byte)21,(byte)179,(byte)217,(byte)132,(byte)219,(byte)140,(byte)217,(byte)133,(byte)131,(byte)162,(byte)152,(byte)0,(byte)109,(byte)115,(byte)101,(byte)108,(byte)105,(byte)4,(byte)0,(byte)17,(byte)158,(byte)129,(byte)111,(byte)32,(byte)108,(byte)101,(byte)99,(byte)104,(byte)101,(byte)114,(byte)131,(byte)155,(byte)140,(byte)0,(byte)167,(byte)217,(byte)136,(byte)219,(byte)85,(byte)0,(byte)216,(byte)180,(byte)217,(byte)135,(byte)100,(byte)10,(byte)147,(byte)131,(byte)159,(byte)135,(byte)0,(byte)143,(byte)36,(byte)2,(byte)219,(byte)140,(byte)81,(byte)10,(byte)245,(byte)8,(byte)168,(byte)219,(byte)140,(byte)226,(byte)128,(byte)140,(byte)60,(byte)183,(byte)67,(byte)128,(byte)216,(byte)168,(byte)134,(byte)167,(byte)97,(byte)6,(byte)198,(byte)177,(byte)241,(byte)10,(byte)219,(byte)140,(byte)3,(byte)26,(byte)46,(byte)0,(byte)202,(byte)165,(byte)105,(byte)1,(byte)167,(byte)177,(byte)10,(byte)121,(byte)1,(byte)180,(byte)217,(byte)2,(byte)198,(byte)123,(byte)40,(byte)216,(byte)185,(byte)217,(byte)129,(byte)131,(byte)150,(byte)181,(byte)0,(byte)99,(byte)104,(byte)105,(byte)108,(byte)100,(byte)105,(byte)115,(byte)104,(byte)47,(byte)24,(byte)0,(byte)136,(byte)86,(byte)92,(byte)3,(byte)27,(byte)174,(byte)0,(byte)50,(byte)34,(byte)142,(byte)237,(byte)175,(byte)57,(byte)5,(byte)167,(byte)217,(byte)134,(byte)217,(byte)135,(byte)3,(byte)144,(byte)43,(byte)0,(byte)131,(byte)143,(byte)90,(byte)0,(byte)98,(byte)108,(byte)97,(byte)99,(byte)107,(byte)34,(byte)32,(byte)115,(byte)116,(byte)121,(byte)108,(byte)101,(byte)61,(byte)34,(byte)134,(byte)6,(byte)45,(byte)119,(byte)101,(byte)1,(byte)2,(byte)124,(byte)128,(byte)70,(byte)17,(byte)58,(byte)98,(byte)111,(byte)108,(byte)100,(byte)34,(byte)62,(byte)42,(byte)86,(byte)49,(byte)105,(byte)110,(byte)103,(byte)32,(byte)115,(byte)104,(byte)105,(byte)112,(byte)3,(byte)155,(byte)102,(byte)0,(byte)142,(byte)198,(byte)197,(byte)6,(byte)201,(byte)8,(byte)82,(byte)17,(byte)60,(byte)47,(byte)112,(byte)62};
String head2=new String(data);
but i get this string :
O�����������������/ǝ ban´#
�dǝn/�00700�vt. ������p align=right dir=rtl>●R رها کردن، دست�U���یمتوقف�
H���9�,�ر�%lef�����%ltr�%N4cEdo not adon
hope��p���a041�a�">ام�0��ازV7��هO����Q��theyV(ed h���ق�%�ذ���Js�رک����he�Hhis� �mpaniMin�L
middle of�journey�����i���³ط سK� �
خ$V��5���لیم�����mseli����o lecher������و�U��شهd �������$یQ
��ی‌<�C�ب��aƱ� ی.��ʥi��
y���{(عف�����childish/���V\���2"��9�نه�+����Z��black"
style="�-we|�F:bold">*V1ing ship�f������R
by the way my string contains html tag too.
so what is the problem?
it seems like your byte array is using UTF-8 as it's default charset (for more read here.
try changing to something like this:
byte[] data == // getData();
String str = new String(data, StandardCharsets.US_ASCII);
Similar problem here: How to convert Java String into byte[]?
"[A]ll String objects in Java are stored internally as UTF-16. When converting to a byte[] you will get a different breakdown of bytes for the given glyphs of that String, depending upon the chosen charset."
So you have to specify a charset as well: String string = new String(byte[] bytes, Charset charset);
For example UTF-16 or ASCII

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");

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

Sending an image as a Base64.encodeToString from android to c# how to get that image

From my android app I am sending an image to C# server converting it to Base64
#Override
public void onPictureTaken(byte[] data, Camera camera)
{
String image = Base64.encodeToString(data, Base64.DEFAULT);
sendtoserver(image);
}
from the server side I received a string but don't know how to convert it and save it.For help I am getting this string tell me how to convert it and save it in C#
/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH/2wBDAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQ
Did you try using Convert.FromBase64String(string s) ?
Source: http://msdn.microsoft.com/en-us/library/system.convert.frombase64string.aspx
UPDATE (posted here instead of in the comments, since the code would display better)
Your Base64 representation is incorrect. For the image you gave in the comments, the Base64 representation is 339801 characters long.
I used the following code to generate a Base64 representation:
string path = #"C:\1XlqZF2.jpg";
Image img = Image.FromFile(path);
byte[] arr;
using (MemoryStream ms = new MemoryStream())
{
img.Save(ms, ImageFormat.Jpeg);
arr = ms.ToArray();
}
String b64 = Convert.ToBase64String(arr);
What is the Base64 class you are using ? I couldn't find it on MSDN.

Why are foreign characters not read using inputStream?

I have a text file which contains data I need to preload into a SQLite database. I saved in in res/raw.
I read the whole file using readTxtFromRaw(), then I use the StringTokenizer class to process the file line by line.
However the String returned by readTxtFromRaw does not show foreign characters that are in the file. I need these as some of the text is Spanish or French. Am I missing something?
Code:
String fileCont = new String(readTxtFromRaw(R.raw.wordstext));
StringTokenizer myToken = new StringTokenizer(fileCont , "\t\n\r\f");
The readTxtFromRaw method is:
private String readTxtFromRaw(Integer rawResource) throws IOException
{
InputStream inputStream = mCtx.getResources().openRawResource(rawResource);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
int i = inputStream.read();
while (i != -1)
{
byteArrayOutputStream.write(i);
i = inputStream.read();
}
inputStream.close();
return byteArrayOutputStream.toString();
}
The file was created using Eclipse, and all characters appear fine in Eclipse.
Could this have something to do with Eclipse itself? I set a breakpoint and checked out myToken in the Watch window. I tried to manually replace the weird character for the correct one (for example í, or é), and it would not let me.
Have you checked the several encodings?
what's the encoding of your source file?
what's the encoding of your output stream?
the byteArrayOutputStream.toString() converts according to the platform's default character encoding. So I guess it will strip the foreign characters or convert them in a way that they are not displayed in your output.
Have you already tried to use byteArrayOutputStream.toString(String enc)? Try "UTF-8" or "iso-8859-1" or "UTF-16" for the encoding.

Categories

Resources