How to decode some number into timeDate? - reverse-engineering

This question is sequel of this one.
So any idea how to decode this number 5252235562500 into date and time 19.11.2010 15:43 ?
I have more pairs like this and I'm thinking about some script for comparing them to find some patern. Any advice what to check and how to search for patterns ?
EDIT: I added four pairs that I curentlly have.
11.11.2010 16:23 > 5252425372575
16.11.2010 15:30 > 5252922462564
19.11.2010 15:39 > 5252231562511
19.11.2010 15:43 > 5252235562500

I think I found the solution. Instead of simply presenting the decoding algorithm I'd like to show you the reasoning.
The answer to the linked question showed that was a barcode in EAN-13 format.
It means the codes have 12 digits and 1 check digit:
11.11.2010 16:23 > 525242537257 5
16.11.2010 15:30 > 525292246256 4
19.11.2010 15:39 > 525223156251 1
19.11.2010 15:43 > 525223556250 0
The check digit can be calculated by
adding the values of the digits in the even-numbered positions: 2, 4, 6 ... (2+2+2+3+2+7=18)
multiplying this result by 3 (18*3=54)
adding the values of the digits in the odd-numbered positions: 1, 3, 5... (5+5+4+5+7+5=31)
summing the two results (54+31=85)
calculating modulo 10 and subtracting it from 10 (5-10=5)
I calculated the check digit for every code, it matched and confirmed the codes were in EAN-13 format.
According to the specification, the first two or three digits of the code could be country codes, so I tried to separate these:
11.11.2010 16:23 > 52 5242537257 5 | 525 242537257 5
16.11.2010 15:30 > 52 5292246256 4 | 525 292246256 4
19.11.2010 15:39 > 52 5223156251 1 | 525 223156251 1
19.11.2010 15:43 > 52 5223556250 0 | 525 223556250 0
The resulting numbers didn't make any sense, because the earlier time had a greater number:
5292246256 or 292246256
than the later time:
5223156251 or 223156251
At this point I suspected the time wasn't stored in binary format.
I reorganized the digits and tried to find repeating patterns.
I ended up with this layout:
11.11.2010 16:23 > 52 52 42 53 72 57 5
16.11.2010 15:30 > 52 52 92 24 62 56 4
19.11.2010 15:39 > 52 52 23 15 62 51 1
19.11.2010 15:43 > 52 52 23 55 62 50 0
This is where things got interesting...
Take a look at the 3rd and 4th row, these are the same except the 4th and 6th column.
The 4th column has 15 and 55. Translate it backwards and you get 51 and 55.
The difference of the two is 55 - 51 = 4 just like the difference of minutes 43 - 39 = 4
Subtract the minutes from code values:
55 - 43 = 12
51 - 39 = 12
It seems the 4th column encodes minutes by adding 12 and storing the digits backwards.
Now try to apply this to the 5th column:
11.11.2010 16:23 > 72 > 27
16.11.2010 15:30 > 62 > 26
19.11.2010 15:39 > 62 > 26
19.11.2010 15:43 > 62 > 26
26 - 15 = 11 and 27 - 16 = 11 so the difference for the 5th column is 11.
From then it's easy, the differences for the columns are 15, 14, 13, 12 & 11.
A few quick calculations and you get the encoding scheme:
Digits Meaning Diff.
2-1 year 15
4-3 month 14
6-5 day 13
8-7 minute 12
10-9 hour 11
Here's a simple code snippet for decoding:
union TimeFormat
{
unsigned short codearray[5];
struct
{
unsigned short year;
unsigned short month;
unsigned short day;
unsigned short minute;
unsigned short hour;
};
};
void DecodeBarcode(char *code, TimeFormat *time)
{
char buf[3]; // for atoi()
buf[2] = 0; // of course it has to be null-terminated
for (int i = 0, diff = 15; i < 5; ++i, --diff)
{
buf[0] = code[i * 2 + 1];
buf[1] = code[i * 2];
time->codearray[i] = atoi(buf) - diff;
}
time->year += 2000;
}

When you're trying to decode a foreign time format, you need two known times. Take the difference between them and see what it equates to - second, milliseconds, days, there aren't too many possibilities. Now that you have the basic time unit, you can work with one of the times and see what the origin time is.
I just had to do this last week. I had two dates:
2009-07-15 15:29:12 1247689752
2009-07-17 08:27:55 1247837275
There are lots of ways to get the difference between two dates. The easiest is probably Excel, which will display the difference in days; in this case 1.70744213. The difference between the two representations is 147523. Multiplying the number of days by the number of seconds in a day (24*60*60) also resulted in 147523, so now I know that the date is the number of seconds elapsed from some starting date.
To get the starting date, I subtract a date from itself. Again this is trivial in Excel: divide the date number by the number of seconds in a day, then subtract. In my case it comes up 1969-12-31 19:00. This seems a little odd, but I realize that my time zone is 5 hours off of UTC in the summer. This tells me that the time value is in UTC, and represents the number of seconds since 1970-01-01.

Script like this what you are thinking not make any sense, because you can by hand for example brake this in time X, then probably you will be write program for this in time 25*X and this program will be working based on checking patterns what you are define then this not make sense when you don't have thousands of algorithms this type to brake but vary in some little part.
Give some more pairs then i can tell you more, one pair is to less to know anything.

Related

Quick search the most similar objects in the n-dimensional space

Lets assume that we have a points in the n-dimensional space. So we have a n coords(n columns) which can describe location of the each point.
We need to implement a table which can be used for a quick searching the most similar points, i.e. points which have the smallest distance to the desired point.
E.g. points in the db:
id c1 c2 c3 c4 c5
1 5 19 42 12 16
2 3 23 38 15 12
3 14 21 32 33 1
4 12 29 21 24 5
If we want to find the best matching for point with coords:
c1 c2 c3 c4 c5
4 20 40 14 15
We will get points with id 1 and 2.
We also have mean coordinate for each dimension(column) and vector for each point in which first element - number of the dimension in which point has the largest difference from the mean coordinate in this dimension, and last - number of the dimension in which point has the smallest difference. Maybe it can be used for the more rapid filtering points which have the biggest distance to the desired point.
So how can I do something like this using MySQL?
I think the composite index and order by abs(cx - $mycx) can be a good solution, but I can't use it because I will have more then 16 columns which I need to include in the one index.
Any help will be very useful!

Base Conversions between 10 and 2

Suppose you have a 20 digit base 10 positive integer, and you want to represent it in base 2. How many bits are necessary? Why?
I tried : log(2)10 * 10 +1 =66.44 + 1 = 67.44
Then I rounded down to 67 as a final answer
The biggest 20-digit number in base 10 is 10^20-1 (or 20 nines). This number is between 2^66 and 2^67. Since 2^66 is 1 followed by 66 zeros in base 2, it is a 67-digit number. 2^67 is the smallest 68-digit number in base 2. Since 10^20-1 is smaller than that, it is only 67 digits long in base 2. Therefore, a 20-digit number in base 10 is at most 67 digits long in base 2.

Finding Reccurring Number Combinations in Column of Numbers

I have searched and found discussions and solutions to similar problems, but not quite or as complex as I'm trying to figure out.
I have an access table which consists of two columns Draw Number and Number Drawn as shown below. Draw Number is repeated 20 times, to correspond to the 20 numbers that are drawn in each particular draw.
I'm trying to figure a way to determine the most frequent occurring combination of numbers (5 numbers) for all of the draws in each of the 20 number sets. So for instance, 12341 occurs n x, 12342 occurs nx, 12343 occurs n x, etc.
I've created parameter queries which allow me to search for different number combinations from 2 to 10 numbers, and they work OK returning the number of occurrences of a combination of numbers that I input through a simple UI. But the goal is to figure out pragmatically what the optimum combination of numbers.
Hope this makes sense. And by the way, there are 36 million or so rows in the table. The para queries work quite well however; it takes just over a second to return results for each number added. So, query two numbers = 2 second wait, three numbers = 3 second wait, etc.
I've been thinking about a loop of some type but don't know how to get started? Processing time isn't an issue; can take a day if required!
This is written in VBA and has an assortment of queries, temp tables, etc to get the job done.
The text says Access, but the tags say MySql, which is it? – RBarryYoung 21 hours ago
This part confuses me: I'm trying to figure a way to determine the most frequent occurring combination of numbers (5 numbers) for all of the draws in each of the 20 number sets. So for instance, 12341 occurs n x, 12342 occurs nx, 12343 occurs n x, etc. – Newd 21 hours ago
^What do you mean five numbers? No where in your sample data do I see 12341. Please explain using the data you have, and give expected results using that data. – McAdam331 21 hours ago
drosberg - clarification:
thanks for the response. It is an Access application, but as a first-time poster Stackoverflow recommends tags?
By five numbers I mean the most frequently occurring group of five numbers (I used five as an example, could be groups of 2 to 10 numbers) which occur in each draw, where a draw consists of 20 drawn numbers from a total of 80 numbers. So the data that I posted was intended as an example. The sample provided only has 50, 51 in common. I can plug 50 and 51 into the parameter query and it will tell me that this combination occurs 60,000 times (or whatever), but perhaps 50 and 57 occurs 65,000 times.
If i was to do this manually, and assuming I'm looking for the most frequent 5 number combination I would enter the following in the parameter query: 1,2,3,4,1 group = 30,000 occurrences 1,2,3,4,2 group = 31,000 occurrences 1,2,3,4,3 group = 31,050 occurrences 1,2,3,4,4 group = 29,050 occurrences etc........... etc...........
but I would have to do this for every combination of 5 numbers that can be derived from the numbers 1 thru 80. I'm hoping to have program do the work!!
thanks
don
DRAW NUMBER NUMBER DRAWN
1 1
1 28
1 19
1 3
1 38
1 46
1 43
1 29
1 13
1 22
1 20
1 11
1 50
1 51
1 53
1 54
1 57
1 64
1 76
1 78
2 29
2 14
2 2
2 1
2 35
2 40
2 39
2 30
2 10
2 27
2 21
2 6
2 42
2 50
2 51
2 53
2 54
2 61
2 65
2 69
I wrote a post a while ago about generating permutations with and without repetition using Excel. Perhaps you can use it.
https://michiel.wordpress.com/2015/03/29/permutations-with-repetition-using-excel/
Here's how it works. I am using strings, but you can easily modify that for numbers (since you say you need 5).
You can use the MID function to grab a single char from a string, and generate permutations from it.
=MID(Pattern,MOD([N]/[P],Length)+1,1)
N revers to the column N
P refers to the horizontal row (1,4,16). You can generate these with a formula like =4^.
After putting in the code, you can make a list of all permutations in Excel and in the cell next to it generate a sql query that you can perform as well from VBA.
Example: Looking up Access database in Excel
Or find a commercial tool like http://thingiequery.com/
I don't know if there's any open source tools for it.
I'm thinking that you should consider:
Say there are 100 balls.
Setting up a table to have one row for each "Draw number" with 100 columns one for every possible number each column has type boolean.
When you look to see which draws had number 23 you just add a
WHERE Column23 = true.
For numbers 23 and 56
WHERE Column23 = true AND Column56 = true
This should massivel simplify and speed up your SQL.
You set up a table with every possible combination of numbers.
You run SQL to find the counts.
Harvey

The binary equivalent of the decimal number 104

Ok,so I know that the binary equivalent of 104 is 1101000.
10=1010
4=0100
so , 104=1101000 (how to get this??how these both mix together and get this binary?)
And from the example here...
the octets from "hellohello" are E8 32 9B FD 46 97 D9 EC 37.
This bit is inserted to the left which yields 1 + 1101000 = 11101000 ("E8").
I still understand this part , but how to convert 11101000 to E8?
I'm so sorry for all these noob questions , I just learn it yesterday , I googled and search for a whole day but still not really understand the concept...
Thank you.
Ok,so I know that the binary equivalent of 104 is 1101000.
10=1010
4=0100
You can't break apart a number like 104 into 10 and 4 when changing bases. You need to look at the number 104 in its entirety. Start with a table of bit positions and their decimal equivalents:
1 1
2 10
4 100
8 1000
16 10000
32 100000
64 1000000
128 10000000
Look up the largest decimal number that is still smaller than your input number: 104 -- it is 64. Write that down:
1000000
Subtract 64 from 104: 104-64=40. Repeat the table lookup with 40 (32 in this case), and write down the corresponding bit pattern below the first one -- aligning the lowest-bit on the furthest right:
1000000
100000
Repeat with 40-32=8:
1000000
100000
1000
Since there's nothing left over after the 8, you're finished here. Sum those three numbers:
1101000
That's the binary representation of 104.
To convert 1101000 into hexadecimal we can use a little trick, very similar to your attempt to use 10 and 4, to build the hex version from the binary version without much work -- look at groups of four bits at a time. This trick works because four bits of base 2 representation completely represent the range of options of base 16 representations:
Bin Dec Hex
0000 0 0
0001 1 1
0010 2 2
0011 3 3
0100 4 4
0101 5 5
0110 6 6
0111 7 7
1000 8 8
1001 9 9
1010 10 A
1011 11 B
1100 12 C
1101 13 D
1110 14 E
1111 15 F
The first group of four bits, (insert enough leading 0 to pad it to four
bits) 0110 is 6 decimal, 6 hex; the second group of four bits, 1000 is
8 decimal, 8 hexadecimal, so 0x68 is the hex representation of 104.
I think you are making some confusions:
104 decimal is 1101000 which is not formed by two groups splitting 104 into 10 and 4.
The exception is for hex numbers that can be formed by two groups 4 binary numbers (2^4 = 16).
So 111010000 = E8 translates into 1110 = E and 8 = 10000. 1110 (binary) would be 14 (decimal) and equivalent of E (hex).
Hex numbers go from 0 to 15 (decimal) where:
10 (decimal) = A (hex)
11(decimal) = B(hex)
...
15(decimal) = F(hex)
What you're missing here is the general formula for digital numbers.
104 = 1*10^2 + 0*10^1 + 4*10^0
Similarly,
0100b = 0*2^3 + 1*2^2 + 0*2^1 + 0*0^0
And for a hexidecimal number, the letters A-F stand for the numbers 10-15. So,
E8 = 14*16^1 + 8*16^0
As you go from right to left, each digit represents the coefficient of the next higher power of the base (also called the radix).
In programming, if you have an integer value (in the internal format of the computer, probably binary, but it isn't relevant), you can extract the right most digit with the modulus operation.
x = 104
x % 10 #yields 4, the "ones" place
And then you can get "all but" the rightmost digit with integer division (integer division discards the remainder which we no longer need).
x = x / 10 #yields 10
x % 10 #now yields 0, the "tens" place
x = x / 10 #yields 1
x % 10 #now yields 1, the "hundreds" place
So if you do modulus and integer division in a loop (stopping when x == 0), you can output a number in any base.
This is basic arithmetic. See binary numeral system & radix wikipedia entries.

How Does Modulus Divison Work

I don't really understand how modulus division works.
I was calculating 27 % 16 and wound up with 11 and I don't understand why.
I can't seem to find an explanation in layman's terms online.
Can someone elaborate on a very high level as to what's going on here?
Most explanations miss one important step, let's fill the gap using another example.
Given the following:
Dividend: 16
Divisor: 6
The modulus function looks like this:
16 % 6 = 4
Let's determine why this is.
First, perform integer division, which is similar to normal division, except any fractional number (a.k.a. remainder) is discarded:
16 / 6 = 2
Then, multiply the result of the above division (2) with our divisor (6):
2 * 6 = 12
Finally, subtract the result of the above multiplication (12) from our dividend (16):
16 - 12 = 4
The result of this subtraction, 4, the remainder, is the same result of our modulus above!
The result of a modulo division is the remainder of an integer division of the given numbers.
That means:
27 / 16 = 1, remainder 11
=> 27 mod 16 = 11
Other examples:
30 / 3 = 10, remainder 0
=> 30 mod 3 = 0
35 / 3 = 11, remainder 2
=> 35 mod 3 = 2
The simple formula for calculating modulus is :-
[Dividend-{(Dividend/Divisor)*Divisor}]
So, 27 % 16 :-
27- {(27/16)*16}
27-{1*16}
Answer= 11
Note:
All calculations are with integers. In case of a decimal quotient, the part after the decimal is to be ignored/truncated.
eg: 27/16= 1.6875 is to be taken as just 1 in the above mentioned formula. 0.6875 is ignored.
Compilers of computer languages treat an integer with decimal part the same way (by truncating after the decimal) as well
Maybe the example with an clock could help you understand the modulo.
A familiar use of modular arithmetic is its use in the 12-hour clock, in which the day is divided into two 12 hour periods.
Lets say we have currently this time: 15:00
But you could also say it is 3 pm
This is exactly what modulo does:
15 / 12 = 1, remainder 3
You find this example better explained on wikipedia: Wikipedia Modulo Article
The modulus operator takes a division statement and returns whatever is left over from that calculation, the "remaining" data, so to speak, such as 13 / 5 = 2. Which means, there is 3 left over, or remaining from that calculation. Why? because 2 * 5 = 10. Thus, 13 - 10 = 3.
The modulus operator does all that calculation for you, 13 % 5 = 3.
modulus division is simply this : divide two numbers and return the remainder only
27 / 16 = 1 with 11 left over, therefore 27 % 16 = 11
ditto 43 / 16 = 2 with 11 left over so 43 % 16 = 11 too
Very simple: a % b is defined as the remainder of the division of a by b.
See the wikipedia article for more examples.
I would like to add one more thing:
it's easy to calculate modulo when dividend is greater/larger than divisor
dividend = 5
divisor = 3
5 % 3 = 2
3)5(1
3
-----
2
but what if divisor is smaller than dividend
dividend = 3
divisor = 5
3 % 5 = 3 ?? how
This is because, since 5 cannot divide 3 directly, modulo will be what dividend is
I hope these simple steps will help:
20 % 3 = 2
20 / 3 = 6; do not include the .6667 – just ignore it
3 * 6 = 18
20 - 18 = 2, which is the remainder of the modulo
Easier when your number after the decimal (0.xxx) is short. Then all you need to do is multiply that number with the number after the division.
Ex: 32 % 12 = 8
You do 32/12=2.666666667
Then you throw the 2 away, and focus on the 0.666666667
0.666666667*12=8 <-- That's your answer.
(again, only easy when the number after the decimal is short)
27 % 16 = 11
You can interpret it this way:
16 goes 1 time into 27 before passing it.
16 * 2 = 32.
So you could say that 16 goes one time in 27 with a remainder of 11.
In fact,
16 + 11 = 27
An other exemple:
20 % 3 = 2
Well 3 goes 6 times into 20 before passing it.
3 * 6 = 18
To add-up to 20 we need 2 so the remainder of the modulus expression is 2.
The only important thing to understand is that modulus (denoted here by % like in C) is defined through the Euclidean division.
For any two (d, q) integers the following is always true:
d = ( d / q ) * q + ( d % q )
As you can see the value of d%q depends on the value of d/q. Generally for positive integers d/q is truncated toward zero, for instance 5/2 gives 2, hence:
5 = (5/2)*2 + (5%2) => 5 = 2*2 + (5%2) => 5%2 = 1
However for negative integers the situation is less clear and depends on the language and/or the standard. For instance -5/2 can return -2 (truncated toward zero as before) but can also returns -3 (with another language).
In the first case:
-5 = (-5/2)*2 + (-5%2) => -5 = -2*2 + (-5%2) => -5%2 = -1
but in the second one:
-5 = (-5/2)*2 + (-5%2) => -5 = -3*2 + (-5%2) => -5%2 = +1
As said before, just remember the invariant, which is the Euclidean division.
Further details:
What is the behavior of integer division?
Division and Modulus for Computer Scientists
Modulus division gives you the remainder of a division, rather than the quotient.
It's simple, Modulus operator(%) returns remainder after integer division. Let's take the example of your question. How 27 % 16 = 11? When you simply divide 27 by 16 i.e (27/16) then you get remainder as 11, and that is why your answer is 11.
Lets say you have 17 mod 6.
what total of 6 will get you the closest to 17, it will be 12 because if you go over 12 you will have 18 which is more that the question of 17 mod 6. You will then take 12 and minus from 17 which will give you your answer, in this case 5.
17 mod 6=5
Modulus division is pretty simple. It uses the remainder instead of the quotient.
1.0833... <-- Quotient
__
12|13
12
1 <-- Remainder
1.00 <-- Remainder can be used to find decimal values
.96
.040
.036
.0040 <-- remainder of 4 starts repeating here, so the quotient is 1.083333...
13/12 = 1R1, ergo 13%12 = 1.
It helps to think of modulus as a "cycle".
In other words, for the expression n % 12, the result will always be < 12.
That means the sequence for the set 0..100 for n % 12 is:
{0,1,2,3,4,5,6,7,8,9,10,11,0,1,2,3,4,5,6,7,8,9,10,11,0,[...],4}
In that light, the modulus, as well as its uses, becomes much clearer.
Write out a table starting with 0.
{0,1,2,3,4}
Continue the table in rows.
{0,1,2,3,4}
{5,6,7,8,9}
{10,11,12,13,14}
Everything in column one is a multiple of 5. Everything in column 2 is a
multiple of 5 with 1 as a remainder. Now the abstract part: You can write
that (1) as 1/5 or as a decimal expansion. The modulus operator returns only
the column, or in another way of thinking, it returns the remainder on long
division. You are dealing in modulo(5). Different modulus, different table.
Think of a Hash Table.
When we divide two integers we will have an equation that looks like the following:
A/B​​ =Q remainder R
A is the dividend; B is the divisor; Q is the quotient and R is the remainder
Sometimes, we are only interested in what the remainder is when we divide A by B.
For these cases there is an operator called the modulo operator (abbreviated as mod).
Examples
16/5= 3 Remainder 1 i.e 16 Mod 5 is 1.
0/5= 0 Remainder 0 i.e 0 Mod 5 is 0.
-14/5= 3 Remainder 1 i.e. -14 Mod 5 is 1.
See Khan Academy Article for more information.
In Computer science, Hash table uses Mod operator to store the element where A will be the values after hashing, B will be the table size and R is the number of slots or key where element is inserted.
See How does a hash table works for more information
This was the best approach for me for understanding modulus operator. I will just explain to you through examples.
16 % 3
When you division these two number, remainder is the result. This is the way how i do it.
16 % 3 = 3 + 3 = 6; 6 + 3 = 9; 9 + 3 = 12; 12 + 3 = 15
So what is left to 16 is 1
16 % 3 = 1
Here is one more example: 16 % 7 = 7 + 7 = 14 what is left to 16? Is 2 16 % 7 = 2
One more: 24 % 6 = 6 + 6 = 12; 12 + 6 = 18; 18 + 6 = 24. So remainder is zero, 24 % 6 = 0