Cloudprime does all the heavy logical thinking so that we don't have to.
EncodeInteger
not ratedUsed to store integers of different value ranges inside another integer, usefull for generating smaller more secure save codes and for storing multiple values in a single unit attribute. Maybe other uses as well :)
Use EncodeInteger to save multiple integers into a single integer, then use DecodeIntegerValue and DecodeIntegerNext to extract the encoded integers in the reverse order that they were encoded. (Last in first out).
The range of each integer encoded is 0 through maxValue. You must know the ranges of all the integers and the order in which they were encoded to extract them.
Use like so... (psuedo code to save space, not all useable JASS)
// ranges (max values)
integer r1 = 17, r2 = 11, r3 = 1, r4 = 70
// values to encode (must be <= to it's respective range and >= 0)
integer v1 = 14, v2 = 0, v3 = 1, v4 = 39
integer encodedInt = 0
set encodedInt = EncodeInteger(encodedInt, v1, r1)
set encodedInt = EncodeInteger(encodedInt, v2, r2)
set encodedInt = EncodeInteger(encodedInt, v3, r3)
set encodedInt = EncodeInteger(encodedInt, v4, r4)
// Done encoding, now decode
set v4 = DecodeIntegerValue(encodedInt, r4)
set encodedInteger = DecodeIntegerNext(encodedInteger, r4)
set v3 = DecodeIntegerValue(encodedInt, r3)
set encodedInteger = DecodeIntegerNext(encodedInteger, r3)
set v2 = DecodeIntegerValue(encodedInt, r2)
set encodedInteger = DecodeIntegerNext(encodedInteger, r2)
set v1 = DecodeIntegerValue(encodedInt, r1)
set encodedInteger = DecodeIntegerNext(encodedInteger, r1)
In actual use encodedInt might be the custom value of a unit, the values are the integers you want to store in that value, and the ranges would probably be held in a global variable array which you set on initialization and does not change.
To find out the max value of your resulting encodedInteger, use a calculator and multiply all the (ranges + 1) together and subtract one. If this value is less than 2,147,483,647 you are not using the entire space of the integer. For the example above, the maximum value would be 18*12*2*71-1 = 30,671, so more variables could be added to the encoded int.
Use EncodeInteger to save multiple integers into a single integer, then use DecodeIntegerValue and DecodeIntegerNext to extract the encoded integers in the reverse order that they were encoded. (Last in first out).
The range of each integer encoded is 0 through maxValue. You must know the ranges of all the integers and the order in which they were encoded to extract them.
Use like so... (psuedo code to save space, not all useable JASS)
// ranges (max values)
integer r1 = 17, r2 = 11, r3 = 1, r4 = 70
// values to encode (must be <= to it's respective range and >= 0)
integer v1 = 14, v2 = 0, v3 = 1, v4 = 39
integer encodedInt = 0
set encodedInt = EncodeInteger(encodedInt, v1, r1)
set encodedInt = EncodeInteger(encodedInt, v2, r2)
set encodedInt = EncodeInteger(encodedInt, v3, r3)
set encodedInt = EncodeInteger(encodedInt, v4, r4)
// Done encoding, now decode
set v4 = DecodeIntegerValue(encodedInt, r4)
set encodedInteger = DecodeIntegerNext(encodedInteger, r4)
set v3 = DecodeIntegerValue(encodedInt, r3)
set encodedInteger = DecodeIntegerNext(encodedInteger, r3)
set v2 = DecodeIntegerValue(encodedInt, r2)
set encodedInteger = DecodeIntegerNext(encodedInteger, r2)
set v1 = DecodeIntegerValue(encodedInt, r1)
set encodedInteger = DecodeIntegerNext(encodedInteger, r1)
In actual use encodedInt might be the custom value of a unit, the values are the integers you want to store in that value, and the ranges would probably be held in a global variable array which you set on initialization and does not change.
To find out the max value of your resulting encodedInteger, use a calculator and multiply all the (ranges + 1) together and subtract one. If this value is less than 2,147,483,647 you are not using the entire space of the integer. For the example above, the maximum value would be 18*12*2*71-1 = 30,671, so more variables could be added to the encoded int.