Result gets \n while converting bytes to base64 String - android

I am trying to convert bytes to base64 String and in the resultant string i am getting \n, how can i get base64 result without these escape sequences, i know i can remove them from the string but the point is why does it ad these extra characters.
That's how i am converting my bytes to base64 string
/*
* Convert bytes to Base64
*/
public static String convertBytesToBase64(byte[] bytes) {
return new String(Base64.encode(bytes, 0));
}

Base64.NO_WRAP Encoder flag bit to omit all line terminators (i.e., the output will be on one long line).
public static String convertBytesToBase64(byte[] bytes) {
return new String(Base64.encodeToString(bytes,Base64.NO_WRAP));
}

Related

I cant able to convert the base64 encoded string to hex string in android?

I am using AES (Rijndael) encryption method to encrypt a string. The transformation used is ECB_PKCS5PADDING. It will return a base64 encoded string. I had decoded the string using base64 and converted to hex using a function. The code snippets are attached below.
private void encrptString()
{
sample = "<xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
String encrypt = Encoder.BuilderAES()
.message(sample)
.method(AES.Method.AES_ECB_PKCS5PADDING)
.key("xxxxxxxxxxxxxx")
.keySize(AES.Key.SIZE_128)
.encrypt();
System.out.println("encrypt"+encrypt);
String stringFromBase = new String(Base64.decode(encrypt,Base64.DEFAULT));
System.out.println("base64"+stringFromBase.trim());
String hex= null;
try {
hex = toHex(stringFromBase);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
Toast.makeText(this, hex, Toast.LENGTH_SHORT).show();
System.out.println("hex"+hex.trim());
}
public String toHex(String arg) throws UnsupportedEncodingException {
return String.format("%040x", new BigInteger(1, arg.getBytes("UTF-8")));
}
base64 Input String:
O3Kmyftikp0lY/8SVWMiLFCn9sdQX7HauqdLZXcE+jkVcZH7kfPD/
jSLIC2wlP1iQTaiynoaW8A7L1INEYunkmCWT2eVwumwouHVT7d5Kq
/sqEcB9/9pyyAuSjrQxtCgJlHm7dlvt0lVEHD2pHnEAhZTRtuuy6b
8MNI7W4jLn+/ZNEqZxdJll26a52D7ceFv
Expected output:
3b72a6c9fb62929d2563ff125563222c50a7f6c7505fb1dabaa74b657704fa391571
91fb91f3c3fe348b202db094fd624136a2ca7a1a5bc03b2f520d118ba79260964f67
95c2e9b0a2e1d54fb7792aafeca84701f7ff69cb202e4a3ad0c6d0a02651e6edd96fb
749551070f6a479c402165346dbaecba6fc30d23b5b88cb9fe
fd9344a99c5d265976e9ae760fb71e16f
I am getting this output:
3b72efbfbdefbfbdefbfbd62efbfbdefbfbd2563efbfbd125563222c50efbfbdefbfbd
efbfbd505fefbfbddabaefbfbd4b657704efbfbd391571efbfbdefbfbdefbfbdefbfbd
efbfbd34efbfbd202defbfbdefbfbdefbfbd624136efbfbdefbfbd7a1a5befbfbd3b2f
520d11efbfbdefbfbdefbfbd60efbfbd4f67efbfbdefbfbde9b0a2efbfbdefbfbd4fef
bfbd792aefbfbdefbfbd4701efbfbdefbfbd69efbfbd202e4a3aefbfbdefbfbdd0a026
51efbfbdefbfbdefbfbd6fefbfbd49551070efbfbd79efbfbd02165346dbaecba6efbf
bd30efbfbd3b5befbfbdcb9fefbfbdefbfbd344aefbfbdefbfbdefbfbd65efbfbd6eef
bfbdefbfbd60efbfbd
I'd leave the decoded Base64 as a byte[] and encode that:
String b64 = "O3Kmyftikp0lY/8SVWMiLFCn9sdQX7HauqdLZXcE+jkVcZH7kfPD/ jSLIC2wlP1iQTaiynoaW8A7L1INEYunkmCWT2eVwumwouHVT7d5Kq /sqEcB9/9pyyAuSjrQxtCgJlHm7dlvt0lVEHD2pHnEAhZTRtuuy6b 8MNI7W4jLn+/ZNEqZxdJll26a52D7ceFv";
StringBuilder hexStringBuilder = new StringBuilder();
for (byte b : Base64.decode(b64, Base64.DEFAULT)) {
hexStringBuilder.append(String.format("%02x", b));
}
String hex = hexStringBuilder.toString()
You should base64-decode to a byte array (byte[]) and then hex encode the byte array to a string.

How to fill byte[] with specific character or string in android

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
You can convert string to byte[] and vice versa this way :
byte[] bytes = text.getBytes("UTF-8");
String text = new String(bytes, "UTF-8");

Base64 decode for image/jpeg;base64 in android

I am trying to decode base64 encoded string into bitmap in android.
The encoded string will start with "image/jpeg;base64,"
I use the following method:
public static Bitmap decodeBase64(String input)
{
byte[] decodedByte = Base64.decode(input, Base64.DEFAULT);
// also tried using Base64.URL_SAFE , Base64.NO_PADDING
return BitmapFactory.decodeByteArray(decodedByte, 0, decodedByte.length);
}
Assume that I am calling the above method as
ImageView get_img=(ImageView)fundViewByID(R.id.getImg);
get_img.setImageBitmap(ImageCache.decodeBase64(url))
When I run the code it either throws
--- SkImageDecoder::Factory returned null
or
bad base-64 with IllegalArgumentException
The below is base64 string
data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAkGBxQSEhUUEhQUFRQVFBQUFBQXFxQWGBQWFxUWFhQXFRUYHSggGBwlHBQUITEhJSkrLi4uFx8zODMsNygtLisBCgoKDg0OGxAQGywmHyQsLCwsLC4sLywsLCwsLCwsLCwsLCwvLCwsLCwsLCwsLCwsLCwsLCwsLCwsLCwsLCwsLP/AABEIAJcBTgMBIgACEQEDEQH/xAAbAAACAwEBAQAAAAAAAAAAAAACAwABBAUGB//EAEUQAAEDAgIGBwQHBwMDBQAAAAEAAhEDIRIxBEFRcYGRBSJhobHB0RMykvAGFBVCUuHxU1RicoKT0jOiskNE4hYjNGNz/8QAGwEAAwEBAQEBAAAAAAAAAAAAAAECAwQFBgf/xAAwEQACAgECBAIKAwEBAQAAAAAAAQIRAxIhBDFBURNhBRQiQoGRobHh8FJx0TLxFf/aAAwDAQACEQMR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WTznxCH2rmmDHL9VFFrbVkJJoa58nqkneBBjO1kgVNQwk3tBEc1FERbYaVuEHuv1Z7QR3zlwSTWvBaZGwjzUUVW7qwik1yP/9k=
The string "data:image/jpeg;base64," is not a valid base64 Encoded string. So it must be removed before decoding.
here you go.
String encodedDataString = "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAkGBxQSEhUUEhQUFRQVFBQUFBQXFxQWGBQWFxUWFhQXFRUYHSggGBwlHBQUITEhJSkrLi4uFx8zODMsNygtLisBCgoKDg0OGxAQGywmHyQsLCwsLC4sLywsLCwsLCwsLCwsLCwvLCwsLCwsLCwsLCwsLCwsLCwsLCwsLCwsLCwsLP/AABEIAJcBTgMBIgACEQEDEQH/xAAbAAACAwEBAQAAAAAAAAAAAAACAwABBAUGB//EAEUQAAEDAgIGBwQHBwMDBQAAAAEAAhEDIRIxBEFRcYGRBSJhobHB0RMykvAGFBVCUuHxU1RicoKT0jOiskNE4hYjNGNz/8QAGwEAAwEBAQEBAAAAAAAAAAAAAAECAwQFBgf/xAAwEQACAgECBAIKAwEBAQAAAAAAAQIRAxIhBDFBURNhBRQiQoGRobHh8FJx0TLxFf/aAAwDAQACEQMRAD8A5+FSEzArwL64+A3FwpCZgVhiA3FwpCbgUwJC3FwpCaGK8CLDcVCmFO9mrDEWFMThUhPwK/ZosNLM8K8Kf7NTAixaWIwqQnezU9mixUxMKQm4FMKLEKhSE3AqLEBYuFITMKmFAWLhVCZhUhAWLhSEyFITDULhSEcKYUBYuFITIUhA7FQpCZCkICxcKoTYVQgdi4VQmwqhFhYuFUJkKQmOxUKQmQqhA7FwpCZCqEDs2imrwJ4YrwLDUb6DPgRexT8CvCjUGkz+zUFNaMCvAjULSZ8CsU0/2aL2aNQtJnwKYFowK8CVhRnwKYFpwKYEWKjNgV4FowKYEWFGbApgWksVYE9RLRmwKYFowKYE7JaM2BTAtGFTAiyaM2BTAtBaqwJ2KjNgQli1FiEtTslozlirCtGBUWJ2TRnw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TAOwW7goonzqyG6uhwYANhjM3sTfIWyCjpLRhLtc5QIyjWooopWVboChTEAmZdOvXrIAsq0mm5omSQMyDHFRRTNbjg7RVKqTmesMna9x+SoylItn88FFE9EdNiU5OVA0asOLX5tG+PmFppFjx94do1bxN1FFCd8zSq5F1NGc28iM5vw7U2iXRMjfH5BRRPkhtWxrtKc3XLdZMEciJ8UTaw2luuBrv2BRRDlSsWncA6SJPXJ7CDvPZlKaykHZkZTlGedwJ2KlFljyOU9LLnjUY2hYols4nHLqkajkMQvbdtWY6Q7FDyPGRE9iiic8sovYUcMJLcZ9otgADxg8DKSdIbJJbBGy3gYUUXRBKa3RzSuL2YWCm6Ze5picp8BbmhGhBvWa8kDO0W4lUoo0xtqkW5SpOw6DpdYujDYSL75hGKjri1pizYEbZ17lFEpwUY2hQm5yplMc8yScvvdu5NNT8WWTjDe6ZKiiyeWVpdzTwY02W4tuW3i8QPGAVkr1YiYzibWOcSROsc1FEeJJNrsLw4tJ9x7W4RiIBBiD6/oipgOHVE9hy3QVFFnHPKUU3XOi5YYxk0m+QP1IOyJYRkAQQdsCAAgcI90hx2EEH0/VRRdsYI4XNidI0gs6tSROrqlU2hOQvnqHmFaizvematVG0TA7WTznxCH2rmmDHL9VFFrbVkJJoa58nqkneBBjO1kgVNQwk3tBEc1FERbYaVuEHuv1Z7QR3zlwSTWvBaZGwjzUUVW7qwik1yP/9k="
encdoedDataString = encodedDataString.replace("data:image/jpeg;base64,","");
byte[] imageAsBytes = Base64.decode(encodedDataString.getBytes(), 0);
imgView.setImageBitmap(BitmapFactory.decodeByteArray(
imageAsBytes, 0, imageAsBytes.length));
You don't need the data:image/jpeg;base64, at the start of the string, that is actually not part of Base64 encoding per se but a Base64 Data URI for a web browser. Just pass the string from /9j/4AAQSkZJRgABAQAAAQABA... onwards and it will decode properly.

NSNonLossyASCIIStringEncoding equivalent for Android

I got to port some chat code from iOS to Android. Before sending the chat message to the socket, the iOS code uses the NSNonLossyASCIIStringEncoding class as parameter of the NSString::dataUsingEncoding.
How would you do it in Android? Same question about the opposite decoding.
Without doing that, for instance, the line breaks disappear in the message received on the other mobile.
Code on iOS:
NSData *data1 = [myStringTosend dataUsingEncoding:NSNonLossyASCIIStringEncoding];
NSString *goodValue = [[[NSString alloc] initWithData:data1 encoding:NSUTF8StringEncoding] autorelease];
And decoding:
NSData *data = [[NSData alloc] initWithData:[response dataUsingEncoding:NSASCIIStringEncoding]];
So far (and not correct), encoding on the Android side:
OutputStream os = socket.getOutputStream();
os.write(request.getBytes("UTF-8"));
os.flush();
And decoding:
while ((bytesRead = is.read(buffer, 0, BUFFER_SIZE)) >= 0) {
if (bytesRead > 0) response.append(new String(buffer, 0, bytesRead, "UTF-8"));
if (bytesRead < BUFFER_SIZE) break;
}
#portforwardpodcast is absolutely correct that you should, if possible, avoid ASCII encoding your utf8 and instead set up your stack to handle/store utf8 directly. That said, if you don't have the ability to change the behavior, the following code may be helpful.
While there's no published explanation of how NSNonLossyASCIIStringEncoding works, based on its output it looks like:
Bytes in the extended ASCII range (decimal values 128 - 255) are escaped using an octal encoding (e.g. ñ with decimal value 241 -> \361)
Non-ASCII code points are escaped in two byte chunks using a hex encoding (e.g. 😥 which takes up 32 bits with decimal value 128549 -> \ud83d\ude25)
So to encode:
public static String encodeToNonLossyAscii(String original) {
Charset asciiCharset = Charset.forName("US-ASCII");
if (asciiCharset.newEncoder().canEncode(original)) {
return original;
}
StringBuffer stringBuffer = new StringBuffer();
for (int i = 0; i < original.length(); i++) {
char c = original.charAt(i);
if (c < 128) {
stringBuffer.append(c);
} else if (c < 256) {
String octal = Integer.toOctalString(c);
stringBuffer.append("\\");
stringBuffer.append(octal);
} else {
String hex = Integer.toHexString(c);
stringBuffer.append("\\u");
stringBuffer.append(hex);
}
}
return stringBuffer.toString();
}
And to decode (this can be made more efficient by parsing the two types of encodings in lock step, rather as two separate passes):
private static final Pattern UNICODE_HEX_PATTERN = Pattern.compile("\\\\u([0-9A-Fa-f]{4})");
private static final Pattern UNICODE_OCT_PATTERN = Pattern.compile("\\\\([0-7]{3})");
public static String decodeFromNonLossyAscii(String original) {
Matcher matcher = UNICODE_HEX_PATTERN.matcher(original);
StringBuffer charBuffer = new StringBuffer(original.length());
while (matcher.find()) {
String match = matcher.group(1);
char unicodeChar = (char) Integer.parseInt(match, 16);
matcher.appendReplacement(charBuffer, Character.toString(unicodeChar));
}
matcher.appendTail(charBuffer);
String parsedUnicode = charBuffer.toString();
matcher = UNICODE_OCT_PATTERN.matcher(parsedUnicode);
charBuffer = new StringBuffer(parsedUnicode.length());
while (matcher.find()) {
String match = matcher.group(1);
char unicodeChar = (char) Integer.parseInt(match, 8);
matcher.appendReplacement(charBuffer, Character.toString(unicodeChar));
}
matcher.appendTail(charBuffer);
return charBuffer.toString();
}
Don't use NSNonLossyASCIIStringEncoding, use utf-8 encoding. I just solved this problem myself on ios+android+java spring backend, and it took me around 4 full days to figure everything out. Android can't display emojis, but this gives me full character support in almost all (or all not sure) languages. Here are the articles that helped me:
Must Read: http://blog.manbolo.com/2012/10/29/supporting-new-emojis-on-ios-6 http://blog.manbolo.com/2011/12/12/supporting-ios-5-new-emoji-encoding
See the hex bytes of a string inside the DB: How can I see raw bytes stored in a MySQL column?
Details about how to setup MySQL: http://technovergence-en.blogspot.com/2012/03/mysql-from-utf8-to-utf8mb4.html
In depth FAQ of utf8- http://www.unicode.org/faq/utf_bom.html#utf8-4
Details about the difference from notation: \ud83d\udc7d and hex value in memory: 0xF09F91BD http://en.wikipedia.org/wiki/UTF-8#Description
Use this to copy and paste characters in to see real hex byte values (works for emojis): http://perishablepress.com/tools/utf8-hex/index.php
Get Spring to support utf8 in urls (for GET params) http://forum.springsource.org/showthread.php?93728-RequestParam-doesn-t-seem-to-be-decoded Get Parameter Encoding http://forum.springsource.org/showthread.php?112181-Unable-to-Override-the-Spring-MVC-URL-decoding-which-uses-default-quot-ISO-8859-1-quot
My answer code is equivalent to IOS NSNonLossyASCIIStringEncoding for Android.
In your gradle put below depandancy.
compile 'org.apache.commons:commons-lang3:3.4'
then Put method to your Utils Class Like this
public static String encode(String s)
{
return StringEscapeUtils.escapeJava(s);
}
public static String decode(String s)
{
return StringEscapeUtils.unescapeJava(s);
}
then Simply call this method where you want to encode string or decode String like this
//for encode
String stencode = Utils.encode("mystring");
//for decode
String stdecode = Utils.decode("mystring")

UTF-8 server side conversion from euc-kr in Korean loses about 15% of characters

During conversion to UTF8 at the server, some of the Korean is not converting correctly(like 15% of the characters).
From server we are getting characters in euc-kr format.
Current scenario:
public static String decodeString(String encoded) {
if (encoded == null) {
encoded = "";
} else if (encoded.equalsIgnoreCase("null")) {
encoded = "";
} else if (encoded.equalsIgnoreCase("true")) {
encoded = "true";
} else if (encoded.equalsIgnoreCase("false")) {
encoded = "false";
} else {
encoded = new String(Base64.decode(encoded, Base64.DEFAULT));
}
return encoded;
}
Server: EUCKR Korean data -> convert to UTF8 -> base64 encode -> send to client
At our Side:
Client: receive base64encoded content -> decode base 64 -> displaying UTF8
However, during the conversion to UTF8 at the server, some of the Korean is not converting correctly(like 15% of the characters).
For example, for the text 에디 쵝오
when doing euckr -> base64 encoding
but when doing euckr -> utf8 -> base64, : (this cuts the string in the middle)
Wanted to see if the app can handle displaying euckr format directly.

Categories

Resources