Conversion between binary vector and 128 bit number - python

Is there a way to convert back and forth between a binary vector and a 128-bit number? I have the following binary vector:
import numpy as np
bits = np.array([1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1,
0, 0, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0,
1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0,
1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1,
0, 0, 1, 1, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 1,
1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 1], dtype=np.uint8)
which is a MD5 hash that I am trying use as a feature for a scikit-learn machine learning classifier (I need to represent the hash as a single feature).

As commented above, numpy only goes up to 64bits, but python has variable length ints, so we can do 128bits int no problem.
The following will go from binary in np.array to python int back to binary in np.array.
import numpy as np
bits = np.array(
[
1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1,
0, 0, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0,
1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0,
1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1,
0, 0, 1, 1, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 1,
1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 1
],
dtype=np.uint8
)
s = "".join(bits.astype("str")) # move from array to string
n = int(s, 2) # convert to int from string of base 2
print(n)
s = bin(n)[2:] # get binary of int, cut "0b" prefix
np.array(list(s), dtype=np.uint8) # put back in np.array

Related

Conflicting versions of sha256 bit calculation

I'm computing sha256 from two different sources, both ran on bit arrays. In Python, I run
from bitarray import bitarray
from hashlib import sha256
inbits = bitarray([1,0,1,0,1,0,1,0,1,0])
sha = sha256(inbits)
outbits = bitarray()
outbits.frombytes(sha.digest())
The other source is a circuit implementation of sha256 (implemented in circom). I'm just wondering if there are different implementations of sha256, as running the sha256 circuit and python code give different outputs.
Output from circom:
0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0,
0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0,
0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0,
1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0,
1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0,
0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1,
0, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0,
1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 1,
1, 1, 0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 1,
1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1,
1, 1, 1, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1,
0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1,
0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0,
1, 1, 0, 0, 1, 0, 1, 1, 0]
and output from python:
bitarray('1110111001111011010110111001100001000011011101011100000100111011001101011111000000010101110000001001100011100001100011010111011110001100100010110010111111110011111101010111111110101000101111011010010001011101000001101110101110111011011010111100101101111100')

Element wise sum of 5 out of 60 list

Consider I have a list with 60 list of 0 and 1s (there are 12~16 1s in series):
my_list=
[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0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1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
The sum list that I aim to get is:
sum_list = [2,2,2,3,3,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,4,2,2,2,2]
I need to find the combinations of 01 lists that can give me the sum_list
for example:
[0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0]
+
[0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0]
+
.....
=
[2,2,2,3,3,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,4,2,2,2,2]
Since the max element is 5, I'm thinking to start with 5,
To select any 5 list out of 60 and add them elementwise, if there is no solution, try 6 out of 60.
I'm not sure how to do this list-element wise.
=============================================================
Based on solution Adrian provided I made a slight change, from combination to product, to include duplicates. Its still running, i'll see how it goes
Time_list = ["6:30","7:00","7:30","8:00","8:30","9:00","9:30","10:00","10:30","11:00","11:30","12:00","12:30","13:00","13:30","14:00","14:30","15:00","15:30","16:00","16:30","17:00","17:30","18:00","18:30"]
goal = [2,2,2,3,3,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,4,2,2,2,2]
my_list = []
a_list = [10,11,12,13,14,15,16]
slots = len(goal)
for j in a_list:
for i in range(slots+1):
if i<=(slots-(j)):
a_list = [0]*(slots-(j)-i)+[1]*(j)+ [0]*(i)
my_list.append(a_list)
print(len(my_list))
import itertools
from operator import add
test_range = len(my_list)
print(test_range)
for num_lists in [6,7]:
print("check combinations of "+str(num_lists)+" Lists.")
for index in itertools.product(range(0,test_range), repeat = num_lists):
#print(len(res))
#print(index)
res = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
for x in index:
res = list(map(add, res, my_list[x]))
#print(res)
if res == goal:
print("Goal found!")
for x in index:
print(my_list[x])
break
print("check combinations of "+str(num_lists)+" completed.")
you could try this brute-force approach. Needs some computing time to find a solution (maybe not solvable).
goal = [2,2,2,3,3,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,4,2,2,2,2]
from operator import add
for num_lists in [5,6,7]:
print("check combinations of "+str(num_lists)+" Lists.")
for index in itertools.combinations(range(0,60), num_lists):
res = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
for x in index:
res = list(map(add, res, my_list[x]))
if res == goal:
print("Goal found!")
for x in index:
print(my_list[x])
break
print("check combinations of "+str(num_lists)+" completed.")
Edit: I tried with this approch all combinations of 5, 6 and 7 lists. I found no solution for your goal. Maybe there is no solution.

How to count 0's & 1's in a matrix using python?

I have a matrix as shown below ,
matrix([[0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 1,
1, 0, 1, 1, 0, 1, 0, 0, 1],
[0, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0,
1, 1, 0, 0, 0, 1, 0, 0, 1],
[0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0,
1, 1, 0, 0, 1, 0, 0, 0, 0],
[1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1,
0, 1, 0, 0, 0, 1, 1, 0, 0],
[1, 1, 1, 0, 1, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 0,
1, 0, 0, 0, 0, 1, 1, 0, 1],
[0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 0, 0,
0, 1, 0, 1, 0, 1, 0, 0, 0],
[0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 1, 1,
0, 1, 1, 1, 1, 0, 0, 1, 0],
[0, 0, 1, 1, 1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1,
1, 0, 1, 0, 1, 0, 0, 0, 0],
[1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0,
1, 0, 0, 1, 0, 0, 1, 1, 1],
[1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 1, 0, 1, 1, 0, 1, 0]])
I want to count the 0's and 1's in that matrix.
The code i tried is ,
def countZeroes(mat):
# start from bottom-left
# corner of the matrix
N = 10;
row = N-1;
col = 0;
# stores number of
# zeroes in the matrix
count = 0;
while (col < N):
# move up until
# you find a 0
while (mat[row][col]):
# if zero is not found
# in current column, we
# are done
if (row < 0):
return count;
row = row - 1;
# add 0s present in
# current column to result
count = count + (row + 1);
# move right to
# next column
col = col + 1;
return count;
The above code is to count 0's.
Could you help me in solving this problem?
I would request you to provide me an answer using loops.
Thanks!
Looks like we don't care at all about the positions of the various 0s and 1s - so, we're not counting on a per-row or per-column basis, then.
matrix = [[0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 1,
1, 0, 1, 1, 0, 1, 0, 0, 1],
[0, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0,
1, 1, 0, 0, 0, 1, 0, 0, 1],
[0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0,
1, 1, 0, 0, 1, 0, 0, 0, 0],
[1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1,
0, 1, 0, 0, 0, 1, 1, 0, 0],
[1, 1, 1, 0, 1, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 0,
1, 0, 0, 0, 0, 1, 1, 0, 1],
[0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 0, 0,
0, 1, 0, 1, 0, 1, 0, 0, 0],
[0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 1, 1,
0, 1, 1, 1, 1, 0, 0, 1, 0],
[0, 0, 1, 1, 1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1,
1, 0, 1, 0, 1, 0, 0, 0, 0],
[1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0,
1, 0, 0, 1, 0, 0, 1, 1, 1],
[1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 1, 0, 1, 1, 0, 1, 0]]
The pythonic solution would be to use a comprehension inside a call to the built-in sum() to just count the number of 1s, then subtract that from the size of the matrix:
matrix_height = len(matrix)
matrix_width = len(matrix[0])
num_ones = sum(cell for row in matrix for cell in row)
num_zeroes = (matrix_height * matrix_width) - num_ones
# num_zeroes = 155
However, we can also just use two nested for loops to count the number of zeroes that way:
num_zeroes = 0
for row in matrix:
for cell in row:
if cell == 0:
num_zeroes += 1
# num_zeroes = 155
You can see that the comprehension I demonstrated before is essentially just these two for loops, but in a single line.
You can use sum() and map() to obtain the number of ones. Then subtract that number from the total number of elements to obtain the number of zeroes:
ones = sum(map(sum,matrix))
zeros = sum(map(len,matrix))-ones

removing the first 0 bit value

i want to get the binary value from a string, but why if i print the binary value and save it into an array there is always 0 bit get inserted automatically, i know that its from the bitwise operation inside the loop, how can i delete it and get the actual binary value from a string?
this is the code
def padding():
a = "Testing"
byte_a = a.encode('utf-8')
print(byte_a)
print(bin(byte_a[0]))
print(bin(byte_a[1]))
print(bin(byte_a[2]))
i = 0
x = []
#byte_a.append(1)
while True:
x.insert(i, ((byte_a[i//8]>>(7-(i%8)))&1))
print(x)
i+=1
and here is the output:
b'Testing'
0b1010100
0b1100101
0b1110011
[0]
[0, 1]
[0, 1, 0]
[0, 1, 0, 1]
[0, 1, 0, 1, 0]
[0, 1, 0, 1, 0, 1]
[0, 1, 0, 1, 0, 1, 0]
[0, 1, 0, 1, 0, 1, 0, 0]
[0, 1, 0, 1, 0, 1, 0, 0, 0]
[0, 1, 0, 1, 0, 1, 0, 0, 0, 1]
[0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 1]
[0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 1, 0]
[0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0]
[0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1]
[0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0]
[0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 1]
[0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0]
[0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1]
[0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1, 1]
[0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1]
[0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0]
[0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0]
[0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0, 1]
...
as you can see the array had the first value of 0 for every binary.
thanks guys

How to apply the CRC16 on a received bitstream? (Python)

could anyone here help me understand, how I apply the CRC16 on the received string?
stream = [0, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1, 0]
rest = [1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1]
https://pastebin.com/7CMtSEE6
I received this APRS message. I decoded it also with mutlimon directly and the I got the same ascii message as with my own PYTHON code.
Inside the stream variable is the received message after removing the bitstuffing and before reversing the bit sequence of each byte.
What I tried so far, but did not work, was to apply the x^16+x^12+x^5+1 on it. I wanted to get the same "rest" as you can see it in the pastebin. This rest was also inside the stream but for better readability I put that to the other variable. Bits inside the rest were just copynpasted out, so they have the same order.
So, where in my train of thoughts am I wrong?
Be frankly with me and I would like to ask you to tell me step by step, because I need to "see" how it works to understand it better. That would be really helpful for me.
So starting first:
stream = [0, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, ...
I would now have the stream devided by the reverse of 0x11021
0,1,1,0,0,0,0,1,0,1,0,0,0,1,0,1,0,0,0,0,0,0,1,...
1,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,1
0,0,1,0,0,0,1,1,0,1,0,0,0,0,0,1,0,1
Is that the correct start? Or do I have to reverse each set of bytes in the stream first?
Thank you in advance,
Andreas
This question is related to this one, but specifically around crc16...
How to decode Bell 202 signal? (APRS data from International Space Station)
There are eleven CRC-16's in Greg Cook's catalog with that polynomial, and probably more that aren't in his catalog. So when you say "CRC-16", it could be a lot of different things.
Your binary data can be interpreted as bytes in two different ways, either most significant bit first, or least significant bit first. If the former, then the CRC in "rest" would be the CRC-16/GENIBUS. If the latter, then it is the X-25 CRC-16.
In both cases the CRC register is initialized with all 1's (0xffff), and at the end is inverted, i.e. exclusive-or'ed with 0xffff. It is that initialization and post-processing that you are probably missing in your attempt to replicate the CRC.
Here is some simple C code for the X-25 CRC-16, which would operate on your data assuming least significant bit first:
unsigned crc16x25(unsigned char *data, size_t len) {
unsigned crc = 0xffff;
while (len--) {
crc ^= *data++;
for (unsigned k = 0; k < 8; k++)
crc = crc & 1 ? (crc >> 1) ^ 0x8408 : crc >> 1;
}
crc ^= 0xffff;
return crc;
}
So if you feed that 0x86 0xa2 ... 0x47, you will get 0xec3f as the CRC.

Categories