android.utils.Base64.encode gets different value - android

When I try this:
map.put("password1", Base64.encode("111111".getBytes(),Base64.DEFAULT));
map.put("password2", Base64.encode("111111".getBytes(),Base64.DEFAULT));
map.put("password3", Base64.encode("111111".getBytes(),Base64.DEFAULT));
I got different value of password1,password2 and password3:
password1:[B#5368aecc
password2:[B#536e9ea0
password3:[B#536c0dec
Is it should be the same value?
Thanks in advance.

You're essentially printing toString() of a byte[] array. The output is different for different byte array objects.
Either don't store the values as byte arrays by removing the .getBytes(), or convert your byte arrays back to string with new String(byteArray, someEncoding).

Yes, its can be same , when u add a some text to your string, the old part of string is in hashed string same. Check more at Wikipedia

Related

Android save Integer HashSet

I want to save Integer ArrayList to sharedpreferrence with HashSet, but I can only do this with String ArrayList. I have tried converting each of the integers to strings but then I have to change a lot of code. Maybe there is an easier way?
https://i.stack.imgur.com/ba4XB.png
There is no support for lists of items in SharedPreferences (except for String). The easiest way to do this is to write yourself a convenience method that converts an array of integers into a String and vice versa. Then store the String in SharedPreferences.

How to process an array returned by a wsdl?

I am using ksoap2 in order to extract an array of strings from a wsdl based webservice(for an android app). How do I process the returned array? I need those 3-4 lines of code which will let me save and use that returned array in my class. Thanks.
String r = NameArray.columncount("userid", limitstart, loadNumber,loggername);
String temp = r.replaceAll(";\\s", ",").replaceAll("string=", " ")
.replace("anyType{", "").replace(",}", "");
String[] fulname = temp.split(",\\s+");
'NameArray.columncount' is my function which gets the array from the wsdl(don't get confused in that)
step 1-
Here I am getting the array values returned from the wsdl in to a string called 'r'.In this case I am getting an array of numbers
Returned array string r looks like this
r ="anyType{string=10054; string=10055; string=10056; string=10035; string=10052; string=10036; string=10037; string=10038; }"
step 2-
Then creating a String variable called temp where I am removing all the unwanted characters using the replaceAll function.
after removing unwanted characters temp looks like this
temp="10054, 10055, 10056, 10035, 10052, 10036, 10037, 10038"
step3-
Finally created a string array called 'fulname' and split the modified string with ',\s'
Array fulname after split looks like this
fulname = [ 10054, 10055, 10056, 10035, 10052, 10036, 10037, 10038]
This will work fine because all the wsdl array return the same type of string with same unwanted characters
Hope you understood
Good Luck
If you are still on this problem, you can check out this article which explain the whole procedure to parse arrays returned in KSOAP:
http://seesharpgears.blogspot.fr/2010/10/web-service-that-returns-array-of.html
Hope this answer to your question ;)

Minusing string number values in android

I am making an android application where i got a set of strings that i load from SharedPreferences so that i can save the strings. The strings contain only numbers, but it is not an int value, its a string value. And i wounder how i can minus the numbers that's in there, becuase usually, i would have been using something like an int value = value - value; But that doesn't seem to work since it's a string and not an int value. How can i do this even though it's a string? I know i could use int values instead, but as i didn't think of this before now, when i'm almost done, it would be alot of work changing all of the code that's related to this. Please help me and thanks so much in advance!
You will have to convert your strings to ints first, then operate on them, then save the string back:
String value = preferences.getString("key:");
int intValue = Integer.valueOf(value);
intValue = intValue - 1;
preferences.edit().putString("key", Integer.toString(intValue)).commit();
Try using Integer.valueOf(string) or Integer.parseInt(string).
Learning basic programming the the concept of casting will help you tremendously. One datatype can be converted to another using the base classes, which often times deal with String. For instance look at the Documentation of Integer.
Well as you have said that it is a lot of work to change the code and save it as int, I would suggest converting the string into an integer, refer to this link for more information, someone has asked about converting strings to integers, and as Android is Java-based, this can apply to your project:
Converting a string to an integer on Android
Hope this helps.

How can i store an Integer Array in SharedPreferences in Android?

I have an Array called Upvalthat has 16 Integer values that I would like to store in my SharedPreferences without creating individual ones for each, but SharedPrefernces won't allow Array's, what is the simplest way of doing this? The declaration looks something like this:
Integer[] UpVal = new Integer[16];
You can store it as a String by transforming it:
Arrays.toString(upVal)
To get it back and convert a String to an Integer array is trivial.
You can serialize an array to String using TextUtils.join(";", myInts) and the deserialize it back using something like TextUtils. SimpleStringSplitter or implement your own TextUtils.StringSplitter.

How to send an image as a Hex String Array in Android?

Is there a mechanism in Android, which allows us to convert a large image into a Hex String Array before we send it to a webservice over the network?
I know how to convert it to a byte array and transfer it but I'm not so confident on how to implement the same using a Hex String.
Thanks in advance.
No direct method to do this. Convert to byte[] first. Then, convert to hex string like in answers to question "In Java, how do I convert a byte array to a string of hex digits while keeping leading zeros?"

Categories

Resources