Function isInt(toCheck As Variant) As Boolean
Dim temp As Integer
On Error GoTo err
temp = CInt(toCheck) 'trys to set the given variable to an integer
isInt = True 'if suceeds then return true
Exit Function
err:
isInt = False 'else return false
End Function
Function Code128b(ByVal rawData) As String
' Function Code128b, returns encoded code128b string (with checkbit)
' default functionality returns string with start and stop bits expected by
' the Code128bWin.ttf font Created by Brian Dobson (http://www.dobsonsw.com)
' provided a 'switch' to allow for apparent characters expected by ID Automations (as well
' as perhaps others') implimentation of Code128B
'
' This function currently has an export limit of 999 characters
Dim offset, highAscii, total, character, asciivalue, checkdigit
Dim check, holder, version, startChar, encodeNum, checkCounter
Dim tstartChar, nstartChar, EndChar, ntChar, tnChar, spaceChar
Dim encodedString
Dim previousEncoding ' 0=text 1=numeric
offset = 32
highAscii = 18
total = 104 ' initiates checksum total calculator
version = 0 ' set this identifier to '1' for ID automation's version (i hope)
If version = 0 Then ' www.dobsonsw.com's Version
tstartChar = "š"
nstartChar = "›"
EndChar = "œ"
ntChar = "–"
tnChar = "•"
spaceChar = "€"
ElseIf version = 1 Then 'ID Automation's (and others?) version
tstartChar = "Ì"
nstartChar = "Í"
EndChar = "Î"
ntChar = "É"
tnChar = "È" ' I'm guessing at this character, because I have not found an explicit example of this case yet atm
spaceChar = "€" ' I'm guessing at this character too
'elseif version = 2 then ' placeholder for 'future' start/stop/seguey characters
'tstartChar = ""
'nstartChar = ""
'EndChar = ""
'ntChar = ""
'tnChar = ""
'spaceChar =""
End If
For stringCounter = 1 To Len(rawData)
checkCounter = checkCounter + 1
'Determine the startCharacter
If stringCounter = 1 Then
If isInt(Mid(rawData, stringCounter, 1)) Then
If isInt(Mid(rawData, stringCounter + 1, 1)) Then
startChar = nstartChar
previousEncoding = 1
total = total + 1
Else
startChar = tstartChar
End If
Else
startChar = tstartChar
End If
End If
If isInt(Mid(rawData, stringCounter, 1)) Then
If isInt(Mid(rawData, stringCounter + 1, 1)) Then
If previousEncoding = 0 Then
encodedString = encodedString & tnChar
previousEncoding = 1
total = total + (((Asc(tnChar) - offset - highAscii)) * checkCounter)
checkCounter = checkCounter + 1
End If
encodeNum = Mid(rawData, stringCounter, 1) & Mid(rawData, stringCounter + 1, 1)
If offset + encodeNum >= 127 Then
character = Chr(offset + encodeNum + highAscii)
Else
character = Chr(offset + encodeNum)
End If
asciivalue = offset + encodeNum
stringCounter = stringCounter + 1
'don't forget to grab the checkbit information for the 'skipped' character
checkdigit = ((asciivalue - offset) * (checkCounter))
Else
If previousEncoding = 1 Then
encodedString = encodedString & ntChar
previousEncoding = 0
total = total + (((Asc(ntChar) - offset - highAscii)) * checkCounter)
checkCounter = checkCounter + 1
End If
character = Mid(rawData, stringCounter, 1)
asciivalue = Asc(character)
checkdigit = ((asciivalue - offset) * (checkCounter))
End If
Else
If previousEncoding = 1 Then
encodedString = encodedString & ntChar
previousEncoding = 0
total = total + (((Asc(ntChar) - offset - highAscii)) * checkCounter)
checkCounter = checkCounter + 1
End If
character = Mid(rawData, stringCounter, 1)
asciivalue = Asc(character)
checkdigit = ((asciivalue - offset) * (checkCounter))
End If
' Replaces any ' ' characters (space) with '€' Characters
' I'm not 1000% sure whether this is the character ID Automation's Font uses either...
If Asc(character) = 32 Then
character = spaceChar
End If
encodedString = encodedString & character
total = total + checkdigit 'adds checkdigit data to running total
Next stringCounter
''' Creates 'checksum character'
check = total Mod 103
If (check + offset) >= 127 Then
holder = check + offset + highAscii
Else
holder = check + offset
End If
If Chr(holder) <> " " Then
checkdigit = Chr(holder)
Else
checkdigit = spaceChar
End If
' Returns barcode string
Code128b = startChar & encodedString & checkdigit & EndChar
End Function
private string Format_Code128(string InString)
{
int Sum;
int Checksum;
int Checkchar;
string MyString;
int CVal;
//
//Initialize running total with value of
//Subset B start character
//
Sum = 104;
//
//Scan the string and add character value times position
//
for (int i = 1; i <= InString.Length; i++)
{
//
// Copy one character from InString position i to MyString
//
MyString = InString.Substring(i - 1, 1);
//
// Get the numeric value of the character and subtract
// 32 to shift (the space character, ASCII value 32, has
// a numeric value of 0 as far as Code 128 is concerned)
//
CVal = Convert.ToInt32(Convert.ToChar(MyString)) - 32;
//
// Add the weighted value into the running sum
//
Sum = Sum + (CVal * i);
}
//
// Calculate the Modulo 103 checksum
//
Checksum = Sum % 103;
//
// Now convert this number to a character. This conversion
// takes into account the particular mapping of the font
// being used (this example is for the font published by
// Azalea Software.
//
if (Checksum == 0)
{
Checkchar = 174;
}
else if (Checksum < 94)
{
Checkchar = Checksum + 32;
}
else
{
Checkchar = Checksum + 71;
}
//
// Now format the final output string: start character,
// data, check character, and stop character
//
MyString = Convert.ToChar(162) + InString + Convert.ToChar(Checkchar) + Convert.ToChar(164);
return MyString;
}