|
actually code 128 have many different types or subtype with different standard and one of them is
code128 B , code 128 A, code 128C
i want to generate generalize code 128 that right now standard 128 cani get some help regarding that one
chetan
|
|
|
|
|
This is Code128. There isn't one standard of which A, B, and C are alternate variants. The code comprises the three sub-codes, and that's the whole story.
This library doesn't use Code C (which is really just a space optimization), but it does switch freely between Code A and Code B, seeking the most space-efficient output.
In other words, any reader that is Code128-compliant should be able to read the output from this library.
|
|
|
|
|
Hi Chris,
How do I encode Interleaved 2 of 5 this way. You software work brilliantly however the barcode scanner we have wont read code128 only ITF interleaved 2 of 5.
Thanks,
Ken
|
|
|
|
|
I just want to say how much I appreciate your piece of code. I was looking for a quick and easy way of generating and printing barcodes (for Code 128), and after searching and getting tons and tons of materials and sample codes, couldn't find anything that work as sweet as this piece of code. Thanks for even giving the source code, 'cos I had to make a modifification to it as well to correct the problem of OutOfIndexRange error.
Thanks again and please keep it up!
|
|
|
|
|
Hi! Can you provide me any help on decoding PDF417 barcodes? I really need help on this!
|
|
|
|
|
|
Dear Chris Wuestefeld I am new to Barcode.Your Applicaation is so good whch is help me alot...But My problem is that when i am print in Zebra printer then all image are not print due to Length of barcode image is very large .So Please give me some suggestion or through put some source code to reduce the length of my barcode image
Thanks in advance
|
|
|
|
|
Dear Sir Or Madam
I need your help , i use control to create barcode but the problem with me when i want to convert to binary file give me message that should install GDI+ library (gdiplus.dll) i installed it but still give message error gdi codec not contain and if i goto to register gdiplus.dll not apple to register message the point not there . i have winxp (SP2) and i now gdiplus.dll not there and i can't convert from image to binary
i tried it in many computers but the same problem
any body can help me
saa@jawa-software.com
saad badr
|
|
|
|
|
This article really helped me. I converted it to vb 2005 because I don't use c# but it does scan all the time...and it scans well. Can't say that about all the other code 128 in .net that I have seen. This code was really easy to work with.
|
|
|
|
|
Thanks for the posting. It works well except that I cannot reduce the bar width.
With bar weight of 1, the image is too wide for my label. I tried to resize the image, but the resulting image is not sharp.
Is there a way to reduce the bar width when generating the image?
Thanks
|
|
|
|
|
There's nothing I can do with it smaller than 1; that's the width of the smallest bar.
Perhaps you could experiment with the GDI properties of the canvas you're putting the resulting Image onto. Maybe you can create it with different properties, so that it can be resized more finely?
|
|
|
|
|
The resolution is limited by the Graphics you are using as in
Graphics gr = Graphics.FromImage(myimg).
In order to take advantage of the high resolution graphics provided by our printer, we need to work with that graphics directly, rather than getting it from the Image.
|
|
|
|
|
Yes, I understand.
I haven't tried it, but I would expect that you should be able to DrawImage() onto your hi-res Graphics the image that I return. This method has various overloads that will do the scaling for you automatically, and given your higher resolution, hopefully won't come out blocky.
|
|
|
|
|
I would like to increase the bar width:
for my case: 1 is too small and 2 is too large; 1.5 is perfect. I know the width of this line: width = ( (codes.Length-3) * 11 + 35) * BarWeight; should be change but I dont know exactly which value will be ok!
Do you have any suggestion?
Thanks in advance,
|
|
|
|
|
As I mentioned in the message you replied to, the image that's returned from my library is only half of what's going on. In order to use this you'll need to draw that image onto a Graphics surface, and the resolution and scaling of that object can determine the physical size of your output.
|
|
|
|
|
Could you tell me how to control the width about barcode bitmap,thank you!
|
|
|
|
|
You can get some very gross control of this by specifying different values for the BarWeight parameter when you call MakeBarcodeImage(), but that's just going to make the image 2x or 3x, etc., larger (since the width of the image that I output is essentially the number of bars times the bar weight, plus the quiet zone).
However, .Net's GDI+ library makes it fairly easy to manipulate images. It's not too hard to stretch the image that I return to make it a particular size. Or, if your method of output allows it, you can simply stick the returned image into an PictureBox control (as I've done in the sample application), set the control to stretch, and then set the width of the control as you desire.
|
|
|
|
|
Chris,
Here is my modification to your code that implement Code C [ I guess ]
Sorry about the formatter (I use BlackSun -eclipse plugin- for C# programming).
Maybe it's not the best way to change your code, but I've tried to change the less I could.
I've tested it using bcTester (http://www.bctester.de/en/index.htm) and it seems to work well, but in some cases bcTester fails to understand it with the original implementation and with this modification. For example, with barcode "a", "abc" or "ABC" bcTester fails. I don't know why. Maybe bcTester is not OK or maybe there is a bug in here, or ... who knows.
A test with a real scanner would be better I think (I don't have one).
Feedback will be appreciated.
Regards.
Gustavo.
PD: This POST is long. If you want to cut it down, just do it.
---------------------------------
using System;
using System.Text;
namespace GenCode128
{
public enum CodeSet
{
CodeA, CodeB, CodeC
}
/// <summary>
/// Represent the set of code values to be output into barcode form
/// </summary>
public class Code128Content
{
private int[] mCodeList;
/// <summary>
/// Create content based on a string of ASCII data
/// </summary>
/// <param name="AsciiData">the string that should be represented</param>
public Code128Content(string AsciiData)
{
mCodeList = StringToCode128(AsciiData);
}
/// <summary>
/// Provides the Code128 code values representing the object's string
/// </summary>
public int[] Codes
{
get { return mCodeList; }
}
/// <summary>
/// Transform the string into integers representing the Code128 codes
/// necessary to represent it
/// </summary>
/// <param name="AsciiData">String to be encoded</param>
/// <returns>Code128 representation</returns>
private int[] StringToCode128(string AsciiData)
{
// turn the string into ascii byte data
byte[] asciiBytes = Encoding.ASCII.GetBytes(AsciiData);
// decide which codeset to start with
CodeSet currcs = Code128Code.GetBestCodeSetAB(
asciiBytes.Length > 0 ? asciiBytes[0] : -1, asciiBytes.Length > 1 ? asciiBytes[1] : -1);
// check for Code C: If first 2 bytes are numeric, then we use Code C
if (asciiBytes.Length > 1)
{
if (Code128Code.IsCodeCAllowedForChar(asciiBytes[0], asciiBytes[1]))
currcs = CodeSet.CodeC;
}
// set up the beginning of the barcode
System.Collections.ArrayList codes = new System.Collections.ArrayList(asciiBytes.Length + 3); // assume no codeset changes, account for start, checksum, and stop
codes.Add(Code128Code.StartCodeForCodeSet(currcs));
// add the codes for each character in the string
for (int i = 0; i < asciiBytes.Length; i++)
{
int thischar = asciiBytes[i];
int nextchar = asciiBytes.Length > (i + 1) ? asciiBytes[i + 1] : -1;
codes.AddRange(Code128Code.CodesForChar(thischar, nextchar, ref currcs));
//Jump 2 bytes if Code C is active
if (currcs == CodeSet.CodeC)
i++;
}
// calculate the check digit
int checksum = (int) (codes[0]);
for (int i = 1; i < codes.Count; i++)
{
checksum += i * (int) (codes[i]);
}
codes.Add(checksum % 103);
codes.Add(Code128Code.StopCode());
int[] result = codes.ToArray(typeof(int)) as int[];
return result;
}
}
/// <summary>
/// Static tools for determining codes for individual characters in the content
/// </summary>
public static class Code128Code
{
#region Constants
private const int cSHIFT = 98;
private const int cCODEA = 101;
private const int cCODEB = 100;
private const int cCODEC = 99;
private const int cSTARTA = 103;
private const int cSTARTB = 104;
private const int cSTARTC = 105;
private const int cSTOP = 106;
#endregion
/// <summary>
/// Determines the best starting code set based on the the first two
/// characters of the string to be encoded
/// </summary>
/// <param name="csa1">First character of input string</param>
/// <param name="csa2">Second character of input string</param>
/// <returns>The codeset determined to be best to start with</returns>
private static CodeSet GetBestStartSet(Code128Code.CodeSetAllowed csa1, Code128Code.CodeSetAllowed csa2)
{
int vote = 0;
vote += (csa1 == Code128Code.CodeSetAllowed.CodeA) ? 1 : 0;
vote += (csa1 == Code128Code.CodeSetAllowed.CodeB) ? -1 : 0;
vote += (csa2 == Code128Code.CodeSetAllowed.CodeA) ? 1 : 0;
vote += (csa2 == Code128Code.CodeSetAllowed.CodeB) ? -1 : 0;
return (vote > 0) ? CodeSet.CodeA : CodeSet.CodeB; // ties go to codeB due to my own prejudices
}
/// <summary>
/// Determines the best starting code set based on the the char1 and char2
/// </summary>
/// <param name="char1">First character</param>
/// <param name="char2">Second character</param>
/// <returns>The codeset determined to be best to start with</returns>
public static CodeSet GetBestCodeSetAB(int char1, int char2)
{
Code128Code.CodeSetAllowed csa1 = char1 != -1
? Code128Code.CodesetAllowedForChar(char1)
: Code128Code.CodeSetAllowed.CodeAorB;
Code128Code.CodeSetAllowed csa2 = char2 != -1
? Code128Code.CodesetAllowedForChar(char2)
: Code128Code.CodeSetAllowed.CodeAorB;
return GetBestStartSet(csa1, csa2);
}
/// <summary>
/// Get the Code128 code value(s) to represent an ASCII character, with
/// optional look-ahead for length optimization
/// </summary>
/// <param name="CharAscii">The ASCII value of the character to translate</param>
/// <param name="LookAheadAscii">The next character in sequence (or -1 if none)</param>
/// <param name="CurrCodeSet">The current codeset, that the returned codes need to follow;
/// if the returned codes change that, then this value will be changed to reflect it</param>
/// <returns>An array of integers representing the codes that need to be output to produce the
/// given character</returns>
public static int[] CodesForChar(int CharAscii, int LookAheadAscii, ref CodeSet CurrCodeSet)
{
int[] result;
int shifter = -1;
// We don't need to switch CodeSet if we are using Code C and the next 2 chars are compatible with it
if (CurrCodeSet == CodeSet.CodeC && IsCodeCAllowedForChar(CharAscii, LookAheadAscii))
{
result = new int[1];
result[0] = CodeCValueForChars(CharAscii, LookAheadAscii);
}
else
{
//If our next 2 chars are compatible with Code C, switch to it.
if (IsCodeCAllowedForChar(CharAscii, LookAheadAscii))
{
CurrCodeSet = CodeSet.CodeC;
result = new int[2];
result[0] = cCODEC; //shifter
result[1] = CodeCValueForChars(CharAscii, LookAheadAscii);
}
else
{
if (!CharCompatibleWithCodeset(CharAscii, CurrCodeSet))
{
// If we are working with Code C or if we have a lookahead character AND if the next character is ALSO not compatible
if (CurrCodeSet == CodeSet.CodeC
|| ((LookAheadAscii != -1) && !CharCompatibleWithCodeset(LookAheadAscii, CurrCodeSet)))
{
// we need to switch code sets
switch (CurrCodeSet)
{
case CodeSet.CodeA:
shifter = cCODEB;
CurrCodeSet = CodeSet.CodeB;
break;
case CodeSet.CodeB:
shifter = cCODEA;
CurrCodeSet = CodeSet.CodeA;
break;
case CodeSet.CodeC:
{
CodeSet cs = GetBestCodeSetAB(CharAscii, LookAheadAscii);
switch (cs)
{
case CodeSet.CodeA:
shifter = cCODEB;
CurrCodeSet = CodeSet.CodeB;
break;
case CodeSet.CodeB:
shifter = cCODEA;
CurrCodeSet = CodeSet.CodeA;
break;
}
break;
}
}
}
else
{
// no need to switch code sets, a temporary SHIFT will suffice
shifter = cSHIFT;
}
}
if (shifter != -1)
{
result = new int[2];
result[0] = shifter;
result[1] = CodeValueForChar(CharAscii);
}
else
{
result = new int[1];
result[0] = CodeValueForChar(CharAscii);
}
}
}
return result;
}
/// <summary>
/// Tells us which codesets a given character value is allowed in
/// </summary>
/// <param name="CharAscii">ASCII value of character to look at</param>
/// <returns>Which codeset(s) can be used to represent this character</returns>
public static CodeSetAllowed CodesetAllowedForChar(int CharAscii)
{
if (CharAscii >= 32 && CharAscii <= 95)
{
return CodeSetAllowed.CodeAorB;
}
else
{
return (CharAscii < 32) ? CodeSetAllowed.CodeA : CodeSetAllowed.CodeB;
}
}
/// <summary>
/// Tells us if Code C is compatible with the 2 parameter bytes
/// </summary>
/// <param name="CharAscii">First char</param>
/// <param name="NextCharAscii">Second char</param>
/// <returns>Which codeset(s) can be used to represent this character</returns>
public static bool IsCodeCAllowedForChar(int CharAscii, int NextCharAscii)
{
return CharAscii >= 48 && CharAscii <= 57 && NextCharAscii >= 48 && NextCharAscii <= 57;
}
/// <summary>
/// Determine if a character can be represented in a given codeset
/// </summary>
/// <param name="CharAscii">character to check for</param>
/// <param name="currcs">codeset context to test</param>
/// <returns>true if the codeset contains a representation for the ASCII character</returns>
public static bool CharCompatibleWithCodeset(int CharAscii, CodeSet currcs)
{
if (currcs == CodeSet.CodeC)
return false;
CodeSetAllowed csa = CodesetAllowedForChar(CharAscii);
return csa == CodeSetAllowed.CodeAorB
|| (csa == CodeSetAllowed.CodeA && currcs == CodeSet.CodeA)
|| (csa == CodeSetAllowed.CodeB && currcs == CodeSet.CodeB);
}
/// <summary>
/// Gets the integer code128 code value for a character (assuming the appropriate code set)
/// </summary>
/// <param name="CharAscii">character to convert</param>
/// <returns>code128 symbol value for the character</returns>
public static int CodeValueForChar(int CharAscii)
{
return (CharAscii >= 32) ? CharAscii - 32 : CharAscii + 64;
}
/// <summary>
/// Gets the integer code128 code value for pair character (assuming Code C)
/// </summary>
/// <param name="CharAscii">First character to convert</param>
/// <param name="NextCharAscii">Second character to convert</param>
/// <returns>code128 symbol value for the character</returns>
public static int CodeCValueForChars(int CharAscii, int NextCharAscii)
{
return (CharAscii - 48) * 10 + NextCharAscii - 48;
}
/// <summary>
/// Return the appropriate START code depending on the codeset we want to be in
/// </summary>
/// <param name="cs">The codeset you want to start in</param>
/// <returns>The code128 code to start a barcode in that codeset</returns>
public static int StartCodeForCodeSet(CodeSet cs)
{
switch (cs)
{
case CodeSet.CodeA:
return cSTARTA;
case CodeSet.CodeB:
return cSTARTB;
case CodeSet.CodeC:
return cSTARTC;
}
throw new Exception("Must be a CodeSet specified");
}
/// <summary>
/// Return the Code128 stop code
/// </summary>
/// <returns>the stop code</returns>
public static int StopCode()
{
return cSTOP;
}
/// <summary>
/// Indicates which code sets can represent a character -- CodeA, CodeB, or either
/// </summary>
public enum CodeSetAllowed
{
CodeA, CodeB, CodeAorB
}
}
}
|
|
|
|
|
gspadari,
Thank you very much for the updated code. You are right, the original implementation was not recognizable by bcTester also with barcode scanner. With your modification it works perfectly alright.
Thanks,
Govind
|
|
|
|
|
Govind, do you have examples of input that generated unreadable barcodes? I haven't seen this myself, but since we've got customer apps using the library as I originally posted it, I'd like to head off any problems before they're discovered.
|
|
|
|
|
Chris, my input characters are "3578200203952089061510001". After using gspadari's updated code I could generate the bar code which gives me correct result when tested with BCTester. But still my client complains that he could not detect the code by scanning using the bar code scanner machine. Please help me what could be wrong with your original code or gspadari's code which is not working for my input. Thanks, Govind
|
|
|
|
|
I just ran into a problem with the Code C implementation today as well... take away the double quotes and here's my data that doesn't work properly
"2009 w/" - the w scans fine with a hand scanner but corrupts with a common software that recognizes a bar codes
"55sr " - the hand scanner gets this one wrong and drops the s or interprets as a ctrl+s, I'm about to check the software but it's in a separate location so for now I'm assuming it doesn't work either.
"10/29/2009" - works fine....
I'll be wading through the code and will post if I find the problem, but help is definitely appreciated.
|
|
|
|
|
Found it about a week ago... still testing some, but it looks like it's fixed. It was a problem of figuring out which characterset to switch back to and would switch to A when it was supposed to switch to B, or at least that's what it looked like to me...here's what I did and it seems to work now.
if (CurrCodeSet == CodeSet.CodeC || ((LookAheadAscii != -1) && !CharCompatibleWithCodeset(LookAheadAscii, CurrCodeSet)))
{
switch (CurrCodeSet)
{
case CodeSet.CodeA:
shifter = cCODEB;
CurrCodeSet = CodeSet.CodeB;
break;
case CodeSet.CodeB:
shifter = cCODEA;
CurrCodeSet = CodeSet.CodeA;
break;
case CodeSet.CodeC:
{
CodeSet cs = GetBestCodeSetAB(CharAscii, LookAheadAscii);
switch (cs)
{
case CodeSet.CodeA:
shifter = cCODEA;
CurrCodeSet = CodeSet.CodeA;
break;
case CodeSet.CodeB:
shifter = cCODEB;
CurrCodeSet = CodeSet.CodeB;
break;
}
break;
}
}
}
|
|
|
|
|
I've made more modifications to the code. I've also included some "smart" code to know when not to switch to codeC. The noticeable case is a barcode like: "A44A" or "A444" You save a bar by not having to switch twice.
using System;
using System.Collections.Generic;
using System.Text;
namespace GenCode128
{
public enum CodeSet
{
CodeA, CodeB, CodeC
}
/// <summary>
/// Represent the set of code values to be output into barcode form
/// </summary>
public class Code128Content
{
private readonly int[] mCodeList;
/// <summary>
/// Create content based on a string of ASCII data
/// </summary>
/// <param name="AsciiData">the string that should be represented</param>
public Code128Content(string AsciiData)
{
mCodeList = StringToCode128(AsciiData);
}
/// <summary>
/// Provides the Code128 code values representing the object's string
/// </summary>
public int[] Codes
{
get { return mCodeList; }
}
/// <summary>
/// Transform the string into integers representing the Code128 codes
/// necessary to represent it
/// </summary>
/// <param name="AsciiData">String to be encoded</param>
/// <returns>Code128 representation</returns>
private static int[] StringToCode128(string AsciiData)
{
// turn the string into ascii byte data
byte[] asciiBytes = Encoding.ASCII.GetBytes(AsciiData);
// decide which codeset to start with
CodeSet currcs = Code128Code.GetBestCodeSet(asciiBytes, 0);
// set up the beginning of the barcode
List<int> codes = new List<int>();
codes.Add(Code128Code.StartCodeForCodeSet(currcs));
// add the codes for each character in the string
for (int i = 0; i < asciiBytes.Length; i++)
{
codes.AddRange(Code128Code.CodesForChar(asciiBytes, i, ref currcs));
//Jump 2 bytes if Code C is active
if (currcs == CodeSet.CodeC)
i++;
}
// calculate the check digit
int checksum = codes[0];
for (int i = 1; i < codes.Count; i++)
{
checksum += i * codes[i];
}
codes.Add(checksum % 103);
codes.Add(Code128Code.StopCode());
int[] result = codes.ToArray();
return result;
}
}
/// <summary>
/// Static tools for determining codes for individual characters in the content
/// </summary>
public static class Code128Code
{
#region Constants
private const int cSHIFT = 98;
private const int cCODEA = 101;
private const int cCODEB = 100;
private const int cCODEC = 99;
private const int cSTARTA = 103;
private const int cSTARTB = 104;
private const int cSTARTC = 105;
private const int cSTOP = 106;
#endregion
/// <summary>
/// Determines the best starting code set based on the the first two
/// characters of the string to be encoded
/// </summary>
/// <param name="csa1">First character of input string</param>
/// <param name="csa2">Second character of input string</param>
/// <returns>The codeset determined to be best to start with</returns>
private static CodeSet GetBestStartSet(Code128Code.CodeSetAllowed csa1, Code128Code.CodeSetAllowed csa2)
{
int vote = 0;
vote += (csa1 == Code128Code.CodeSetAllowed.CodeA) ? 1 : 0;
vote += (csa1 == Code128Code.CodeSetAllowed.CodeB) ? -1 : 0;
vote += (csa2 == Code128Code.CodeSetAllowed.CodeA) ? 1 : 0;
vote += (csa2 == Code128Code.CodeSetAllowed.CodeB) ? -1 : 0;
return (vote > 0) ? CodeSet.CodeA : CodeSet.CodeB; // ties go to codeB due to my own prejudices
}
/// <summary>
/// Determines the best starting code set based on the the char1 and char2
/// </summary>
/// <returns>The codeset determined to be best to start with</returns>
public static CodeSet GetBestCodeSet(byte[] asciiBytes, int position)
{
int char1 = asciiBytes.Length > position ? asciiBytes[position] : -1;
int char2 = asciiBytes.Length > position+1 ? asciiBytes[position + 1] : -1;
// check for Code C: If first 2 bytes are numeric, then we use Code C
if (asciiBytes.Length > position + 1)
{
if (Code128Code.IsCodeCAllowedForChar(asciiBytes[position], asciiBytes[position+1]))
return CodeSet.CodeC;
}
Code128Code.CodeSetAllowed csa1 = char1 != -1
? Code128Code.CodesetAllowedForChar(char1)
: Code128Code.CodeSetAllowed.CodeAorB;
Code128Code.CodeSetAllowed csa2 = char2 != -1
? Code128Code.CodesetAllowedForChar(char2)
: Code128Code.CodeSetAllowed.CodeAorB;
return GetBestStartSet(csa1, csa2);
}
/// <summary>
/// Get the Code128 code value(s) to represent an ASCII character, with
/// optional look-ahead for length optimization
/// </summary>
/// <param name="position"></param>
/// <param name="asciiBytes"></param>
/// <param name="CurrCodeSet">The current codeset, that the returned codes need to follow;
/// if the returned codes change that, then this value will be changed to reflect it</param>
/// <returns>An array of integers representing the codes that need to be output to produce the
/// given character</returns>
public static int[] CodesForChar(byte[] asciiBytes, int position, ref CodeSet CurrCodeSet)
{
int CharAscii = asciiBytes[position];
int LookAheadAscii = asciiBytes.Length > (position + 1) ? asciiBytes[position + 1] : -1;
int LookAheadAscii2 = asciiBytes.Length > (position + 2) ? asciiBytes[position + 2] : -1;
int LookAheadAscii3 = asciiBytes.Length > (position + 3) ? asciiBytes[position + 3] : -1;
int[] result;
int shifter = -1;
// We don't need to switch CodeSet if we are using Code C and the next 2 chars are compatible with it
if (CurrCodeSet == CodeSet.CodeC && IsCodeCAllowedForChar(CharAscii, LookAheadAscii))
{
result = new int[1];
result[0] = CodeCValueForChars(CharAscii, LookAheadAscii);
return result;
}
//If our next 2 chars are compatible with Code C, switch to it.
//Actually... Only make the switch if the next 4 characters are compatable. Here's the cases.
// 00 the cost is 2 characters, once to switch to code C, one for the numbers. 2 vs 2
// 000 It's more expensive, because switch to code c, 00, switch to code a, last number. 4 vs 3
// 00a The cost is more expensive, once to switch to code c, one for the numbers, one to go to code a, then the letter. 4 Vs 3
// 0000 switch to code c, first number, second number. 3 vs 4
// 0000a switch to code c, first number, second number, switch to code a, last letter, 4 vs 5
if (IsCodeCAllowedForChar(CharAscii, LookAheadAscii) && IsCodeCAllowedForChar(LookAheadAscii2, LookAheadAscii3))
{
CurrCodeSet = CodeSet.CodeC;
result = new int[2];
result[0] = cCODEC;
result[1] = CodeCValueForChars(CharAscii, LookAheadAscii);
return result;
}
if (!CharCompatibleWithCodeset(CharAscii, LookAheadAscii, CurrCodeSet))
{
// If we are working with Code C or if we have a lookahead character AND if the next character is ALSO not compatible
if (CurrCodeSet == CodeSet.CodeC
|| ((LookAheadAscii != -1) && !CharCompatibleWithCodeset(LookAheadAscii, -1, CurrCodeSet)))
{
// we need to switch code sets
switch (CurrCodeSet)
{
case CodeSet.CodeA:
shifter = cCODEB;
CurrCodeSet = CodeSet.CodeB;
break;
case CodeSet.CodeB:
shifter = cCODEA;
CurrCodeSet = CodeSet.CodeA;
break;
case CodeSet.CodeC:
CodeSet cs = GetBestCodeSet(asciiBytes, position);
switch (cs)
{
case CodeSet.CodeA:
shifter = cCODEA;
CurrCodeSet = CodeSet.CodeA;
break;
case CodeSet.CodeB:
shifter = cCODEB;
CurrCodeSet = CodeSet.CodeB;
break;
case CodeSet.CodeC:
throw new Exception("We don't want to shift to CodeC if it not allowed.");
}
break;
}
}
else
{
// no need to switch code sets, a temporary SHIFT will suffice
shifter = cSHIFT;
}
}
if (shifter != -1)
{
result = new int[2];
result[0] = shifter;
CodeSet shiftedCodeSet;
switch (shifter)
{
case cCODEA:
shiftedCodeSet = CodeSet.CodeA;
break;
case cCODEB:
shiftedCodeSet = CodeSet.CodeB;
break;
case cSHIFT:
switch (CurrCodeSet)
{
case CodeSet.CodeA:
shiftedCodeSet = CodeSet.CodeB;
break;
case CodeSet.CodeB:
shiftedCodeSet = CodeSet.CodeA;
break;
default:
throw new ArgumentOutOfRangeException();
}
break;
default:
throw new ArgumentOutOfRangeException();
}
result[1] = CodeValueForChar(CharAscii, shiftedCodeSet);
}
else
{
result = new int[1];
result[0] = CodeValueForChar(CharAscii, CurrCodeSet);
}
return result;
}
/// <summary>
/// Tells us which codesets a given character value is allowed in
/// </summary>
/// <param name="CharAscii">ASCII value of character to look at</param>
/// <returns>Which codeset(s) can be used to represent this character</returns>
public static CodeSetAllowed CodesetAllowedForChar(int CharAscii)
{
if (CharAscii >= 32 && CharAscii <= 95)
{
return CodeSetAllowed.CodeAorB;
}
return (CharAscii < 32) ? CodeSetAllowed.CodeA : CodeSetAllowed.CodeB;
}
/// <summary>
/// Tells us if Code C is compatible with the 2 parameter bytes
/// </summary>
/// <param name="CharAscii">First char</param>
/// <param name="NextCharAscii">Second char</param>
/// <returns>Which codeset(s) can be used to represent this character</returns>
public static bool IsCodeCAllowedForChar(int CharAscii, int NextCharAscii)
{
if (CharAscii == -1 && NextCharAscii == -1)
return false;
return CharAscii >= 48 && CharAscii <= 57 && NextCharAscii >= 48 && NextCharAscii <= 57;
}
/// <summary>
/// Determine if a character can be represented in a given codeset
/// </summary>
/// <param name="CharAscii">character to check for</param>
/// <param name="NextCharAscii"></param>
/// <param name="currcs">codeset context to test</param>
/// <returns>true if the codeset contains a representation for the ASCII character</returns>
public static bool CharCompatibleWithCodeset(int CharAscii, int NextCharAscii, CodeSet currcs)
{
if (currcs == CodeSet.CodeC)
{
return IsCodeCAllowedForChar(CharAscii, NextCharAscii);
}
CodeSetAllowed csa = CodesetAllowedForChar(CharAscii);
return csa == CodeSetAllowed.CodeAorB
|| (csa == CodeSetAllowed.CodeA && currcs == CodeSet.CodeA)
|| (csa == CodeSetAllowed.CodeB && currcs == CodeSet.CodeB);
}
/// <summary>
/// Gets the integer code128 code value for a character (assuming the appropriate code set)
/// </summary>
/// <param name="CharAscii">character to convert</param>
/// <param name="currcs">The current Codeset</param>
/// <returns>code128 symbol value for the character</returns>
public static int CodeValueForChar(int CharAscii, CodeSet currcs)
{
if (CharCompatibleWithCodeset(CharAscii, -1, currcs) == false)
throw new Exception("Encoding error. For some reason, we're trying to encode ASCII char " + CharAscii + " in codeset " + currcs);
return (CharAscii >= 32) ? CharAscii - 32 : CharAscii + 64;
}
/// <summary>
/// Gets the integer code128 code value for pair character (assuming Code C)
/// </summary>
/// <param name="CharAscii">First character to convert</param>
/// <param name="NextCharAscii">Second character to convert</param>
/// <returns>code128 symbol value for the character</returns>
public static int CodeCValueForChars(int CharAscii, int NextCharAscii)
{
return (CharAscii - 48) * 10 + NextCharAscii - 48;
}
/// <summary>
/// Return the appropriate START code depending on the codeset we want to be in
/// </summary>
/// <param name="cs">The codeset you want to start in</param>
/// <returns>The code128 code to start a barcode in that codeset</returns>
public static int StartCodeForCodeSet(CodeSet cs)
{
switch (cs)
{
case CodeSet.CodeA:
return cSTARTA;
case CodeSet.CodeB:
return cSTARTB;
case CodeSet.CodeC:
return cSTARTC;
}
throw new Exception("Must be a CodeSet specified");
}
/// <summary>
/// Return the Code128 stop code
/// </summary>
/// <returns>the stop code</returns>
public static int StopCode()
{
return cSTOP;
}
/// <summary>
/// Indicates which code sets can represent a character -- CodeA, CodeB, or either
/// </summary>
public enum CodeSetAllowed
{
CodeA, CodeB, CodeAorB
}
}
}
|
|
|
|
|
using System;
using NUnit.Framework;
using GenCode128;
namespace GenCode128Tests
{
/// <summary>
/// Summary description for Content.
/// </summary>
[TestFixture]
public class ContentTests
{
private const int cSHIFT = 98;
private const int cCODEA = 101;
private const int cCODEB = 100;
[Test]
public void CharRangeTests()
{
Assert.AreEqual(Code128Code.CodeSetAllowed.CodeAorB, Code128Code.CodesetAllowedForChar(66), "Incorrect codeset requirement returned");
Assert.AreEqual(Code128Code.CodeSetAllowed.CodeA, Code128Code.CodesetAllowedForChar(17), "Incorrect codeset requirement returned");
Assert.AreEqual(Code128Code.CodeSetAllowed.CodeB, Code128Code.CodesetAllowedForChar(110), "Incorrect codeset requirement returned");
}
[Test]
public void CharTranslationTests()
{
// in CodeA, thischar Either, nextchar Either
byte thischar = 66;
byte nextchar = 66;
CodeSet currcs = CodeSet.CodeA;
CodeSet origcs = currcs;
int[] resultcodes = Code128Code.CodesForChar(new byte[] { thischar, nextchar }, 0, ref currcs);
Assert.IsNotNull(resultcodes, "No codes returned");
Assert.AreEqual(1, resultcodes.Length, "Incorrect number of codes returned");
Assert.AreEqual(34, resultcodes[0], "Incorrect code returned");
Assert.AreEqual(origcs, currcs, "Incorrect code set returned");
// in CodeA, thischar CodeA, nextchar Either
thischar = 1; // "^A"
nextchar = 66;
currcs = CodeSet.CodeA;
origcs = currcs;
resultcodes = Code128Code.CodesForChar(new byte[] { thischar, nextchar }, 0, ref currcs);
Assert.IsNotNull(resultcodes, "No codes returned");
Assert.AreEqual(1, resultcodes.Length, "Incorrect number of codes returned");
Assert.AreEqual(65, resultcodes[0], "Incorrect code returned");
Assert.AreEqual(origcs, currcs, "Incorrect code set returned");
// in CodeA, thischar CodeB, nextchar Either
thischar = 110; // "n"
nextchar = 66;
currcs = CodeSet.CodeA;
origcs = currcs;
resultcodes = Code128Code.CodesForChar(new byte[] { thischar, nextchar }, 0, ref currcs);
Assert.IsNotNull(resultcodes, "No codes returned");
Assert.AreEqual(2, resultcodes.Length, "Incorrect number of codes returned");
Assert.AreEqual(cSHIFT, resultcodes[0], "Incorrect code returned");
Assert.AreEqual(78, resultcodes[1], "Incorrect code returned");
Assert.AreEqual(origcs, currcs, "Incorrect code set returned");
// in CodeA, thischar Either, nextchar -1
thischar = 66; // "B"
currcs = CodeSet.CodeA;
origcs = currcs;
resultcodes = Code128Code.CodesForChar(new byte[] { thischar }, 0, ref currcs);
Assert.IsNotNull(resultcodes, "No codes returned");
Assert.AreEqual(1, resultcodes.Length, "Incorrect number of codes returned");
Assert.AreEqual(34, resultcodes[0], "Incorrect code returned");
Assert.AreEqual(origcs, currcs, "Incorrect code set returned");
// in CodeA, thischar CodeA, nextchar -1
thischar = 1; // "^A"
currcs = CodeSet.CodeA;
origcs = currcs;
resultcodes = Code128Code.CodesForChar(new byte[] { thischar }, 0, ref currcs);
Assert.IsNotNull(resultcodes, "No codes returned");
Assert.AreEqual(1, resultcodes.Length, "Incorrect number of codes returned");
Assert.AreEqual(65, resultcodes[0], "Incorrect code returned");
Assert.AreEqual(origcs, currcs, "Incorrect code set returned");
// in CodeA, thischar CodeB, nextchar -1
thischar = 110; // "n"
currcs = CodeSet.CodeA;
origcs = currcs;
resultcodes = Code128Code.CodesForChar(new byte[] { thischar }, 0, ref currcs);
Assert.IsNotNull(resultcodes, "No codes returned");
Assert.AreEqual(2, resultcodes.Length, "Incorrect number of codes returned");
Assert.AreEqual(cSHIFT, resultcodes[0], "Incorrect code returned");
Assert.AreEqual(78, resultcodes[1], "Incorrect code returned");
Assert.AreEqual(origcs, currcs, "Incorrect code set returned");
// in CodeA, thischar Either, nextchar CodeA
thischar = 66; // "B"
nextchar = 1;
currcs = CodeSet.CodeA;
origcs = currcs;
resultcodes = Code128Code.CodesForChar(new byte[] { thischar, nextchar }, 0, ref currcs);
Assert.IsNotNull(resultcodes, "No codes returned");
Assert.AreEqual(1, resultcodes.Length, "Incorrect number of codes returned");
Assert.AreEqual(34, resultcodes[0], "Incorrect code returned");
Assert.AreEqual(origcs, currcs, "Incorrect code set returned");
// in CodeA, thischar CodeA, nextchar CodeA
thischar = 1; // "^A"
nextchar = 1;
currcs = CodeSet.CodeA;
origcs = currcs;
resultcodes = Code128Code.CodesForChar(new byte[] { thischar, nextchar }, 0, ref currcs);
Assert.IsNotNull(resultcodes, "No codes returned");
Assert.AreEqual(1, resultcodes.Length, "Incorrect number of codes returned");
Assert.AreEqual(65, resultcodes[0], "Incorrect code returned");
Assert.AreEqual(origcs, currcs, "Incorrect code set returned");
// in CodeA, thischar CodeB, nextchar CodeA
thischar = 110; // "n"
nextchar = 1;
currcs = CodeSet.CodeA;
origcs = currcs;
resultcodes = Code128Code.CodesForChar(new byte[] { thischar, nextchar }, 0, ref currcs);
Assert.IsNotNull(resultcodes, "No codes returned");
Assert.AreEqual(2, resultcodes.Length, "Incorrect number of codes returned");
Assert.AreEqual(cSHIFT, resultcodes[0], "Incorrect code returned");
Assert.AreEqual(78, resultcodes[1], "Incorrect code returned");
Assert.AreEqual(origcs, currcs, "Incorrect code set returned");
// in CodeA, thischar Either, nextchar CodeB
thischar = 66; // "B"
nextchar = 110;
currcs = CodeSet.CodeA;
origcs = currcs;
resultcodes = Code128Code.CodesForChar(new byte[] { thischar, nextchar }, 0, ref currcs);
Assert.IsNotNull(resultcodes, "No codes returned");
Assert.AreEqual(1, resultcodes.Length, "Incorrect number of codes returned");
Assert.AreEqual(34, resultcodes[0], "Incorrect code returned");
Assert.AreEqual(origcs, currcs, "Incorrect code set returned");
// in CodeA, thischar CodeA, nextchar CodeB
thischar = 1; // "^A"
nextchar = 110;
currcs = CodeSet.CodeA;
origcs = currcs;
resultcodes = Code128Code.CodesForChar(new byte[] { thischar, nextchar }, 0, ref currcs);
Assert.IsNotNull(resultcodes, "No codes returned");
Assert.AreEqual(1, resultcodes.Length, "Incorrect number of codes returned");
Assert.AreEqual(65, resultcodes[0], "Incorrect code returned");
Assert.AreEqual(origcs, currcs, "Incorrect code set returned");
// in CodeA, thischar CodeB, nextchar CodeB
thischar = 110; // "n"
nextchar = 110;
currcs = CodeSet.CodeA;
origcs = currcs;
resultcodes = Code128Code.CodesForChar(new byte[] { thischar, nextchar }, 0, ref currcs);
Assert.IsNotNull(resultcodes, "No codes returned");
Assert.AreEqual(2, resultcodes.Length, "Incorrect number of codes returned");
Assert.AreEqual(cCODEB, resultcodes[0], "Incorrect code returned");
Assert.AreEqual(78, resultcodes[1], "Incorrect code returned");
Assert.AreNotEqual(origcs, currcs, "Incorrect code set returned");
// in CodeB, thischar Either, nextchar Either
thischar = 66; // "B"
nextchar = 66;
currcs = CodeSet.CodeB;
origcs = currcs;
resultcodes = Code128Code.CodesForChar(new byte[] { thischar, nextchar }, 0, ref currcs);
Assert.IsNotNull(resultcodes, "No codes returned");
Assert.AreEqual(1, resultcodes.Length, "Incorrect number of codes returned");
Assert.AreEqual(34, resultcodes[0], "Incorrect code returned");
Assert.AreEqual(origcs, currcs, "Incorrect code set returned");
// in CodeB, thischar CodeA, nextchar Either
thischar = 1; // "^A"
nextchar = 66;
currcs = CodeSet.CodeB;
origcs = currcs;
resultcodes = Code128Code.CodesForChar(new byte[] { thischar, nextchar }, 0, ref currcs);
Assert.IsNotNull(resultcodes, "No codes returned");
Assert.AreEqual(2, resultcodes.Length, "Incorrect number of codes returned");
Assert.AreEqual(cSHIFT, resultcodes[0], "Incorrect code returned");
Assert.AreEqual(65, resultcodes[1], "Incorrect code returned");
Assert.AreEqual(origcs, currcs, "Incorrect code set returned");
// in CodeB, thischar CodeB, nextchar Either
thischar = 110; // "n"
nextchar = 66;
currcs = CodeSet.CodeB;
origcs = currcs;
resultcodes = Code128Code.CodesForChar(new byte[] { thischar, nextchar }, 0, ref currcs);
Assert.IsNotNull(resultcodes, "No codes returned");
Assert.AreEqual(1, resultcodes.Length, "Incorrect number of codes returned");
Assert.AreEqual(78, resultcodes[0], "Incorrect code returned");
Assert.AreEqual(origcs, currcs, "Incorrect code set returned");
// in CodeB, thischar Either, nextchar -1
thischar = 66; // "B"
currcs = CodeSet.CodeB;
origcs = currcs;
resultcodes = Code128Code.CodesForChar(new byte[] { thischar }, 0, ref currcs);
Assert.IsNotNull(resultcodes, "No codes returned");
Assert.AreEqual(1, resultcodes.Length, "Incorrect number of codes returned");
Assert.AreEqual(34, resultcodes[0], "Incorrect code returned");
Assert.AreEqual(origcs, currcs, "Incorrect code set returned");
// in CodeB, thischar CodeA, nextchar -1
thischar = 1; // "^A"
currcs = CodeSet.CodeB;
origcs = currcs;
resultcodes = Code128Code.CodesForChar(new byte[] { thischar, nextchar }, 0, ref currcs);
Assert.IsNotNull(resultcodes, "No codes returned");
Assert.AreEqual(2, resultcodes.Length, "Incorrect number of codes returned");
Assert.AreEqual(cSHIFT, resultcodes[0], "Incorrect code returned");
Assert.AreEqual(65, resultcodes[1], "Incorrect code returned");
Assert.AreEqual(origcs, currcs, "Incorrect code set returned");
// in CodeB, thischar CodeB, nextchar -1
thischar = 110; // "n"
currcs = CodeSet.CodeB;
origcs = currcs;
resultcodes = Code128Code.CodesForChar(new byte[] { thischar }, 0, ref currcs);
Assert.IsNotNull(resultcodes, "No codes returned");
Assert.AreEqual(1, resultcodes.Length, "Incorrect number of codes returned");
Assert.AreEqual(78, resultcodes[0], "Incorrect code returned");
Assert.AreEqual(origcs, currcs, "Incorrect code set returned");
// in CodeB, thischar Either, nextchar CodeA
thischar = 66; // "B"
nextchar = 1;
currcs = CodeSet.CodeB;
origcs = currcs;
resultcodes = Code128Code.CodesForChar(new byte[] { thischar, nextchar }, 0, ref currcs);
Assert.IsNotNull(resultcodes, "No codes returned");
Assert.AreEqual(1, resultcodes.Length, "Incorrect number of codes returned");
Assert.AreEqual(34, resultcodes[0], "Incorrect code returned");
Assert.AreEqual(origcs, currcs, "Incorrect code set returned");
// in CodeB, thischar CodeA, nextchar CodeA
thischar = 1; // "^A"
nextchar = 1;
currcs = CodeSet.CodeB;
origcs = currcs;
resultcodes = Code128Code.CodesForChar(new byte[] { thischar, nextchar }, 0, ref currcs);
Assert.IsNotNull(resultcodes, "No codes returned");
Assert.AreEqual(2, resultcodes.Length, "Incorrect number of codes returned");
Assert.AreEqual(cCODEA, resultcodes[0], "Incorrect code returned");
Assert.AreEqual(65, resultcodes[1], "Incorrect code returned");
Assert.AreNotEqual(origcs, currcs, "Incorrect code set returned");
// in CodeB, thischar CodeB, nextchar CodeA
thischar = 110; // "n"
nextchar = 1;
currcs = CodeSet.CodeB;
origcs = currcs;
resultcodes = Code128Code.CodesForChar(new byte[] { thischar, nextchar }, 0, ref currcs);
Assert.IsNotNull(resultcodes, "No codes returned");
Assert.AreEqual(1, resultcodes.Length, "Incorrect number of codes returned");
Assert.AreEqual(78, resultcodes[0], "Incorrect code returned");
Assert.AreEqual(origcs, currcs, "Incorrect code set returned");
// in CodeB, thischar Either, nextchar CodeB
thischar = 66; // "B"
nextchar = 110;
currcs = CodeSet.CodeB;
origcs = currcs;
resultcodes = Code128Code.CodesForChar(new byte[] { thischar, nextchar }, 0, ref currcs);
Assert.IsNotNull(resultcodes, "No codes returned");
Assert.AreEqual(1, resultcodes.Length, "Incorrect number of codes returned");
Assert.AreEqual(34, resultcodes[0], "Incorrect code returned");
Assert.AreEqual(origcs, currcs, "Incorrect code set returned");
// in CodeB, thischar CodeA, nextchar CodeB
thischar = 1; // "^A"
nextchar = 110;
currcs = CodeSet.CodeB;
origcs = currcs;
resultcodes = Code128Code.CodesForChar(new byte[] { thischar, nextchar }, 0, ref currcs);
Assert.IsNotNull(resultcodes, "No codes returned");
Assert.AreEqual(2, resultcodes.Length, "Incorrect number of codes returned");
Assert.AreEqual(cSHIFT, resultcodes[0], "Incorrect code returned");
Assert.AreEqual(65, resultcodes[1], "Incorrect code returned");
Assert.AreEqual(origcs, currcs, "Incorrect code set returned");
// in CodeB, thischar CodeB, nextchar CodeB
thischar = 110; // "n"
nextchar = 110;
currcs = CodeSet.CodeB;
origcs = currcs;
resultcodes = Code128Code.CodesForChar(new byte[] { thischar, nextchar }, 0, ref currcs);
Assert.IsNotNull(resultcodes, "No codes returned");
Assert.AreEqual(1, resultcodes.Length, "Incorrect number of codes returned");
Assert.AreEqual(78, resultcodes[0], "Incorrect code returned");
Assert.AreEqual(origcs, currcs, "Incorrect code set returned");
}
[Test]
public void CharCompatibilityTests()
{
int thischar = 66;
Assert.AreEqual(true, Code128Code.CharCompatibleWithCodeset(thischar, -1, CodeSet.CodeA), "Compat test failed");
thischar = 66; // "B"
Assert.AreEqual(true, Code128Code.CharCompatibleWithCodeset(thischar, -1, CodeSet.CodeB), "Compat test failed");
thischar = 17; // "^Q"
Assert.AreEqual(true, Code128Code.CharCompatibleWithCodeset(thischar, -1, CodeSet.CodeA), "Compat test failed");
thischar = 17; // "^Q"
Assert.AreEqual(false, Code128Code.CharCompatibleWithCodeset(thischar, -1, CodeSet.CodeB), "Compat test failed");
thischar = 110; // "n"
Assert.AreEqual(false, Code128Code.CharCompatibleWithCodeset(thischar, -1, CodeSet.CodeA), "Compat test failed");
thischar = 110; // "n"
Assert.AreEqual(true, Code128Code.CharCompatibleWithCodeset(thischar, -1, CodeSet.CodeB), "Compat test failed");
}
[Test]
public void CharValueTranslationTests()
{
Assert.AreEqual(0, Code128Code.CodeValueForChar(32, CodeSet.CodeA), "Code translation wrong");
Assert.AreEqual(31, Code128Code.CodeValueForChar(63, CodeSet.CodeA), "Code translation wrong");
Assert.AreEqual(32, Code128Code.CodeValueForChar(64, CodeSet.CodeA), "Code translation wrong");
Assert.AreEqual(63, Code128Code.CodeValueForChar(95, CodeSet.CodeA), "Code translation wrong");
Assert.AreEqual(64, Code128Code.CodeValueForChar(96, CodeSet.CodeB), "Code translation wrong");
Assert.AreEqual(64, Code128Code.CodeValueForChar(0, CodeSet.CodeA), "Code translation wrong");
Assert.AreEqual(95, Code128Code.CodeValueForChar(31, CodeSet.CodeA), "Code translation wrong");
}
[Test]
public void FullStringTest()
{
Code128Content content = new Code128Content("BarCode 1");
int[] result = content.Codes;
Assert.AreEqual(12, result.Length, "Wrong number of code values in result");
Assert.AreEqual(104, result[0], "Start code wrong");
Assert.AreEqual(34, result[1], "Code value #1 wrong");
Assert.AreEqual(65, result[2], "Code value #2 wrong");
Assert.AreEqual(82, result[3], "Code value #3 wrong");
Assert.AreEqual(35, result[4], "Code value #4 wrong");
Assert.AreEqual(79, result[5], "Code value #5 wrong");
Assert.AreEqual(68, result[6], "Code value #6 wrong");
Assert.AreEqual(69, result[7], "Code value #7 wrong");
Assert.AreEqual(0, result[8], "Code value #8 wrong");
Assert.AreEqual(17, result[9], "Code value #9 wrong");
Assert.AreEqual(33, result[10], "Checksum wrong");
Assert.AreEqual(106, result[11], "Stop character wrong");
content = new Code128Content("\x11S12345");
result = content.Codes;
Assert.AreEqual(10, result.Length, "Wrong number of code values in result");
}
[Test]
public void KnownBug1()
{
Code128Content content = new Code128Content("55sr ");
int[] result = content.Codes;
Assert.AreEqual(8, result.Length, "Wrong number of code values in result");
Assert.AreEqual(105, result[0], "Start code wrong");
Assert.AreEqual(55, result[1], "Code value #1 wrong");
Assert.AreEqual(100, result[2], "Code value #2 wrong");
Assert.AreEqual(83, result[3], "Code value #3 wrong");
Assert.AreEqual(82, result[4], "Code value #4 wrong");
Assert.AreEqual(0, result[5], "Code value #5 wrong");
Assert.AreEqual(10, result[6], "Checksum wrong");
Assert.AreEqual(106, result[7], "Stop character wrong");
}
[Test]
public void KnownBug2()
{
Code128Content content = new Code128Content("2009 w/");
int[] result = content.Codes;
Assert.AreEqual(9, result.Length, "Wrong number of code values in result");
Assert.AreEqual(105, result[0], "Start code wrong");
Assert.AreEqual(20, result[1], "Code value #1 wrong");
Assert.AreEqual(9, result[2], "Code value #2 wrong");
Assert.AreEqual(100, result[3], "Code value #3 wrong");
Assert.AreEqual(0, result[4], "Code value #4 wrong");
Assert.AreEqual(87, result[5], "Code value #5 wrong");
Assert.AreEqual(15, result[6], "Code value #6 wrong");
Assert.AreEqual(41, result[7], "Checksum wrong");
Assert.AreEqual(106, result[8], "Stop character wrong");
}
[Test]
public void Code3Shorter()
{
Code128Content content = new Code128Content("aa00aa");
int[] result = content.Codes;
Assert.AreEqual(9, result.Length, "Wrong number of code values in result");
Assert.AreEqual(104, result[0], "Start code wrong");
Assert.AreEqual(65, result[1], "Code value #1 wrong");
Assert.AreEqual(65, result[2], "Code value #2 wrong");
Assert.AreEqual(16, result[3], "Code value #3 wrong");
Assert.AreEqual(16, result[4], "Code value #4 wrong");
Assert.AreEqual(65, result[5], "Code value #5 wrong");
Assert.AreEqual(65, result[6], "Code value #6 wrong");
Assert.AreEqual(96, result[7], "Checksum wrong");
Assert.AreEqual(106, result[8], "Stop character wrong");
}
[Test]
public void Code3Shorter2()
{
Code128Content content = new Code128Content("aa0000aa");
int[] result = content.Codes;
Assert.AreEqual(11, result.Length, "Wrong number of code values in result");
Assert.AreEqual(104, result[0], "Start code wrong");
Assert.AreEqual(65, result[1], "Code value #1 wrong");
Assert.AreEqual(65, result[2], "Code value #2 wrong");
Assert.AreEqual(99, result[3], "Code value #3 wrong");
Assert.AreEqual(0, result[4], "Code value #4 wrong");
Assert.AreEqual(0, result[5], "Code value #5 wrong");
Assert.AreEqual(100, result[6], "Code value #6 wrong");
Assert.AreEqual(65, result[7], "Code value #6 wrong");
Assert.AreEqual(65, result[8], "Code value #6 wrong");
Assert.AreEqual(8, result[9], "Checksum wrong");
Assert.AreEqual(106, result[10], "Stop character wrong");
}
[Test]
[ExpectedException(typeof(Exception))]
public void ShouldntBeAbleToDoAnInvalidEncode()
{
//Code A doesn't support lower case... this should throw some form of an exception.
GenCode128.Code128Code.CodeValueForChar(115, CodeSet.CodeA);
}
[Test]
[ExpectedException(typeof(Exception))]
public void ShouldntBeAbleToDoAnInvalidEncode2()
{
//Code B doesn't support non printing characters... this should throw some form of an exception.
GenCode128.Code128Code.CodeValueForChar(16, CodeSet.CodeB);
}
}
}
|
|
|
|
|