Thread: Encoding multiple values into one variable

Page 1 of 2 12 LastLast
Results 1 to 10 of 11
  1. #1 Encoding multiple values into one variable 
    Banned

    Join Date
    Aug 2012
    Posts
    611
    Thanks given
    88
    Thanks received
    118
    Rep Power
    0
    So, I was just fiddling around with a bunch of different ideas, which is something I do often when I'm bored. I came across a post somewhere on Stack Overflow where someone had posted this to a question: [Only registered and activated users can see links. ].

    Now, I was wondering...what if you could encode one integer using multiple bytes and then reverse the integer to the state without the encoding by only knowing the length of the bytes and the currently encoded integer.

    I have tried using masks and a whole bunch of diddly squat that doesn't even get close to what I need. Would anyone know of a way or algorithm this can be achieved or if it's even possible. I've been at this for nearly 4 days now and can't figure an algorithm to do this

    I did not mean to say "Encoded", I already know how many bits can be stored into data types, so please don't post anything related to that. I apologize for misconceptions. What I meant was that I actually wanted to create a formula for adding/subtracting bytes from/to a previous value that can be reversed without a key. This hypothesis has been otherwise proven impossible (in the mean time).
    Reply With Quote  
     

  2. #2  
    Donator

    Netty's Avatar
    Join Date
    Jul 2011
    Posts
    176
    Thanks given
    189
    Thanks received
    62
    Rep Power
    115
    Runescape protocol features many different ways of encoding and decoding different types of data (integers, strings, even bits) to and from a byte array. Have a look at the different methods in [Only registered and activated users can see links. ].

    Think of base 10: to encode 4 different digits into one single number, for example, 1, 2, 3, and 4, take the first number (most significant digit) and multiply it by its place value (1000). Add that with the second number multiplied by its place value (200) and so on. Implement this in base 10 then replace *1000,*100,*10 with <<24,<<16,<<8, (assuming each symbol is 8 bits long). You could also use the or operator instead of add in base 2.



    Reply With Quote  
     

  3. #3  
    Renown Programmer

    Join Date
    Dec 2010
    Posts
    2,883
    Thanks given
    509
    Thanks received
    1,907
    Rep Power
    5000
    what are you trying to achieve
    never talk to me or my wife's son ever again
    Reply With Quote  
     

  4. #4  
    Registered Member
    Join Date
    Jun 2013
    Posts
    68
    Thanks given
    15
    Thanks received
    37
    Rep Power
    33
    Combining the two integer's into a long.

    Code:
    int a = 12345;
    int b = 54321;
    long total = (a << 32) + b; //shifting the 32 bits from value 'a' right 32 bits so we can store 'b' in the original slot 'a' was in
    To get the value of 'b'

    Code:
    int value = (int) total; //no need for shift as Integer has 32 bits and will take ending 32 bits of a 64 bit Long
    To get the value of 'a'

    Code:
    int value = (int) (total >> 32); //shifting the bits right 32 discarding the value 'b' (in slot 0-31) and replacing them with value 'a' (in slot 32-64)
    Code:
    0 (value of a bit) [1 bit]
    00000000 (value of a byte) [8 bits]
    0000000000000000 (value of a short) [16 bits]
    00000000000000000000000000000000 (value of an integer) [32 bits]
    0000000000000000000000000000000000000000000000000000000000000000 (value of a long) [64 bits]
    This isn't limited to Integer's into longs, you can/could store 4 bytes into an integer if you really wanted to.

    Edit: Hope this helps, and whoop whoop x50 posts!
    Reply With Quote  
     

  5. #5  
    Banned

    Join Date
    Aug 2012
    Posts
    611
    Thanks given
    88
    Thanks received
    118
    Rep Power
    0
    Quote Originally Posted by packersfan84 View Post
    what are you trying to achieve
    I had a hypothesis. If I can have multiple values encoded into one integer or byte, then I could compress trillions and quadrillions of bytes into a couple kilos and have the world's fastest and best compression. Of course, this is pretty much impossible at the moment without knowing the values to encode with.

    eg
    Code:
    public static byte[] compress(byte[] input) {
    	byte[] compressed = new byte[1];
    	for (int i = 0; i < input.length; i++)
    		compressed[0] = (byte) (compressed[0] | (1 << input[i])); // encoding every value into one
    
    	byte[] lengthToBytes = ByteBuffer.allocate(4).putInt(input.length).array();
    	compressed = Arrays.copyOf(compressed, lengthToBytes.length + compressed.length);
    	for (int i = 0; i < lengthToBytes.length; i++)
    		compressed[1 + i] = lengthToBytes[i]; // the first byte is the entire stack of bytes
    	return compressed;
    }
    Now how would I decompress those values if I only know the encoded value and the length of the bytes?

    Maybe something like this?
    Code:
    public static byte[] decompress(byte[] input) {
    	int length = ByteBuffer.wrap(Arrays.copyOfRange(input, 1, input.length)).getInt();
    	byte[] decompressed = new byte[length]; // encoded value
    	byte previous = input[0];
    	for (int i = 0; i < length; i++) {
    		for (byte b = Byte.MIN_VALUE; b < Byte.MAX_VALUE; b++) {
    			if ((previous & (1 << b)) != 0) {
    				decompressed[i] = (byte) (previous &= (1 << b));
    				break;
    			}
    		}
    	}
    	return decompressed;
    }
    Reply With Quote  
     

  6. #6  
    Registered Member
    Join Date
    Dec 2010
    Posts
    498
    Thanks given
    41
    Thanks received
    84
    Rep Power
    54
    Quote Originally Posted by Wildskiller View Post
    If I can have multiple values encoded into one integer or byte, then I could compress trillions and quadrillions of bytes into a couple kilos and have the world's fastest and best compression.
    Yes, you can by bit shifting but keep in mind you can only store 4 bytes in an integer and 8 bits in a byte, you can NOT store more than that. There is always limits to how far you can compress things without data loss. Based on the numbers you are randomly throwing out and the expectation of "the world's fastest and best compression" I think you have little to no idea how data encryption works let alone how to implement it. I recommend reading about lossy and lossless compression from here: [Only registered and activated users can see links. ]
    Reply With Quote  
     

  7. #7  
    Renown Programmer

    Join Date
    Dec 2010
    Posts
    2,883
    Thanks given
    509
    Thanks received
    1,907
    Rep Power
    5000
    lol!!!!!!!!!!!!!!!!!!!
    never talk to me or my wife's son ever again
    Reply With Quote  
     

  8. Thankful user:


  9. #8  
    Banned

    Join Date
    Aug 2012
    Posts
    611
    Thanks given
    88
    Thanks received
    118
    Rep Power
    0
    Quote Originally Posted by packersfan84 View Post
    lol!!!!!!!!!!!!!!!!!!!
    shutup you butthole, I've already figured it out ( ° ͜ʖ͡°)╭∩╮. I already know only 32 bits can be stored into an integer, so I can take advantage of this for byte compression and byte pair encoding.


    Quote Originally Posted by H3ll Ruler View Post
    Yes, you can by bit shifting but keep in mind you can only store 4 bytes in an integer and 8 bits in a byte, you can NOT store more than that. There is always limits to how far you can compress things without data loss. Based on the numbers you are randomly throwing out and the expectation of "the world's fastest and best compression" I think you have little to no idea how data encryption works let alone how to implement it. I recommend reading about lossy and lossless compression from here: [Only registered and activated users can see links. ]
    4 bytes, 8 bits per byte, 32 bits per integer. I know this. I don't know why I said "encoded", when I actually wanted to just add or subtract the next byte value from the current byte value and reverse that formulas for byte decompression.
    Reply With Quote  
     

  10. #9  
    Registered Member
    Join Date
    Dec 2010
    Posts
    498
    Thanks given
    41
    Thanks received
    84
    Rep Power
    54
    Quote Originally Posted by Wildskiller View Post
    when I actually wanted to just add or subtract the next byte value from the current byte value and reverse that formulas for byte decompression.
    But then you aren't compressing anything? You need to know the value of a byte to derive the value which is equal to a byte. All you are doing at that point is poor encryption with the key being the bytes following the first one... Well if you figured it out then congrats because apparently don't grasp what you are trying to do here...
    Reply With Quote  
     

  11. #10  
    Banned

    Join Date
    Aug 2012
    Posts
    611
    Thanks given
    88
    Thanks received
    118
    Rep Power
    0
    Quote Originally Posted by H3ll Ruler View Post
    But then you aren't compressing anything? You need to know the value of a byte to derive the value which is equal to a byte. All you are doing at that point is poor encryption with the key being the bytes following the first one... Well if you figured it out then congrats because apparently don't grasp what you are trying to do here...
    The key is what holds the byte values. I know what compression is and was just seeing if I could combine it with encryption. I have already said this is impossible, so we might as well leave this be.
    Reply With Quote  
     

Page 1 of 2 12 LastLast

Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Similar Threads

  1. Inserting high values into highscores..
    By NewRune in forum Help
    Replies: 6
    Last Post: 10-16-2011, 07:09 PM
  2. Replies: 3
    Last Post: 07-02-2011, 10:38 PM
  3. Replies: 17
    Last Post: 06-08-2011, 03:34 AM
  4. RSPS-Clients! Multiple Servers on one site.
    By JavaKing in forum Advertise
    Replies: 0
    Last Post: 03-24-2011, 06:52 PM
  5. How do i make all sprites into one big sprite
    By baseball01 in forum Help
    Replies: 12
    Last Post: 08-05-2009, 03:34 PM
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •