0

I am reading a PDF file (as bytes) using this code

byte[] bytes=FileUtils.readFileToByteArray(pdf);

i want to convert this byte[] to base64 string and send it to a client.

I am getting String value as [B@42109d30. And I want to fill byte[] with the same value [B@42109d30.

I tried to fetch each character of string into a Byte and then assign this Byte value to byte[] , but even then the final value of byte[] changes.

String getbase64 = "[B@42109d30";
String a = String.valueOf((getbase64.charAt(i)));

if (a.equals("@") || a.equals("d") ) {
    bytes[i]=0; 
} else {
    b = Byte.valueOf(""+a);
    bt = b;
    bytes[i] = bt;
    Log.d("bytes", bytes.toString());
}

Please help,thank you

Yazan
  • 6,074
  • 1
  • 19
  • 33
Karan Gada
  • 65
  • 7
  • 1
    i don't think this `[B@42109d30` is an actual value , its the result of calling `toString()` on an object. where did you get this value, and what kind of object it's stored in? – Yazan Nov 06 '16 at 07:47
  • yes, it is result of tostring called on byte[] variable, what i am doing is ,i am reading a pdf file as" byte[] bytes=FileUtils.readFileToByteArray(pdf) " and then instead of converting this value to base64 string i want to send this byte[] to client. so how to get this byte[] value on client app in its original form – Karan Gada Nov 06 '16 at 07:58
  • well, this is a different story, PDF is not stored as plain text or just a bytes array need to be converted to string, it has it's own format (still bunch of bytes) but its not convertible to string directly, usually `byte[]` to string is a simple as `String a = new String(byteArray, "UTF8");` but this is not the case (i think) you need to get a lib or something that can handle PDF (P.S i have added PDF tag to ur question to get better help) – Yazan Nov 06 '16 at 08:00
  • yes but which library should i use cause i dont want to store base64 string in my database as base64 values are of size greater than the original pdf – Karan Gada Nov 06 '16 at 08:03
  • i have edited the question for you. you can have a look here http://stackoverflow.com/questions/7360403/base-64-encode-and-decode-example-code on the accepted answer, it says how to convert byte[] to base64, then you can send it to your client. (note base64 is not the regular string that you can create from byte[], it has specific format and a way to poduce, use the answer link to find out) – Yazan Nov 06 '16 at 08:13
  • ive already done that thing, and the whole point being instead of storing base64 string on database n den that being fetched by client is not what i want . eg- uploading pdf size 2.3MB , after geting this into byte[] n then encoding it into BASE64 string which is now of size 3.2 MB, now to store this string on database doent make any sense , why should cient download a pdf of size 2.3mb in base64 form string of size 3.2 mb – Karan Gada Nov 06 '16 at 08:31
  • , so instead i want to store byte[] in sql database n send this to client n then on client side would encode this byte[] to base64 n decode it there itself so as to get the original pdf . SO IS THIS POSSIBLE ? – Karan Gada Nov 06 '16 at 08:31
  • what do you mean by send to client? if you want to upload to a server, then so it as `upload` if `client` is a web-service, then it may not allow you to send byte[] , or it may? and what is the rule of database in this situation? is it on ur side or the client side ? – Yazan Nov 06 '16 at 08:35

1 Answers1

2

You can convert string to byte[] and vice versa this way :

byte[] bytes = text.getBytes("UTF-8");
String text = new String(bytes, "UTF-8");
Omid Heshmatinia
  • 5,089
  • 2
  • 35
  • 50