How to calculate check digit for Barcode 128 Auto php - php

I have a string which uses 128B and 128C conversion. ANCV0005YRF01234.
So
ANCV = 128B
0005 = 128C
YRF0= 128B
1234= 128 C
Cant use code 128 Auto as it converts the 0 after F into 128C (which i dont want.). At the moment using two different scripts and concatenating the barcode images,Need to calculate the check digit for that. Not sure how the check digit will be generated ?.
Any help is appreciated. Thanks

I got sum of characters by weights (rightmost column) from your example string = 5468 % 103 = 9 for a checksum if you want to switch back and forth.

Related

Store many numbers as a single unique number

I have the necessity to store many numbers (i can decide which numbers) as a single unique number from which i should be able to retrieve the original number.
I already know 2 ways to do this:
1) Fundamental theorem of arithmetic (Prime Numbers)
Say i have 5 values, i assign a prime number other than 1 to each value
a = 2
b = 3
c = 5
d = 7
e = 13
If i want to store a, b and c i can multiply them 2*3*5=30 and i know no other product of primes can be 30. Then to check if a value contains, for example, b, all i need to do is 30 % b == 0
2) Bitmask
Just like Linux permissions, use powers of 2 and sum each value
But these 2 methods grow up fast (1st way faster than 2nd), and using prime numbers requires me to have a lot of primes.
Is there any other method to do this efficiently when you have, for example, a thousand values?
If you are storing, say, base 10 numbers, then do a conversion through base 11 numbers. With the increased base, you have an extra 'digit'. Use that digit as a separator. So, three base 10 numbers "10, 42, 457" become "10A42A457": a single base 11 number (with 'A' as the additional digit).
Whatever base your original numbers are in, increase the base by 1 and concatenate, using the extra digit as a separator. That will give you a single number in the increased base.
That single number can be stored in whatever number base you find convenient: binary, denary or hex for example.
To retrieve your original numbers just convert to base 11 (or whatever) and replace the extra digit with separators.
ETA: You don't have to use base 11. The single number "10A42A457" is also a valid hexadecimal number, so any base of 11 or above could be used. Hex may be easier to work with than base 11.
Is there any other method to do this efficiently when you have, for example, a thousand values?
I an not a mathematician but it's basic math, all depends on range
Range 0-1: You want to store 4 numbers 0-1 - it's basically binary system
Number1 + Number2 * 2^1 + Number3 * 2^2 + Number4 * 2^3
Range 0-50 You want to store 4 numbers 0-49
Number1 + Number2 * 50^1 + Number3 * 50^2 + Number4 * 50^3
Range 0-X You want to store N numbers 0-X
Number1 + Number2 * (X+1)^1 + Number3 * (X+1)^2 + ... + NumberN * (X+1)^(N-1)
If you have no pattern for your numbers (so it can get compressed in some way) there is really no other way.
It's also super easy for computer to resolve the number unlike the prime numbers
Predetermined values
#FlorainK comment pointed me to fact I missed
(i can decide which numbers)
The only logical solution is give your numbers references
0 is 15342
1 is 6547
2 is 76234
3 is "i like stack overflow"
4 is 42141
so you'll work range 0-4 (5 options) and whatever combination length. Use reference when "encoding" and "decoding" the number
a thousand values?
so you'll work with Range 0-999
0 is 62342
1 is 7456345653
2 is 45656234532
...
998 is 7623452
999 is 4324234326453
Let's say you use 64-bit system and programming/db language that works with 64-bit integers
2^64 = 18446744073709551616
your max range is 1000^X < 18446744073709551616 where X is number of numbers you can store in one single 64-bit integer number
Which is only 6.
You can store only 6 separate numbers 0-999 that will fit one 64-bit integer number.
0,0,0,0,0,0 is 0
1,0,0,0,0,0 is 1
0,1,0,0,0,0 is 1000
999,999,999,999,999,999 is ~1e+18
Ok so you want to store "a,b,c" or "a,b" or "a,b,c,d" or "a" etc. (thanks #FlorianK)
in such case just could use bitwise operators and powers of two
$a = 1 << 0; // 1
$b = 1 << 1; // 2
$c = 1 << 2; // 4
$d = 1 << 3; // 8
.. etc
let's say $flag has $a and $c
$flag = $a | $c; // $flag is integer here
now check it
$ok = ($flag & $a) && ($flag & $c); // true
$ok = ($flag & $a) && ($flag & $b); // false
so in 64 bit system/language/os you can use up to 64 flags which gives you a 2^64 combinations
there is no really other option. prime numbers are much worse for this as you skip many numbers in-between while binary system uses every single number.
I see you are using database and you want to store this in DB.
I really think we are dealing here with XY Problem and you should reconsider your application instead of making such workarounds.

Dealing with big size binary numbers in php

Algorithm:
Given a number n, list(L(n)) of all binary numbers of size n can be calculated from L(n-1) in the following way:
Suppose array L(n-1) contains all the binary numbers of length n-1.
Reverse L(n-1) and call RL(n-1).
Append '0' to all binary numbers in L(n-1). Append '1' to all binary numbers in RL(n-1).
Merge new appended arrays L(n-1) and RL(n-1) to get all the binary numbers of size n.
Base Case,
if n=1, output = [0,1].
Example, if n=2, We can get list of all binary numbers of size 2 in the following way:
Let a = [0,1] be list of binary numbers of size (2-1) = 1.
Let b = reverse of a = [1,0].
Append 0 to all elements in a. New a = [00,11].
Append 1 to all elements in b. New b = [11,10].
Merge new a and b. [00,11,11,10].
Problem Statement: Given a number n, find list of n binary numbers.
Solution: A simple recursive or non-recursive solution works if n is less than 20.
Question: My code fails if a bigger number is passed lets say 40 and exceeds memory limits.
Why? - 'Coz For a number lets say 40, Total number of binary numbers will be power(2,40) which is huge(1048576 * 1048576).
So, Is there any better algorithm or way to solve the above problem?
What you can do is store only first n numbers. It seems that in every case you only need first n-1 numbers to only reverse and append with '1's for the correct output.

How to covert string of phone number to int or float without .0 at the end?

Value from database is 63906xxxxxxx which is a phone number.Now, how to pull this number exactly 12 digit since i have a 32 bit version of PhP.
Here is my code for getting as string:
$contactNum = $ngo_cotactNo;
$numeric_indexed_array = array_values($contactNum);
$n = $numeric_indexed_array[0];
dd($n);
output:"639063449729"
Here is my code for getting as string converted to int:
$contactNum = $ngo_cotactNo;
$numeric_indexed_array = array_values($contactNum);
$n = $numeric_indexed_array[0];
$number = (int) $n;
dd($number);
output : 2147483647
string to float
output : 639063449729.0
What i want is the whole number 639063449729 so that i can use it for passing to the API for my sms messaging.I've been working on this since yesterday but no luck.
Don't convert phone numbers into integers or floats. In a 32 bit environment positive integers going up to 2^31, which is a little bit more than 2 000 000 000. It is simply impossible to place a twelve digit number in 32 bits. Don't use floats neither. Floats having a limited precision, so your phone number might be rounded to the next representable number. Please leave them in a string.
Wikipedia says:
In the international telephone network, the format of telephone
numbers is standardized by ITU-T recommendation E.164. This code
specifies that the entire number should be 15 digits or shorter, and
begin with a country prefix.

Choosing colours based on IP address

In my PHP (v5.2.17) script, I want to select a unique colour for the current user's entries, based on their IP address. I don't want to map the colour values from the hex codes, because I also want to fade the colours of each entry over time. The colour must always have one of the RGB values set to zero (it's like a set of bright, primary colours).
Is there a clever mathematical solution to do this?
I'd greatly appreciate if any math genuises reading this would share some insights. :-)
Are you really limiting yourself to just six "base" colors?
255 255 0
255 0 255
0 255 255
0 0 255
0 255 0
255 0 0
I presume you're going to apply a linear function to these colors to try to fade them out. This won't necessarily look as good as you think it might -- RGB as a representation isn't very linear. You can cheaply approximate a better "linear" representation by using an HSV or HSL representation instead. They surely aren't perfect but it will feel a little more natural than RGB.
As for mapping the IP address to a color, you could store these color combinations in an array and pick among the six elements by using a simple hash function. Something like this might be sufficient:
b1, b2, b3, b4 = <split the four bytes from an IP address>
index = (b1 * 17 + (b2 * 17 + (b3 * 17 + b4))) % 6
(I just picked the multiplier 17 out of the air -- its binary representation is 10001, which means the bits of each byte in the address get "smeared" over each other. There might be better values. Once you've gotten a few colors selected and a handful of IP addresses you can try changing the multiplier to e.g. 21 or 53 and see what makes most sense.)
Although this won't give you a result where one of {R,G,B} is always 0, a HSL representation might look good. As an example, let hue be a decimal value from 0 to 1, defined by
(float)(octet[0] + octet[1] << 8 + octet[2] << 16 + octet[3] << 24) / (2^32-1)
, where each octet[i] is an unsigned byte, and ^ is exponentiation). And then perhaps set lightness and saturation by hand, as per your preference. Just an idea!
As an added bonus, this makes fading the colours easy (just subtract some portion of "time" from saturation/lightness).
are you using a database to store the relations? you could always grab the user's IP Address
<?php
function userIP(){
if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$userIp=$_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
$userIp=$_SERVER['REMOTE_ADDR'];
}
return trim($userIp);
}
?>
Then use the function to set a usable variable of the IP:
<?php
$Users_IP_address = userIP();
?>
once you have that, you can assign a color that isn't in use and save the association for future reference.

increment alpha numerically values

Like the title suggests I need to do something like so...
$i++;//we all know this.
$value = 'a';
increment($value);// i need this functionality
//output
string [ b ]
///here are some more samples, to help you understand...
increment('b'); //output// c
increment('z'); //output// A [capital A not fussy but would be good :) ]
increment('9'); //output// a1
increment('a1'); //output// a2
increment('aa1'); //output// aa2
and so on...
UPDATE
well lets say I use numeric values
$id++;
I would end up with a massive number eventuall 1310743942525;
which can take alot more space than say `ab34c9" im trying to save length of characters to save on db ...
You try to treat it as a base 62 number:
http://www.pgregg.com/projects/php/base_conversion/base_conversion.php
with source code at
http://www.pgregg.com/projects/php/base_conversion/base_conversion.inc.phps
convert it to decimal, increment, and convert it back to base 62
UPDATE
From how I read the code, you could have a workflow like this:
$value = 'ab8Zb';
$value_base10 = base_base2dec($value, 62);
$value_base10++;
$value = base_dec2base($value_base10, 62); // should be 'ab8Zc'
If all you are trying to do is save database space, consider this.
In MySQL you can have a field with a type UNSIGNED BIGINT. The maximum size of this field is 18446744073709551615 and the storage space is only 8 bytes.
If you were to convert this number (1.844 x 10^19) to base-62, it would be represented as LygHa16AHYF. You would need a CHAR(11) (11 bytes) or a VARCHAR(11) (12 bytes) in order to store the converted number.
If you used VARCHAR for the field type, smaller numbers would take less space, but for the larger numbers it actually takes more. 8 bytes for a huge number is pretty minimal anyway. I would save the effort and just make the DB field a UNSIGNED BIGINT.
You can use ascii codes of each letter. This is just a simple example that will show you the idea, ofcourse it need a lot of modyfications if you want to increment 'aa1' into 'aa2' but impossible is nothig ;P You will just need to write few conditions.
function increment($value)
{
if(strlen($value)>1)
return false;
$asciiCode = ord($value);
return chr($asciiCode + 1);
}
http://www.asciitable.com/ - ASCII codes table :)

Categories