nativescript-nfc rfid reader plugin unique id issue [closed] - unique

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I use the nfc reader and Im able to get the unique id of the tags. When I read the tag the id is called like this:
id: [ -52, 22, -61, -67, 80, 1, 4, -32, [length]: 8 ]
How can I get the hexadecimal one? It should be called CC:16:C3:BD:50:01:04:E0. That's also the way I will have database entries. So the way I get the id back is somehow useless for me.
I will appreciate any helpful answer. Thank you in advance.

The range of byte is -128 to 127
e.g. For Java
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
"byte: The byte data type is an 8-bit signed two's complement integer. It has a minimum value of -128 and a maximum value of 127 (inclusive)."
You just need to convert that to display as a String as Hex.
Usually According to ISO/IEC 14443-3 the UID is a 7 byte Number with byte 4 being part of the check bytes
I don't know how to convert bytes to string in Nativescript but in Java you can do
StringBuilder Uid = new StringBuilder();
for (int i = 0; i < result.length; i++) {
// byte 4 is a check byte
if (i == 3) continue;
Uid.append(String.format("%02X ", result[i]));
}
While skipping the check byte is not totally needed to give you an unique ID this code does it.
For JavaScript How to convert hex string into a bytes array, and a bytes array in the hex string? should provide the answer with a similar iterate over the array converting each byte to hex string

Related

How to convert vector in to an array? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I want to convert vector into an array in Ocatve.
I want to compare a string available in a vector or not. The vector is predefined. I am going to use strcmp function to check for the existance of a particular string in the list. This function works only with arrays, but not with vectors.
How can we convert a vector into an array in octave?
Please, find the difference between vector and arrays using strcmp in below screenshot:
Octave (and Matlab) do not have vector types. They also do not have list types.
What they do have are arrays, and cell arrays.
Both have 2 dimensions or more by convention.
A "horizontal vector" in octave is effectively a 2D array where the first dimension (i.e. rows) is 1.
A "vertical / column vector" in octave is effectively a 2D array where the second dimension (i.e. columns) is 1.
A "scalar" is a 2D array where the first and second dimensions are both 1.
Octave / matlab provides functions to test if an array is effectively a 'vector' or a 'scalar', but they are not considered as separate 'types'. So the question of converting a 'vector' to an equivalent 'array' does not make sense.
An array has the restriction that all its elements must be of the same type (e.g. numerical, character, class-derived objects, etc)
A cell array has no such restriction; each element of the cell array can have any type, including a numerical array or another cell array.
It sounds like what you want is to create a cell array, where you make each element a string.
You should be able to take it from there.

How to generate 8 character unique string without checking if it exist in the database

I want to generate 1 million qr codes every day. every qr code should have random unique value. in order to achieve uniqueness i have two options as below
1)First option is to generate 8 characters alphanumeric random string and check if it is exist in database. if not exist then store in the database and if exist then retry gain.
The problem with this approach is that it takes around 45 minutes to generate 1 million unique alphanumeric strings because we have to check every time if generated alphanumeric string is present in the database or not.
2)Second option is to generate unique token by appending 6 characters alphanumeric string with time(hour+minute+second+date+month+year). with this option i am able to generate 1 million unique alphanumeric string within 5 minutes.
i am using this second option but the problem with this option is that the alphanumeric string generated from this is 15 characters long. i want to generate alphanumeric string with only 8 character long. if i use second option then timestamp itself takes 8 characters and string become easily guessable.
I want to know how to generate 8 characters random and unique alphanumeric string without checking if it exist in the database or not.
Using set structure is enough
def random_str(n):
base = 'abcefghijklmnopqrstuvwxzy0123456789'
return ''.join([base[ord(i) % len(base)] for i in os.urandom(n)])
s = set()
SIZE = 1000000
while len(s) < SIZE:
s.add(random_str(5))
And, it takes about 5 seconds.
Sorry, I missing every day. Split the 8 characters to tow part: the first part(3 characters) meaning every day or other conditions, can store it in Mysql and query before generate once or using days since 1970 and change it to 36 based number. The seconds part 5 characters using the code above to generate unique keys.
You could use symmetric encryption (example AES) to create your unique ids.
First, create a unique secret key.
Second, initialize a counter.
For every new code you want to generate, encrypt the current counter value with your encryption key and use it. And increment the counter.
PS: Only minor weird thing is you want to use alphanumeric output. So use a 32 bit key and you will get 32 bit output. Now, convert that into 8 byte alphanumeric output.
Try this ...
token = SecureRandom.urlsafe_base64(6) # This will give you 8 char uniq string
Hope this will work for you.
New plan.
Each day, take the numbers 000000..999999, shuffle them, base64 encode them (3 characters).
Take the number of days since your service started, as a six digit number, base64 encode that (3 chars). Good for a million days.
Note these numbers are unique and "random", but definitely not cryptographically secure at all.
EDIT some pseudo code:
int a[1000000];
int daysWeveBeenDoingThis;
for i from 0 to 999999 {
a[i] = i
}
ShuffleArray(a);
function getNthTokenToday ( int n ) {
String s = '';
long x =
(a[n] << 16L) + daysWeveBeenDoingThis);
for c from 0 to 5 {
v = (x >> (c*6)) & 63;
b = 'a-zA-Z0-9/.'[v];
s.append(b)(
}
return s;
}

Multiply two matrix in cuda c [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have to matrices A(32*32) and B(32*n), in which 'n' is coming from inputs and is between 2000 to 2000000.
I have two kind of inputs one is integers between 0 to 255 and the other one is 0,1. this multiplication is in a loop that iterates 3000 times. B(32*n) comes form input and is constant in all of the iterations but A(32*32) can change in each iteration.
//read B from file
//read A from file
double D[3000];
for(int i = 0; i < 3000; i++)
{
C = multiply(A, B);
// D[i] = mean of all elements in C
// build A from B using D[i] (this part is really complicated sequential process that contains lots of if and switches)
}
What is the fastest way to do this?
thank you.
Nobody here is going to write code for you, that is not what Stack Overflow is intended for. However, it would appear to be that there are a number of characteristics of the problem which you should be looking to exploit to improve the performance of your code:
Recognise that because one of the matrices only contains 0 or 1 and you are performing this in integer, what you are describing as matrix multiplication is really a large number of independent sparse sums
Recognise that because the next operation is to compute an average, you don't actually have to store the intermediate dot products and could directly perform a reduction on partial results of the matrix row summation
There are probably parallel primitives in the thrust library which you could use for prototyping, and an optimal hand written kernel would be aiming to fuse both the first and most of the second part of the operation into a single kernel.

How to store longitude and latitude in database. MySQL won't go further than -99.9999999 [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Ok right now I'm storing lat and lng columns as DECIMAL(10,8)
I'm trying to insert this:
-117.1779216 However it keeps inserting as this : -99.99999999
Why is this? I see every other board storing it as DECIMAL, but it won't go back more when I'm giving it 10 places before the period... So I would think it could go -117.. None of them are marked as unsigned either.
Precision (10) - Scale (8) = 2
2 is the number of digits you can have to the left of the decimal
If you increase the precision, you can have more digits to the left of the decimal.
The declaration syntax for a DECIMAL column is DECIMAL(M,D). The ranges of values for the arguments in MySQL 5.1 are as follows:
M is the maximum number of digits (the precision). It has a range of 1 to 65. (Older versions of MySQL permitted a range of 1 to 254.)
D is the number of digits to the right of the decimal point (the scale). It has a range of 0 to 30 and must be no larger than M.
Taken from the manual :D
You set it to DECIMAL(10,8) with the number -117.1779216. MySQL reads this as -117.17792160 because it needs 8 digits of precision, but you said it should only have 10 digits, so anything over 99 makes it 11 digits and invalid.

Can I get more explanations for BSON?

I am trying to understand BSON via http://bsonspec.org/#/specification, but still some questions remain.
let's take an example from the web site above:
{"hello": "world"} → "\x16\x00\x00\x00\x02hello\x00\x06\x00\x00\x00world\x00\x00"
Question 1
in the above example, for the encoded bytes results, the double quotes actually are not part of the results, right?
Question 2
I understand that the first 4 bytes \x16\x00\x00\x00 is the size of the whole BSON doc.
And it is little endian format. But why? Why not take big endian?
Question 3
How comes the size of the example doc being \x16, i.e. 22?
Question 4
Normally, if I want to encode the doc by myself, how do I calculate the size of the doc? I think my trouble majorly is how to decide the size of UTF-8 string?
Let's take another example:
{"BSON": ["awesome", 5.05, 1986]}
→
"\x31\x00\x00\x00\x04BSON\x00\x26\x00\x00\x00\x020\x00\x08\x00\x00
\x00awesome\x00\x011\x00\x33\x33\x33\x33\x33\x33\x14\x40\x102\x00\xc2\x07\x00\x00
\x00\x00"
Question 5
In this example, there is an array. according to the specification, for array, it is actually a list of {key, value} pairs, whereas the key is 0, 1, etc. My question is so the 0, 1 here are strings too, right?
Question 1
in the above example, for the encoded bytes results, the double quotes actually are not part of the results, right?
The quotes are not part of the strings. They're used to mark JSON strings
Question 2
And it is little endian format. But why? Why not take big endian?
Choice of endianness is largely a matter of preference. One advantage of little endian is that commonly used platforms are little endian, and thus don't need to reverse the bytes.
Question 3
How comes the size of the example doc being \x16, i.e. 22?
There are 22 bytes (including the length prefix)
Question 4
Normally, if I want to encode the doc by myself, how do I calculate the size of the doc? I think my trouble majorly is how to decide the size of UTF-8 string?
First write out the document, and then go back to fill in the length.
Question 5
n this example, there is an array. according to the specification, for array, it is actually a list of {key, value} pairs, whereas the key is 0, 1, etc. My question is so the 0, 1 here are strings too, right?
Yes. Zero terminated strings without length prefix to be exact. (Called cstring in the list). Just like an embedded document.