Thursday, June 19, 2008

Code 128 font (barcode) code in C#

I have problem using code128 barcode font.
When I use character 154 (& #154) in html it will be š but in C# its wrong.
š value will be 353 in C#.
So I make table for the character.
here's the codes:



readonly static string code128AsciiTable = "€!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~‘’“”•–—˜™š›œ";
private char Code128ToAscii(char c)
{
char res = (char)code128AsciiTable[(int)c];
return res;
}
private char AsciiToCode128(char c)
{
char res = (char)code128AsciiTable.IndexOf(c);
return res;
}
private char Code128CheckSum( string str, char startChar )
{
int result = 0;
for (int i = 0; i < str.Length; ++i)
{
result += (int)str[i] * (i + 1);
}
result += (int)startChar;
result %= 103;
result = Code128ToAscii((char)result);
return (char)result;
}
public string ConvertToCode128Format(string str)
{
char startChar = code128AsciiTable[104];
char stopChar = code128AsciiTable[106];
string convertedStr = "";
for (int i = 0; i < str.Length; ++i)
{
convertedStr += AsciiToCode128(str[i]);
}
char checkSum = Code128CheckSum(convertedStr, (char)104);
string FullConvert = startChar + str + checkSum + stopChar;
return FullConvert;
}
public void Test()
{
string testString = "156461003";
string code128String = ConvertToCode128Format(testString);
}


3 comments:

Anonymous said...

hi, we, barcodelib, are interested to place our barcode text ads on your page. interested, please email to support@barcodelib.com, richard

Anonymous said...

I was looking for a way to convert the higher valued chars to string but got stuck. Starting with a string containing al posibilities was the trick i was looking for.

Thanx

David said...

http://www.keepautomation.com/font_tools/quickbooks_barcode_font_encoder.html