how to converting byte array to string? - android

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

Related

Cannot VIew the Data Sent By a TCP Packet Sending Program(Packet Server)

I'm trying to develop a small app which receives some data via sockets and based on the data it receives,it prints a toast message for the user.I' getting the data,but the data apparently cannot be read properly.Here is the relavent portion for the same.
int red = -1;
byte[] buffer = new byte[1024]; // a read buffer of 5KiB
byte[] redData;
while ((red = cs.getInputStream().read(buffer)) > -1) {
String redDataTextappend;
redData = new byte[red];
redDataTextappend = new String(redData);
Log.w("Data got",redDataTextappend);
if (redDataTextappend == "hi")
{
//Display Toast message using runonUIThread(new Runnable);
}
else
{//Display Message Using runonUITHread(new Runnable);
}
This code runs on a separate thread as android does not allow networking operations on a separate thread.
The 4 Diamonds is the data displayed by the android studio and cs is the name of the socket which accepts connections.
Thanks.
You are simply printing the String that encodes to zero bytes since you never copy the data that is read in.
It would make more sense to convert the byte array to a hex string if the array contains arbitrary data: see the answers to this question for options for that.
If the array contains the encoding of a String in some charset, for example UTF-8, then do the following:
byte[] redData = Arrays.copyOf(buffer, red);
String redDataTextappend = new String(redData, Charset.forName("UTF-8"));
Log.w("Data got",redDataTextappend);

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.

How to display x509certifcate in android app

I use spongycastle. I downloaded byte array certificate from webservice in android app (I think is base64 byte array) and want to display it on screen. I wrote below code but It does not work and c variable is null.
in addition I don't know how to display it on screen.
SoapObject result = (SoapObject)envelope.bodyIn;
if(result != null)
{
Object cert= result.getProperty(0);
InputStream is = new ByteArrayInputStream(cert.toString().getBytes());
CertificateFactory cf = CertificateFactory.getInstance("X.509","BC");
java.security.cert.Certificate c = cf.generateCertificate(is);
X509Certificate t = (X509Certificate) c;
System.out.println("ca=" + t.getSubjectDN());
}
Probably cert.toString().getBytes() is the problem. A certificate is encoded in binary format. To build it you need a byte array, not a string converted to bytes.
Ensure in which format are you receiving the certificate in order to transform it into binary. If you think that the certificate is encoded in base64, decode it in this way
byte[] data = Base64.decode(base64, Base64.DEFAULT);
Note also the Java Security API Provider name for SpongyCastle is SC rather than BC
To display a text you need for example to set the contento of a TextView inside an Activity or Fragment. I suggest you to get a tutorial first

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