• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

About creating string from Base64 encoded byte array and then invoking getBytes...

 
Rancher
Posts: 1369
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I have a Base64 encoded byte array(encoded using Base64.encodeBase64(..) method of org.apache.commons.codec.binary.Base64 class) and I create a String using this byte array:
String aString = new String(base64EncodedArray);
will the constructor affect the data in byte array or specifically hurt the encoding in any way?

If I do aString.getBytes() will the resulting byte array contain same byte stream as base64EncodedArray?

Thanks in advance.
 
Rancher
Posts: 43028
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is easy enough to test with a few lines of code, but I think the answer is "yes" for both cases. Base-64 only uses non-control characters that are part of US-ASCII - those should have the same encoded values in just about any encoding.

As a rule of thumb, you shouldn't use new String(byte[]) and String.getBytes() without specifying the encoding; that's just asking for trouble.
 
Marshal
Posts: 76862
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Too difficult a question for beginning Java. Moving.
 
Monu Tripathi
Rancher
Posts: 1369
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

This is easy enough to test with a few lines of code, but I think the answer is "yes" for both cases. Base-64 only uses non-control characters that are part of US-ASCII - those should have the same encoded values in just about any encoding.

As a rule of thumb, you shouldn't use new String(byte[]) and String.getBytes() without specifying the encoding; that's just asking for trouble.



Ulf: Thank you for your reply.

I tested the equality of(==) encoded characters in byteArray and characters in the array returned by String.getBytes(). Both were equal.
I also tested this by writing image data in Byte array, encoding it and then use the String constructor, getting the bytes and again reconstructing the image back. The image was same with no distortion(difference).
So, I think it is safe to assume that encoding is not hurt, atleast in this case.

Also, thanks for your advice about String constructor usage.

Cheers!
 
She's brilliant. She can see what can be and is not limited to what is. And she knows this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic