Advertisement
Guest User

C# Barcode Habrahabr

a guest
Sep 6th, 2011
3,545
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 14.77 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Collections;
  6.  
  7. namespace WindowsFormsApplication6
  8. {
  9.     class Code128B
  10.     {
  11.         public string input;
  12.         public string output;
  13.         List<string> lst;
  14.         Hashtable htb;
  15.         public void ComputeSum()
  16.         {
  17.             int sum = 104;
  18.             for (int i = 0; i < input.Length; i++)
  19.             {
  20.                 int pos = i + 1;
  21.                 int c = lst.IndexOf(input[i].ToString());
  22.                 int res = c * pos;
  23.                 sum += res;
  24.             }
  25.             sum = sum % 103;
  26.             string newe="";
  27.             for(int i=0;i<input.Length;i++) newe+=String.Format("[{0}",input[i]);
  28.             input=newe;
  29.             input += "[";
  30.             input += lst[sum];
  31.         }
  32.         public Code128B(string inp)
  33.         {
  34.             input = inp;
  35.             htb = new Hashtable();
  36.             htb.Add(" ", "11011001100");
  37.             htb.Add("!", "11001101100");
  38.             htb.Add("\"", "11001100110");
  39.             htb.Add("#", "10010011000");
  40.             htb.Add("$", "10010001100");
  41.             htb.Add("%", "10001001100");
  42.             htb.Add("&", "10011001000");
  43.             htb.Add("'", "10011000100");
  44.             htb.Add("(", "10001100100");
  45.             htb.Add(")", "11001001000");
  46.             htb.Add("*", "11001000100");
  47.             htb.Add("+", "11000100100");
  48.             htb.Add(",", "10110011100");
  49.             htb.Add("-", "10011011100");
  50.             htb.Add(".", "10011001110");
  51.             htb.Add("/", "10111001100");
  52.             htb.Add("0", "10011101100");
  53.             htb.Add("1", "10011100110");
  54.             htb.Add("2", "11001110010");
  55.             htb.Add("3", "11001011100");
  56.             htb.Add("4", "11001001110");
  57.             htb.Add("5", "11011100100");
  58.             htb.Add("6", "11001110100");
  59.             htb.Add("7", "11101101110");
  60.             htb.Add("8", "11101001100");
  61.             htb.Add("9", "11100101100");
  62.             htb.Add(":", "11100100110");
  63.             htb.Add(";", "11101100100");
  64.             htb.Add("<", "11100110100");
  65.             htb.Add("=", "11100110010");
  66.             htb.Add(">", "11011011000");
  67.             htb.Add("?", "11011000110");
  68.             htb.Add("@", "11000110110");
  69.             htb.Add("A", "10100011000");
  70.             htb.Add("B", "10001011000");
  71.             htb.Add("C", "10001000110");
  72.             htb.Add("D", "10110001000");
  73.             htb.Add("E", "10001101000");
  74.             htb.Add("F", "10001100010");
  75.             htb.Add("G", "11010001000");
  76.             htb.Add("H", "11000101000");
  77.             htb.Add("I", "11000100010");
  78.             htb.Add("J", "10110111000");
  79.             htb.Add("K", "10110001110");
  80.             htb.Add("L", "10001101110");
  81.             htb.Add("M", "10111011000");
  82.             htb.Add("N", "10111000110");
  83.             htb.Add("O", "10001110110");
  84.             htb.Add("P", "11101110110");
  85.             htb.Add("Q", "11010001110");
  86.             htb.Add("R", "11000101110");
  87.             htb.Add("S", "11011101000");
  88.             htb.Add("T", "11011100010");
  89.             htb.Add("U", "11011101110");
  90.             htb.Add("V", "11101011000");
  91.             htb.Add("W", "11101000110");
  92.             htb.Add("X", "11100010110");
  93.             htb.Add("Y", "11101101000");
  94.             htb.Add("Z", "11101100010");
  95.             htb.Add("[", "11100011010");
  96.             htb.Add("\\", "11101111010");
  97.             htb.Add("]", "11001000010");
  98.             htb.Add("^", "11110001010");
  99.             htb.Add("_", "10100110000");
  100.             htb.Add("`", "10100001100");
  101.             htb.Add("a", "10010110000");
  102.             htb.Add("b", "10010000110");
  103.             htb.Add("c", "10000101100");
  104.             htb.Add("d", "10000100110");
  105.             htb.Add("e", "10110010000");
  106.             htb.Add("f", "10110000100");
  107.             htb.Add("g", "10011010000");
  108.             htb.Add("h", "10011000010");
  109.             htb.Add("i", "10000110100");
  110.             htb.Add("j", "10000110010");
  111.             htb.Add("k", "11000010010");
  112.             htb.Add("l", "11001010000");
  113.             htb.Add("m", "11110111010");
  114.             htb.Add("n", "11000010100");
  115.             htb.Add("o", "10001111010");
  116.             htb.Add("p", "10100111100");
  117.             htb.Add("q", "10010111100");
  118.             htb.Add("r", "10010011110");
  119.             htb.Add("s", "10111100100");
  120.             htb.Add("t", "10011110100");
  121.             htb.Add("u", "10011110010");
  122.             htb.Add("v", "11110100100");
  123.             htb.Add("w", "11110010100");
  124.             htb.Add("x", "11110010010");
  125.             htb.Add("y", "11011011110");
  126.             htb.Add("z", "11011110110");
  127.             htb.Add("{", "11110110110");
  128.             htb.Add("|", "10101111000");
  129.             htb.Add("}", "10100011110");
  130.             htb.Add("~", "10001011110");
  131.             htb.Add("DEL", "10111101000");
  132.             htb.Add("FNC3", "10111100010");
  133.             htb.Add("FNC2", "11110101000");
  134.             htb.Add("SHIFT", "11110100010");
  135.             htb.Add("CODEC", "10111011110");
  136.             htb.Add("FNC4", "10111101110");
  137.             htb.Add("CODEA", "11101011110");
  138.             htb.Add("FNC1", "11110101110");
  139.             htb.Add("Start A", "11010000100");
  140.             htb.Add("Start B", "11010010000");
  141.             htb.Add("Start C", "11010011100");
  142.             htb.Add("Stop", "1100011101011");
  143.              lst = new List<string>();
  144.             lst.Add(" ");
  145.             lst.Add("!");
  146.             lst.Add("\"");
  147.             lst.Add("#");
  148.             lst.Add("$");
  149.             lst.Add("%");
  150.             lst.Add("&");
  151.             lst.Add("'");
  152.             lst.Add("(");
  153.             lst.Add(")");
  154.             lst.Add("*");
  155.             lst.Add("+");
  156.             lst.Add(",");
  157.             lst.Add("-");
  158.             lst.Add(".");
  159.             lst.Add("/");
  160.             lst.Add("0");
  161.             lst.Add("1");
  162.             lst.Add("2");
  163.             lst.Add("3");
  164.             lst.Add("4");
  165.             lst.Add("5");
  166.             lst.Add("6");
  167.             lst.Add("7");
  168.             lst.Add("8");
  169.             lst.Add("9");
  170.             lst.Add(":");
  171.             lst.Add(";");
  172.             lst.Add("<");
  173.             lst.Add("=");
  174.             lst.Add(">");
  175.             lst.Add("?");
  176.             lst.Add("@");
  177.             lst.Add("A");
  178.             lst.Add("B");
  179.             lst.Add("C");
  180.             lst.Add("D");
  181.             lst.Add("E");
  182.             lst.Add("F");
  183.             lst.Add("G");
  184.             lst.Add("H");
  185.             lst.Add("I");
  186.             lst.Add("J");
  187.             lst.Add("K");
  188.             lst.Add("L");
  189.             lst.Add("M");
  190.             lst.Add("N");
  191.             lst.Add("O");
  192.             lst.Add("P");
  193.             lst.Add("Q");
  194.             lst.Add("R");
  195.             lst.Add("S");
  196.             lst.Add("T");
  197.             lst.Add("U");
  198.             lst.Add("V");
  199.             lst.Add("W");
  200.             lst.Add("X");
  201.             lst.Add("Y");
  202.             lst.Add("Z");
  203.             lst.Add("[");
  204.             lst.Add("\\");
  205.             lst.Add("]");
  206.             lst.Add("^");
  207.             lst.Add("_");
  208.             lst.Add("`");
  209.             lst.Add("a");
  210.             lst.Add("b");
  211.             lst.Add("c");
  212.             lst.Add("d");
  213.             lst.Add("e");
  214.             lst.Add("f");
  215.             lst.Add("g");
  216.             lst.Add("h");
  217.             lst.Add("i");
  218.             lst.Add("j");
  219.             lst.Add("k");
  220.             lst.Add("l");
  221.             lst.Add("m");
  222.             lst.Add("n");
  223.             lst.Add("o");
  224.             lst.Add("p");
  225.             lst.Add("q");
  226.             lst.Add("r");
  227.             lst.Add("s");
  228.             lst.Add("t");
  229.             lst.Add("u");
  230.             lst.Add("v");
  231.             lst.Add("w");
  232.             lst.Add("x");
  233.             lst.Add("y");
  234.             lst.Add("z");
  235.             lst.Add("{");
  236.             lst.Add("|");
  237.             lst.Add("}");
  238.             lst.Add("~");
  239.             lst.Add("DEL");
  240.             lst.Add("FNC 3");
  241.             lst.Add("FNC 2");
  242.             lst.Add("SHIFT");
  243.             lst.Add("CODE C");
  244.             lst.Add("FNC 4");
  245.             lst.Add("CODE A");
  246.             lst.Add("FNC 1");
  247.             lst.Add("Start A");
  248.             lst.Add("Start B");
  249.             lst.Add("Start C");
  250.             lst.Add("Stop");
  251.             ComputeSum();
  252.             input = string.Format("[Start B{0}", input);
  253.             input += "[Stop";
  254.             string[] arr=input.Split('[');
  255.             output += "00000";
  256.             for (int i = 0; i < arr.Length; i++) output += htb[arr[i]];
  257.  
  258.         }
  259.  
  260.     }
  261. }
  262.  
  263. using System;
  264. using System.Collections.Generic;
  265. using System.Linq;
  266. using System.Text;
  267.  
  268. namespace WindowsFormsApplication6
  269. {
  270.     class EAN13
  271.     {
  272.         public string input;
  273.         public string output;
  274.         private int CheckSum()
  275.         {
  276.             string code = input;
  277.             int x = 0;
  278.             int y = 0;
  279.             for (int i = 0; i < 12; i++)
  280.             {
  281.                 int pos = i + 1;
  282.                 if (pos % 2 == 0) y += Convert.ToInt32(input[i])-48; else x +=Convert.ToInt32(input[i])-48;
  283.             }
  284.             int z = x + 3 * y;
  285.             int zz = z;
  286.             while (z % 10 != 0) z++;
  287.             int checksum = z - zz;
  288.             return checksum;
  289.         }
  290.         public EAN13(string inp)
  291.         {
  292.             input = inp;
  293.             if (input.Length != 13)
  294.             {
  295.                 input=input.Insert(12, Convert.ToString(CheckSum()));
  296.             }
  297.            
  298.             string temp = "";
  299.             for (int i = 12; i >= 0; i--) temp += input[i];
  300.             //input = temp;
  301.             string[] first_table = { "0001101", "0011001", "0010011", "0111101", "0100011", "0110001", "0101111", "0111011", "0110111", "0001011" };
  302.             string[] second_table = { "0100111", "0110011", "0011011", "0100001", "0011101", "0111001", "0000101", "0010001", "0001001", "0010111" };
  303.             string[] third_table = { "1110010", "1100110", "1101100", "1000010", "1011100", "1001110", "1010000", "1000100", "1001000", "1110100" };
  304.             int num = input[0]-48;
  305.             output += "101";
  306.             switch (num)
  307.             {
  308.                 case 0:
  309.                     {
  310.                         for (int i = 1; i < 7; i++) output += first_table[input[i]-48];
  311.                         break;
  312.                     }
  313.                 case 1:
  314.                     {
  315.                         output += first_table[input[1]-48];
  316.                         output += first_table[input[2]-48];//AABABB
  317.                         output += second_table[input[3]-48];
  318.                         output += first_table[input[4]-48];
  319.                         output += second_table[input[5]-48];
  320.                         output += second_table[input[6]-48];
  321.                        
  322.                         break;
  323.                     }
  324.                 case 2://AABBAB
  325.                     {
  326.                         output += first_table[input[1]-48];
  327.                         output += first_table[input[2]-48];
  328.                         output += second_table[input[3]-48];
  329.                         output += second_table[input[4]-48];
  330.                         output += first_table[input[5]-48];
  331.                         output += second_table[input[6]-48];
  332.                         break;
  333.                     }
  334.                 case 3://AABBBA
  335.                     {
  336.                         output += first_table[input[1]-48];
  337.                         output += first_table[input[2]-48];
  338.                         output += second_table[input[3]-48];
  339.                         output += second_table[input[4]-48];
  340.                         output += second_table[input[5]-48];
  341.                         output += first_table[input[6]-48];
  342.                         break;
  343.                     }
  344.                 case 4://ABAABB
  345.                     {
  346.                         output += first_table[input[1]-48];
  347.                         output += second_table[input[2]-48];
  348.                         output += first_table[input[3]-48];
  349.                         output += first_table[input[4]-48];
  350.                         output += second_table[input[5]-48];
  351.                         output += second_table[input[6]-48];
  352.                         break;
  353.                     }
  354.                 case 5://ABBAAB
  355.                     {
  356.                         output += first_table[input[1]-48];
  357.                         output += second_table[input[2]-48];
  358.                         output += second_table[input[3]-48];
  359.                         output += first_table[input[4]-48];
  360.                         output += first_table[input[5]-48];
  361.                         output += second_table[input[6]-48];
  362.                         break;
  363.                     }
  364.                 case 6://ABBBAA
  365.                     {
  366.                         output += first_table[input[1]-48];
  367.                         output += second_table[input[2]-48];
  368.                         output += second_table[input[3]-48];
  369.                         output += second_table[input[4]-48];
  370.                         output += first_table[input[5]-48];
  371.                         output += first_table[input[6]-48];
  372.                         break;
  373.                     }
  374.                 case 7://ABABAB
  375.                     {
  376.                         output += first_table[input[1]-48];
  377.                         output += second_table[input[2]-48];
  378.                         output += first_table[input[3]-48];
  379.                         output += second_table[input[4]-48];
  380.                         output += first_table[input[5]-48];
  381.                         output += second_table[input[6]-48];
  382.  
  383.                         break;
  384.                     }
  385.                 case 8://ABABBA
  386.                     {
  387.                         output += first_table[input[1]-48];
  388.                         output += second_table[input[2]-48];
  389.                         output += first_table[input[3]-48];
  390.                         output += second_table[input[4]-48];
  391.                         output += second_table[input[5]-48];
  392.                         output += first_table[input[6]-48];
  393.  
  394.                         break;
  395.                     }
  396.                 case 9://ABBABA
  397.                     {
  398.                         output += first_table[input[1]-48];
  399.                         output += second_table[input[2]-48];
  400.                         output += second_table[input[3]-48];
  401.                         output += first_table[input[4]-48];
  402.                         output += second_table[input[5]-48];
  403.                         output += first_table[input[6]-48];
  404.                         break;
  405.                     }
  406.             }
  407.             output += "01010";
  408.             for (int i = 7; i < 13; i++) output += third_table[input[i]-48];
  409.             output += "101";
  410.         }
  411.     }
  412. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement