How to calculate the variable weight/price check digit in a GTIN? - barcode

The GS1 standard defines GTIN codes that contain a 4 or 5 digit variable weight or price. This variable weight or price part needs to be validated with a check digit.
How can I calculate this check digit in code? Please be aware that this check digit is not calculated the same way as the default check digit at the end of the GTIN.
For reference: https://www.gs1.org/docs/barcodes/GS1_General_Specifications.pdf Page 466/467

You did not specify the language of choice, so hopefully this will be useful to you. Essentially it can be boiled down to array lookups and simple math. This code is written is AutoITScript, but it should be simple to translate to others.
I am sure this code could be made smarter (with 2d array lookup, functions etc) but for simplicity I made it more readable.
It should be noted that AutoIT is not a typed language, so 1234 can be treated as a number and a string all the same. Also that AutoIT uses $var[index1][index2] to access a 0-based array value.
Hope this helps you on ! If so, please accept solution.
RESULTS
============= barcode checksum variable length ================
https://www.gs1.org/docs/barcodes/GS1_General_Specifications.pdf section 7.9.2
----------------------
Doing checksum on 4 digits: 2875
digit 1 is: 2 --> Check = 4
digit 2 is: 8 --> Check = 5
digit 3 is: 7 --> Check = 1
digit 4 is: 5 --> Check = 3
==> Sum is 39
==> CHECK DIGIT IS RIGHT-MOST DIGIT = 9
----------------------
Doing checksum on 5 digits: 14685
digit 1 is: 1 --> Check = 5
digit 2 is: 4 --> Check = 8
digit 3 is: 6 --> Check = 7
digit 4 is: 8 --> Check = 4
digit 5 is: 5 --> Check = 9
==> Sum is 33
==> CHECK DIGIT IS 40-sum = 7
The code
#include <String.au3>
; barcode checksum
; https://www.gs1.org/docs/barcodes/GS1_General_Specifications.pdf section 7.9.2
;
ConsoleWrite("============= barcode checksum variable length ================" & #CRLF)
ConsoleWrite("https://www.gs1.org/docs/barcodes/GS1_General_Specifications.pdf section 7.9.2" & #CRLF)
; index 0 index 1 index 2 index 3
; --- wf2- ---------- --- wf3 ----------- --- wf5+ ---------- ---- wf5- ----------
Static $f[4][10] = [ [0,2,4,6,8,9,1,3,5,7], [0,3,6,9,2,5,8,1,4,7], [0,5,1,6,2,7,3,8,4,9], [0,5,9,4,8,3,7,2,6,1] ]
; 4 digit version 5 digit version
static $lookupTables[2][5] =[ [ 0, 0, 1, 3] , [ 2, 0, 3, 2, 0] ]
; ==================================
; show some examples
_calcit(2875)
_calcit(14685)
; -------------------- function to calculate digits
;
Func _calcit($thenumber)
; assume the length is correct here
; theNumber is now array [0] is left-most
local $arrayNumber = _StringExplode($thenumber, "", 0)
local $t ; our lookup digit
local $tSum ; summarized
local $theversion = 0 ; 0 = 4 digit, 1 = 5 digits
if StringLen($thenumber) = 4 then
$theversion = 0
else
$theversion = 1
endif
ConsoleWrite("----------------------" & #CRLF)
ConsoleWrite("Doing checksum on " & StringLen($thenumber) & " digits: " & $thenumber & #CRLF)
for $i = 0 to StringLen($thenumber)-1
ConsoleWrite("digit " & $i+1 & " is: " & $arrayNumber[$i])
;lookup the number in our 2d array. Which row determined by the lookup table.
$t = $f [$lookupTables[$theversion][$i]] [$arrayNumber[$i] ]
ConsoleWrite(" --> Check = " & $t & #CRLF)
$tSum = $tSum + $t
Next
if $theversion = 0 then
$tSum = $tSum * 3
ConsoleWrite(" ==> Sum is " & $tSum & #CRLF)
ConsoleWrite(" ==> CHECK DIGIT IS RIGHT-MOST DIGIT = " & StringRight($tSum, 1) & #CRLF)
else
ConsoleWrite(" ==> Sum is " & $tSum & #CRLF)
$tSum = 40-$tSum
ConsoleWrite(" ==> CHECK DIGIT IS 40-sum = " & StringRight($tSum, 1) & #CRLF)
EndIf
EndFunc

Related

How to capitalise a random letter?

In VBScript is it possible to select a random letter and capitalize it until all letters have been capitalized at least once?
Dim a
a = "Hello"
For i=o To Len(a)-1
If Mid(a,i+1, 1) = Mid(a, i+1, 1) Then
b = Mid(a, i+1, 1)
MsgBox b
End If
Next
That's some code I gathered. So far it reads a string letter by letter.
I want the output to be something like:
hello
Hello
hEllo
etc.
but I can't see how to do it without getting into super complex Mid Left Right statements that become confusing. Is it possible? Or do I need to use something like Mid(LCase(s,1,1) & Mid(UCase(s,2,1)) & Mid(LCase(s,3,3)?
Use Len() to determine the positions of letters to capitalize and Left() + UCase(Mid()) + Mid() to actually uppercase the letter at p =
Option Explicit
Dim s : s = "hello"
Dim l : l = Len(s)
Dim i : i = 0
Do Until s = UCase(s)
Dim p : p = Fix(Rnd() * l) + 1
If Mid(s, p, 1) <> UCase(Mid(s, p, 1)) Then s = Left(s, p - 1) & UCase(Mid(s, p, 1)) & Mid(s, p + 1)
WScript.Echo i, p, s
i = i + 1
Loop
output:
cscript 52911013.vbs
0 4 helLo
1 3 heLLo
2 3 heLLo
3 2 hELLo
4 2 hELLo
5 4 hELLo
6 1 HELLo
7 4 HELLo
8 5 HELLO

Numerical sequence of 1 2 4

I need help in providing an algorithm for a numerical sequence which should display a series of 1 2 4 and its consecutive summations.
e.g. If my input value is 20, it should display
1 2 4 8 9 11 15 16 18
Wherein
1 = 1
2 = 1 + 1
4 = 2 + 2
8 = 4 + 4
And the summation of 1 and 2 and 4 will repeat again starting with the present number which is 8 and so on..
9 = 8 + 1
11 = 9 + 2
15 = 11 + 4
16 = 15 + 1
18 = 16 + 2
As you can see, it should not proceed to 22 (18 + 4) since our sample input value is 20. I hope you guys get my point. I'm having a problem in designing the algorithms in the for loop. What I have now which is not working is
$input = 20;
for ($i = $i; $i < $input; $i = $i+$i) {
if($i==0){
$i = 4;
$i = $i - 3;
}elseif($i % 4 == 0){
$i = $i + 1;
}
print_r("this is \$i = $i<br><br>");
}
NOTE: Only one variable and one for loop is required, it will not be accepted if we use functions or arrays. Please help me, this is one of the most difficult problems I've encountered in PHP..
you can use the code
$input = 20;
$current = 1;
$val = 1;
while($val < $input){
print_r("this is \$val = $val\n");
$val = $val + $current;
$current = ($current == 4 ? 1 : $current*2);
}
see the online compiler
Since you have mentioned Only one variable and one for loop is required
Try this,
$input = 20;
for ($i = 1; $i < $input; $i) {
if($i>$input) break;
print_r("this is \$i = $i<br><br>");
$i=$i+1;
if($i>$input) break;
print_r("this is \$i = $i<br><br>");
$i=$i+2;
if($i>$input) break;
print_r("this is \$i = $i<br><br>");
$i=$i+4;
}
Online Compiler
def getSeq(n):
if n == 1:
return [1]
temp = [1]
seq = [ 1, 2, 4]
count, current, prev = 0, 0, 1
while True:
current = prev + seq[count]
if current > n:
break
prev = current
temp += [current]
count = (count + 1) % 3
return temp
print getSeq(20)
I'm pretty sure that this one is going to work
the case that we have to take care of is n == 1 and return a static result [1].
in other cases the second value is repeating circularly and adding up to previous value.
This Python solution should be implementable in any reasonable language:
limit = 20
n = 1 << 2
while n >> 2 < limit:
print(n >> 2)
n = (((n >> 2) + (2 ** (n & 3))) << 2) + ((n & 3) + 1) % 3
Perl Equivalent (using the style of for loop you expect):
$limit = 20;
for ($n = 1 << 2; $n >> 2 < $limit; $n = ((($n >> 2) + (2 ** ($n & 3))) << 2) + (($n & 3) + 1) % 3) {
print($n >> 2, "\n");
}
OUTPUT
1
2
4
8
9
11
15
16
18
EXPLANATION
The basic solution is this:
limit = 20
n = 1
i = 0
while n < limit:
print(n)
n = n + (2 ** i)
i = (i + 1) % 3
But we need to eliminate the extra variable i. Since i only cycles through 0, 1 and 2 we can store it in two bits. So we shift n up two bits and store the value for i in the lower two bits of n, adjusting the code accordingly.
Not only one variable and one for loop, no if statements either!

if i declare 5 values in 25 size of an array, how can i find used size of the array in VB Script?

dim a(100)
a(0)=9,a(1)=3,a(2)=-3,a(3)=8,a(4)=2
how can i find size of used array(i.e used size is 5
You have to count the non-empty elements:
Option Explicit
Function UsedElms(a)
UsedElms = 0
Dim i
For i = 0 To UBound(a)
If Not IsEmpty(a(i)) Then UsedElms = UsedElms + 1
Next
End Function
Dim a(5)
a(2) = 2
a(4) = 4
WScript.Echo "ub:", UBound(a), "sz:", UBound(a) + 1, "us:", UsedElms(a)
output:
cscript 23027576.vbs
ub: 5 sz: 6 us: 2
Here's a hacky one-liner that I just thought of. It essentially counts the number of empty elements by converting them to spaces and then trimming them off.
intLastIndex = UBound(a) - Len(Join(a, " ")) + Len(Trim(Join(a, " ")))
Just for fun! Don't go putting it into your production code. It would certainly be more efficient as a two-liner:
s = Join(a, " ")
intLastIndex = UBound(a) - Len(s) + Len(Trim(s))
Ekkehard has the proper answer here, though. This hack only works if your array is filled contiguously.

VB6 Password generation tool fails on Chinese / Russian PCs

I have inherited some legacy vb6 code. It's a tool which generates a local admin password for a windows pc. The password is supposed to contain only a limited set of characters. The tool works fine.. as long as it's not run on chinese or russian PCs. There, it generates weird passwords that nobody can type in. Of course it has something to do with the current operating system culture. The Chr(CharCode) function returns some weird characters on those systems. Anyone has a clue on how to make this culture invariant? I only want the tool to generate passwords with standard ASCII characters.
Function generatePassword(PASSWORD_LENGTH)
Dim NUMLOWER, NUMUPPER, LOWERBOUND, UPPERBOUND, LOWERBOUND1, UPPERBOUND1, SYMLOWER, SYMUPPER
Dim newPassword, count, pwd
Dim pCheckComplex, pCheckComplexUp, pCheckComplexLow, pCheckComplexNum, pCheckComplexSym, pCheckAnswer
NUMLOWER = 48 ' 48 = 0
NUMUPPER = 57 ' 57 = 9
LOWERBOUND = 65 ' 65 = A
UPPERBOUND = 90 ' 90 = Z
LOWERBOUND1 = 97 ' 97 = a
UPPERBOUND1 = 122 ' 122 = z
SYMLOWER = 33 ' 33 = !
SYMUPPER = 46 ' 46 = .
pCheckComplexUp = 0 ' used later to check number of character types in password
pCheckComplexLow = 0 ' used later to check number of character types in password
pCheckComplexNum = 0 ' used later to check number of character types in password
pCheckComplexSym = 0 ' used later to check number of character types in password
' initialize the random number generator
Randomize
newPassword = ""
count = 0
Do Until count = PASSWORD_LENGTH
' generate a num between 2 and 10
' if num <= 2 create a symbol
If Int((10 - 2 + 1) * Rnd + 2) <= 2 Then
'pwd = Int( ( SYMUPPER - SYMLOWER + 1 ) * Rnd + SYMLOWER )
pwd = Int((UPPERBOUND1 - LOWERBOUND1 + 1) * Rnd + LOWERBOUND1)
' if num is between 3 and 5 create a lowercase
ElseIf Int((10 - 2 + 1) * Rnd + 2) > 2 And Int((10 - 2 + 1) * Rnd + 2) <= 5 Then
pwd = Int((UPPERBOUND1 - LOWERBOUND1 + 1) * Rnd + LOWERBOUND1)
' if num is 6 or 7 generate an uppercase
ElseIf Int((10 - 2 + 1) * Rnd + 2) > 5 And Int((10 - 2 + 1) * Rnd + 2) <= 7 Then
pwd = Int((UPPERBOUND - LOWERBOUND + 1) * Rnd + LOWERBOUND)
Else
pwd = Int((NUMUPPER - NUMLOWER + 1) * Rnd + NUMLOWER)
End If
If Chr(pwd) <> "l" And Chr(pwd) <> "I" Then
newPassword = newPassword + Chr(pwd)
count = count + 1
End If
'Check to make sure that a proper mix of characters has been created. If not discard the password.
If count = (PASSWORD_LENGTH) Then
For pCheckComplex = 1 To PASSWORD_LENGTH
'Check for uppercase
If Asc(Mid(newPassword, pCheckComplex, 1)) > 64 And Asc(Mid(newPassword, pCheckComplex, 1)) < 90 Then
pCheckComplexUp = 1
'Check for lowercase
ElseIf Asc(Mid(newPassword, pCheckComplex, 1)) > 96 And Asc(Mid(newPassword, pCheckComplex, 1)) < 123 Then
pCheckComplexLow = 1
'Check for numbers
ElseIf Asc(Mid(newPassword, pCheckComplex, 1)) > 47 And Asc(Mid(newPassword, pCheckComplex, 1)) < 58 Then
pCheckComplexNum = 1
'Check for symbols
ElseIf Asc(Mid(newPassword, pCheckComplex, 1)) > 32 And Asc(Mid(newPassword, pCheckComplex, 1)) < 47 Then
pCheckComplexSym = 1
End If
Next
'Add up the number of character sets. We require 3 or 4 for a complex password.
pCheckAnswer = pCheckComplexUp + pCheckComplexLow + pCheckComplexNum + pCheckComplexSym
If pCheckAnswer < 3 Then
newPassword = ""
count = 0
End If
End If
Loop
'The password is good so return it
generatePassword = newPassword
End Function
Deanna's comment is correct. Just change Chr to ChrW and change Asc to AscW.
ChrW accepts Unicode code points
Chr accepts "ANSI" code points, and the meaning of a particular code point will be different depending on the system code page. For example on Chinese and Russian code pages.
As Russian and Chinese PCs (and many others) do not operate with an ASCII character set but culture specific UNICODE then I am not surprised that your password program generates a password that cannot be typed in by the user.
You'll only achieve success by moving away from ASCII.
There are a number of open source password generators available online. If you can find one with source code then you'll be quids in , so to speak.

How can I generate this pattern of numbers?

Given inputs 1-32 how can I generate the below output?
in. out
1
1
1
1
2
2
2
2
1
1
1
1
2
2
2
2
...
Edit Not Homework.. just lack of sleep.
I am working in C#, but I was looking for a language agnostic algorithm.
Edit 2 To provide a bit more background... I have an array of 32 items that represents a two dimensional checkerboard. I needed the last part of this algorithm to convert between the vector and the graph, where the index aligns on the black squares on the checkerboard.
Final Code:
--Index;
int row = Index >> 2;
int col = 2 * Index - (((Index & 0x04) >> 2 == 1) ? 2 : 1);
Assuming that you can use bitwise operators you can check what the numbers with same output have in common, in this case I preferred using input 0-31 because it's simpler (you can just subtract 1 to actual values)
What you have?
0x0000 -> 1
0x0001 -> 1
0x0010 -> 1
0x0011 -> 1
0x0100 -> 2
0x0101 -> 2
0x0110 -> 2
0x0111 -> 2
0x1000 -> 1
0x1001 -> 1
0x1010 -> 1
0x1011 -> 1
0x1100 -> 2
...
It's quite easy if you notice that third bit is always 0 when output should be 1 and viceversa it's always 1 when output should be 2
so:
char codify(char input)
{
return ((((input-1)&0x04)>>2 == 1)?(2):(1));
}
EDIT
As suggested by comment it should work also with
char codify(char input)
{
return ((input-1 & 0x04)?(2):(1));
}
because in some languages (like C) 0 will evaluate to false and any other value to true. I'm not sure if it works in C# too because I've never programmed in that language. Of course this is not a language-agnostic answer but it's more C-elegant!
in C:
char output = "11112222"[input-1 & 7];
or
char output = (input-1 >> 2 & 1) + '1';
or after an idea of FogleBird:
char output = input - 1 & 4 ? '2' : '1';
or after an idea of Steve Jessop:
char output = '2' - (0x1e1e1e1e >> input & 1);
or
char output = "12"[input-1>>2&1];
C operator precedence is evil. Do use my code as bad examples :-)
You could use a combination of integer division and modulo 2 (even-odd): There are blocks of four, and the 1st, 3rd, 5th block and so on should result in 1, the 2nd, 4th, 6th and so on in 2.
s := ((n-1) div 4) mod 2;
return s + 1;
div is supposed to be integer division.
EDIT: Turned first mod into a div, of course
Just for laughs, here's a technique that maps inputs 1..32 to two possible outputs, in any arbitrary way known at compile time:
// binary 1111 0000 1111 0000 1111 0000 1111 0000
const uint32_t lu_table = 0xF0F0F0F0;
// select 1 bit out of the table
if (((1 << (input-1)) & lu_table) == 0) {
return 1;
} else {
return 2;
}
By changing the constant, you can handle whatever pattern of outputs you want. Obviously in your case there's a pattern which means it can probably be done faster (since no shift is needed), but everyone else already did that. Also, it's more common for a lookup table to be an array, but that's not necessary here.
The accepted answer return ((((input-1)&0x04)>>2 == 1)?(2):(1)); uses a branch while I would have just written:
return 1 + ((input-1) & 0x04 ) >> 2;
Python
def f(x):
return int((x - 1) % 8 > 3) + 1
Or:
def f(x):
return 2 if (x - 1) & 4 else 1
Or:
def f(x):
return (((x - 1) & 4) >> 2) + 1
In Perl:
#!/usr/bin/perl
use strict; use warnings;
sub it {
return sub {
my ($n) = #_;
return 1 if 4 > ($n - 1) % 8;
return 2;
}
}
my $it = it();
for my $x (1 .. 32) {
printf "%2d:%d\n", $x, $it->($x);
}
Or:
sub it {
return sub {
my ($n) = #_;
use integer;
return 1 + ( (($n - 1) / 4) % 2 );
}
}
In Haskell:
vec2graph :: Int -> Char
vec2graph n = (cycle "11112222") !! (n-1)
Thats pretty straightforward:
if (input == "1") {Console.WriteLine(1)};
if (input == "2") {Console.WriteLine(1)};
if (input == "3") {Console.WriteLine(1)};
if (input == "4") {Console.WriteLine(1)};
if (input == "5") {Console.WriteLine(2)};
if (input == "6") {Console.WriteLine(2)};
if (input == "7") {Console.WriteLine(2)};
if (input == "8") {Console.WriteLine(2)};
etc...
HTH
It depends of the language you are using.
In VB.NET, you could do something like this :
for i as integer = 1 to 32
dim intAnswer as integer = 1 + (Math.Floor((i-1) / 4) mod 2)
' Do whatever you need to do with it
next
It might sound complicated, but it's only because I put it into a sigle line.
In Groovy:
def codify = { i ->
return (((((i-1)/4).intValue()) %2 ) + 1)
}
Then:
def list = 1..16
list.each {
println "${it}: ${codify(it)}"
}
char codify(char input)
{
return (((input-1) & 0x04)>>2) + 1;
}
Using Python:
output = 1
for i in range(1, 32+1):
print "%d. %d" % (i, output)
if i % 4 == 0:
output = output == 1 and 2 or 1
JavaScript
My first thought was
output = ((input - 1 & 4) >> 2) + 1;
but drhirsch's code works fine in JavaScript:
output = input - 1 & 4 ? 2 : 1;
and the ridiculous (related to FogleBird's answer):
output = -~((input - 1) % 8 > 3);
Java, using modulo operation ('%') to give the cyclic behaviour (0,1,2...7) and then a ternary if to 'round' to 1(?) or 2(:) depending on returned value.
...
public static void main(String[] args) {
for (int i=1;i<=32;i++) {
System.out.println(i+"="+ (i%8<4?1:2) );
}
Produces:
1=1 2=1 3=1 4=2 5=2 6=2 7=2 8=1 9=1
10=1 11=1 12=2 13=2 14=2 15=2 16=1
17=1 18=1 19=1 20=2 21=2 22=2 23=2
24=1 25=1 26=1 27=1 28=2 29=2 30=2
31=2 32=1

Resources