Just need to convert a UPC Code

Anyone got code to convert a long 10 digit UPC code to a short 6 digit one?

I've written code to convert short UPC to long if it helps any looks something like this:

        If Mid(cUPC, 7, 1) < "3" Then
            '//if ends in "012"
            '//expand as 1-2,8-10th
            cUPC = "00" + Mid(cUPC, 2, 2) + Mid(cUPC, 7, 1) + "0000" + Mid(cUPC, 4, 3)
        ElseIf Mid(cUPC, 7, 1) = "3" Then
            '//if ends in "3"
            '//expand as 1-3,9-10th
            cUPC = "00" + Mid(cUPC, 2, 3) + "00000" + Mid(cUPC, 5, 2)
        ElseIf Mid(cUPC, 7, 1) = "4" Then
            '//if ends in "4"
            '//expand as 1-4,10th
            cUPC = "00" + Mid(cUPC, 2, 4) + "00000" + Mid(cUPC, 6, 1)
        Else
            '//must end in "5678 or 9"
            '//expand as UPC 1-5,10th
            cUPC = "00" + Mid(cUPC, 2, 5) + "0000" + Mid(cUPC, 7, 1)
        End If

This code assumes the 6 digit code has leading and trailing system and check digit already added and operates on the 2-7 digit to expand six to ten plus leading country code (assumed zero) and system digit also zero and checkdigit makes 13 when done.

Question is I want to go the other way and this is giving me a headache trying to reverse this code :)

LVL 1
rayfordAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

rayfordAuthor Commented:
Ugly old code might help if I turn some + signs into & too

Increasing points for a really good payout to anyone motivated or who can locate this code for us!
0
rayfordAuthor Commented:
More data for UPC conversions:
In plain English as to what above code does:

Take the six digits and analyse like this-

LastDigit UseLastDigit AddZeroes
0-2       Yes @Spot3   add 4 0's @4-7
3         no           add 5 0's @4-8
4         no           add 5 0's @5-9
5-9       Yes @Spot10  add 4 0's @6-9

examples:
123451 = 12100 00345
123453 = 12300 00045
123454 = 12340 00005
078349 = 07834 00009

0
rayfordAuthor Commented:
This is so not proportional fontish!
LastDigit UseLastDigit AddZeroes
0-2       Yes @Spot3   add 4 0's @4-7
3          no             add 5 0's @4-8
4          no             add 5 0's @5-9
5-9       Yes @Spot10  add 4 0's @6-9

0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

mcriderCommented:
OK, Here you go... With an explaination first...

The 10 and 6 digit UPC numbers you're trying to get are actually 12 and 8 digits...

In your example, the UPC code:

   12100 00345

is actually:

   012100 003454

Where the leading 0 is part of the manufacturer's ID and the trailing 4 is the checksum for the UPC.

If you look at a UPC Bar code, you will see this, and it's notated as:

       0  12100 00345  4

Same thing goes for a short UPC (UPCE)

   123451

is actually:

   01234514

and is notated as:


       0  123451  4
     
With that said, you can use the function below, "UPCtoUPCE" to convert a standard UPC to a short UPCE.  You must pass the full long UPC with the check digit to the function as a string, and it will return the short UPC with the check digit.  If you pass an invalid LONG UPC the function will return nothing.

To use this function, place it in a module, and call it like this:

Dim ShortUPC as String
ShortUPC = UPCtoUPCE("012100003454")



Cheers!



THE CODE:


Function UPCtoUPCE(myString As String) As String
    'CONVERT A 12 DIGIT UPC NUMBER TO AN 8 DIGIT UPC NUMBER
    Dim iVal, jVal, CheckHold1, CheckHold2, CheckSum As Integer
    Dim RevString As String
    Dim Mfr As String
    Dim ProductCode As String
    Dim ShortUPC As String
   
    UPCtoUPCE = ""
    If Len(myString) = 12 Then
        RevString = ""
        CheckSum = -1
        CheckHold1 = 0
        CheckHold2 = 0
        For iVal = 12 To 1 Step -1
            RevString = RevString + Mid$(myString, iVal, 1)
        Next iVal
        For iVal = 2 To 12 Step 2
            CheckHold1 = CheckHold1 + Val(Mid$(RevString, iVal, 1))
        Next iVal
        CheckHold1 = CheckHold1 * 3
        For iVal = 3 To 11 Step 2
            CheckHold2 = CheckHold2 + Val(Mid$(RevString, iVal, 1))
        Next iVal
        CheckHold1 = CheckHold1 + CheckHold2
        For iVal = 0 To 9
            If (CheckHold1 + iVal) Mod 10 = 0 Then
                CheckSum = iVal
                Exit For
            End If
        Next iVal
        If CheckSum <> Val(Left$(RevString, 1)) Then Exit Function
        'GOT A VALID UPC
        Mfr = Left$(myString, 6): ProductCode = Mid$(myString, 7, 5)
        If Right(Mfr, 1) <> 0 Then
            ShortUPC = Mfr + Right$(ProductCode, 1) + CStr(CheckSum)
            If isUPCe(ShortUPC) = True Then UPCtoUPCE = ShortUPC
            Exit Function
        End If
        Select Case Right$(Mfr, 3)
            Case "000", "100", "200"
                ShortUPC = Left$(Mfr, 3) + Right$(ProductCode, 3) + Mid$(Mfr, 4, 1) + CStr(CheckSum)
                If isUPCe(ShortUPC) = True Then UPCtoUPCE = ShortUPC
                Exit Function
            Case "300", "400", "500", "600", "700", "800", "900"
                ShortUPC = Left$(Mfr, 4) + Right$(ProductCode, 2) + Mid$(Mfr, 4, 1) + CStr(CheckSum)
                If isUPCe(ShortUPC) = True Then UPCtoUPCE = ShortUPC
                Exit Function
            Case Else
                ShortUPC = Left$(Mfr, 5) + Right$(ProductCode, 1) + Mid$(Mfr, 5, 1) + CStr(CheckSum)
                If isUPCe(ShortUPC) = True Then UPCtoUPCE = ShortUPC
                Exit Function
        End Select
    End If
End Function

Function isUPCe(myString As String) As Boolean
    'SEE IF STRING PASSED IS A VALID 8 DIGIT UPC NUMBER
    Dim iVal, jVal, CheckHold1, CheckHold2, CheckSum As Integer
    Dim NewUPC, RevString As String
   
    isUPCe = False
    If Len(myString) = 8 Then
        NewUPC = "0" + Mid$(myString, 2, 2)
        Select Case Val(Mid$(myString, 7, 1))
            Case 0
                NewUPC = NewUPC + "00000" + Mid$(myString, 4, 3)
            Case 1
                NewUPC = NewUPC + "10000" + Mid$(myString, 4, 3)
            Case 2
                NewUPC = NewUPC + "20000" + Mid$(myString, 4, 3)
            Case 3
                NewUPC = NewUPC + Mid$(myString, 4, 1) + "00000" + Mid$(myString, 5, 2)
            Case 4
                NewUPC = NewUPC + Mid$(myString, 4, 2) + "00000" + Mid$(myString, 6, 1)
            Case 5 To 9
                NewUPC = NewUPC + Mid$(myString, 4, 3) + "0000" + Mid$(myString, 7, 1)
        End Select
        NewUPC = NewUPC + Right$(myString, 1)
        RevString = ""
        CheckSum = -1
        CheckHold1 = 0
        CheckHold2 = 0
        For iVal = 12 To 1 Step -1
            RevString = RevString + Mid$(NewUPC, iVal, 1)
        Next iVal
        For iVal = 2 To 12 Step 2
            CheckHold1 = CheckHold1 + Val(Mid$(RevString, iVal, 1))
        Next iVal
        CheckHold1 = CheckHold1 * 3
        For iVal = 3 To 11 Step 2
            CheckHold2 = CheckHold2 + Val(Mid$(RevString, iVal, 1))
        Next iVal
        CheckHold1 = CheckHold1 + CheckHold2
        For iVal = 0 To 9
            If (CheckHold1 + iVal) Mod 10 = 0 Then
                CheckSum = iVal
                Exit For
            End If
        Next iVal
        If CheckSum = Val(Left$(RevString, 1)) Then
            isUPCe = True
        End If
    End If
End Function
0
rayfordAuthor Commented:
If you'll check my question you'll see I've already provided my own small version of UPCtoUPCE (10 to 6 or as you say 12 to 8 digit conversion).  This already works great so what I need is "UPCEtoUPC"

by the way many UPC now also have leading 13th digit is a Country code like 0 USA and 1 Japan.  Guess they only plan to support ten countries :)

Thanks!
0
rayfordAuthor Commented:
My fault partly for starting it off apparently incorrectly but at the bottom I explain that

"Question is I want to go the OTHER way and this is giving me a headache trying to reverse this code"
0
rayfordAuthor Commented:
Nevermind Sorry MCRIDER I haven't had my coffee yet today - please resubmit your question I will award points that actually WAS what I was looking for..  

I hate it when that happens!  
0
rayfordAuthor Commented:
Told ya my head hurts
0
mcriderCommented:
What I gave you WAS 10DIGIT to 6DIGIT.

Resubmitting for grade...
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mcriderCommented:
P.S.  Get some coffee for me please...

;-)
0
rayfordAuthor Commented:
I've found this function does not work properly.  Here is the UPC off of a Can of Lysol spray for instance.

0 19200 02938 4

which gets into the 000,100,200 part of the ISUPCE function and gets rejected for some reason.  Bug?

Other UPC's seem to work especially those which are converted first from Short UPCE to long UPC and then fed to the function.
0
mcriderCommented:
One other thing I forgot to mention with the routine I gave you...

If you pass a "valid" UPC to the UPCtoUPCE function, and it does not conform to the product numbering standard, you will not get a short UPC back...

What I mean is for manufacturer codes that end with:

000,100,200 - Product code must be between 00000 and 00999

300,400,500,600,700,800,900 - Product code must be between 00000 and 00099

10,20,30,40,50,60,70,80,90 - Product code must be between 00000 and 00009

non-zero ending - Product code must be between 00005 and 00009


So, even though this UPC 0 31600 25500 2 is a "valid" UPC, it can not be compressed into an 8 digit UPC because the product code 25500 is greater than 00099.


Cheers!
0
mcriderCommented:
NOT A BUG!

In your example, the UPC 0 19200 02938 4 can't be compressed...

the manufacurer code ends in 200.  This means the product code must be between 00000 and 000999.  It is 02938.


Cheers!
0
rayfordAuthor Commented:
OK I've determined that only UPC's which were derived originally from UPCE's can be converted back.  Not all UPC's can be shortened.

Again please repost your answer for credit.

Thanks!
0
rayfordAuthor Commented:
Good Answer!
0
mcriderCommented:
Here's a URL that explains UPC/UPCE encoding...

http://www.uc-council.org/old_files/d36-2.htm


Cheers!

(Worth any more points?) ;-)

Cheers!
0
mcriderCommented:
DANG EXPERTS EXCHANGE!!

You were supposed to be able to click on the URL I gave you, but Cheers! and Worth got added to the URL even with 2 CR's... They changed EE over the weekend... UCK!


Anyways, heres the URL:

http://www.uc-council.org/old_files/d36-2.htm
0
mcriderCommented:
Another bizzare EE BUG... that stuff after the 2nd url post was not typed into the comment box... I don't know where that came from...

Cheers!
0
mcriderCommented:
Thanks for the "A"!


Cheers!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.