Convert a String to a byte array and then back to the original String

I would suggest using the members of string, but with an explicit encoding: byte[] bytes = text.getBytes(“UTF-8”); String text = new String(bytes, “UTF-8”); By using an explicit encoding (and one which supports all of Unicode) you avoid the problems of just calling text.getBytes() etc: You’re explicitly using a specific encoding, so you know which encoding … Read more

Parsing URL hash/fragment identifier with JavaScript

Here it is, modified from this query string parser: function getHashParams() { var hashParams = {}; var e, a = /\+/g, // Regex for replacing addition symbol with a space r = /([^&;=]+)=?([^&;]*)/g, d = function (s) { return decodeURIComponent(s.replace(a, ” “)); }, q = window.location.hash.substring(1); while (e = r.exec(q)) hashParams[d(e[1])] = d(e[2]); return hashParams; … Read more

Flattening hash into string in Ruby

I wouldn’t override .flatten, which is already defined: Returns a new array that is a one-dimensional flattening of this hash. That is, for every key or value that is an array, extract its elements into the new array. Unlike Array#flatten, this method does not flatten recursively by default. If the optional level argument determines the … Read more

How to hash a string in Android?

This snippet calculate md5 for any given string public String md5(String s) { try { // Create MD5 Hash MessageDigest digest = java.security.MessageDigest.getInstance(“MD5”); digest.update(s.getBytes()); byte messageDigest[] = digest.digest(); // Create Hex String StringBuffer hexString = new StringBuffer(); for (int i=0; i<messageDigest.length; i++) hexString.append(Integer.toHexString(0xFF & messageDigest[i])); return hexString.toString(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } return … Read more

What is image hashing used for?

Hashing is a function that applies to an arbitrary data and produces the data of a fixed size (mostly a very small size). There are many different types of hashes, but if we are talking about image hashing, it is used either to: find duplicates very fast. Almost any hash function will work. Instead of … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)