Kanishka Ganguly
Kanishka Ganguly

Reputation: 1298

Reduce a large string to a smaller string using hashing or encoding

Possible Duplicate How to reduce a bigger string in smaller string in C++? Probably by hashing?

I have tried to create my own checksum algorithm for a bmp file in java. So, for a 54 byte (432 bit) header, the resulting checksum is 378 bits long.

How can I reduce it to a smaller dataset? Any suggestions on how to implement my own hashing algorithm? (One of the conditions was to not use existing algorithms).

I have used a very simple hash function.

public static String hash_function(String bmpBytes) {

    String hash = "";
    int left_shift = Integer.parseInt(bmpBytes);
    int right_shift = Integer.parseInt(bmpBytes);
    left_shift = left_shift << 2;
    right_shift = right_shift >> 2;
    int xor = left_shift ^ right_shift;
    hash += Integer.toString(xor);
    return hash;
}

POSSIBLE ANSWER :

I found out a way to shorten my string by generating 'n' random binary bits where n < sizeOf(hash) and then doing a hash % n-bits.

If that is a valid answer, do let me know and I shall mark the question as answered.

Upvotes: 4

Views: 6690

Answers (1)

Logan Murphy
Logan Murphy

Reputation: 6230

Here is something simple. Maybe this can inspire you to do something better.

public static String encode(String header) {
    char[] code = new char[32];
    for(int i = 0; i < header.length(); i++) {
        code[i % code.length] = (char)((int)code[i % code.length] ^ (int)header.charAt(i));
    }
    return new String(code);
}

Upvotes: 4

Related Questions