这段代码是用于生成Code 128条形码的, 该javascript没有问题, 但我转换成C#代码后,却不行。大家帮看看错在哪?
<html>
<head>
<script
language="javascript">
function getBarcode(rawData) { 
var offset = 32; 
var highAscii = 18; 
var newCodeString = new Array(rawData.length + 3); 
newCodeString[0] = offset + highAscii + 104; 
var total = 104; 
for(var stringCounter = 0 ; stringCounter < rawData.length ; stringCounter++){ 
var character = rawData.substr(stringCounter, 1); 
var ASCIIValue = character.charCodeAt(0); 
var checkDigit = ((ASCIIValue - offset) * (stringCounter + 1)); 
total += checkDigit; 
newCodeString[stringCounter + 1] = ASCIIValue; 

var check = total % 103; 
var holder = 0; 
if(check + offset >= 127) { 
holder = check + offset + highAscii; 
} else {
holder = check + offset; 

newCodeString[newCodeString.length - 2] = holder; 
holder = 106 + offset + highAscii;
newCodeString[newCodeString.length - 1] = holder; 
for(var rCounter = 0 ; rCounter < newCodeString.length ; rCounter++) {
if(newCodeString[rCounter] == 32) { newCodeString[rCounter] = 128; } 
}
return getBarcodeText(newCodeString);
}function getBarcodeText(codeString) { 
var returnVal = "<p align=\"center\"style=\"font-size:16.0pt;mso-bidi-font-size:16.0pt;font-family:Code128bWinLarge\">";
for(var counter = 0 ; counter < codeString.length ; counter++) {
var intValue = codeString[counter]; 
returnVal += "&#" + parseInt(intValue); 
} returnVal += "</P>"; 
return returnVal;
}
function showbarcode(){
var obj = document.getElementById('test');
obj.innerHTML = getBarcode('IamABoy');
}
</script> 
</head>
<body onLoad="javascript:showbarcode();">
<span id="test"></span>
</body>
</html>
我转换后的C#代码:
using System;
using System.Collections.Generic;
using System.Text;
namespace Barcode_Printer
{
    class GetBarcode
    {        
        public  static string  BuildBarcode(string rawData)
        {
            int offset = 32;
            int highAssii = 18;
            int[] newCodeString = new int[rawData.Length + 3];
            newCodeString[0] = offset + highAssii + 104;
            int total = 104;
            for (int i = 0; i < rawData.Length; i++)
            {
                char character = rawData.Substring(i, 1)[0];
                int ASCIIValue = (int)character;
                int checkDigit = (ASCIIValue - offset) * (i + 1);
                total += checkDigit;
                newCodeString[i + 1] = ASCIIValue;
            }
            int check = total % 103;
            int holder = 0;
            if ((check + offset) >= 127)
            {
                holder = check + offset + highAssii;
            }
            else
            {
                holder = check + offset;
            }
            newCodeString[newCodeString.Length - 2] = holder;
            holder = 106 + offset + highAssii;
            newCodeString[newCodeString.Length - 1] = holder;
            for (int j = 0; j < newCodeString.Length; j++)
            {
                if (newCodeString[j] == 32)
                {
                    newCodeString[j] = 128;
                }
            }            StringBuilder sb = new StringBuilder();
            foreach (int i in newCodeString)
            {
                sb.Append((char)i);
            }            return sb.ToString();
        }
    }
}

解决方案 »

  1.   

    1:char character = rawData.Substring(i, 1)[0]; 改成
    char character = rawData.Substring(i, 1); 
      

  2.   

    错了,应该是改成
                    char character = rawData.ToCharArray()[i];
      

  3.   


            static void Main(string[] args)
            {
                showbarcode();
            }        private static string getBarcode(string rawData)
            {
                int offset = 32;
                int highAscii = 18;
                int[] newCodeString = new int[rawData.Length + 3];
                newCodeString[0] = offset + highAscii + 104;
                int total = 104;
                for (int stringCounter = 0; stringCounter < rawData.Length; stringCounter++)
                {
                    string character = rawData.Substring(stringCounter, 1);
                    char ASCIIValue = character[0];
                    int checkDigit = ((ASCIIValue - offset) * (stringCounter + 1));
                    total += checkDigit;
                    newCodeString[stringCounter + 1] = ASCIIValue; 
                }
                int check = total % 103;
                int holder = 0;
                if (check + offset >= 127)
                {
                    holder = check + offset + highAscii;
                }
                else
                {
                    holder = check + offset;
                }
                newCodeString[newCodeString.Length - 2] = holder;
                holder = 106 + offset + highAscii;
                newCodeString[newCodeString.Length - 1] = holder;
                for (var rCounter = 0; rCounter < newCodeString.Length; rCounter++)
                {
                    if (newCodeString[rCounter] == 32) { newCodeString[rCounter] = 128; }
                }
                return getBarcodeText(newCodeString); 
            }        private static string getBarcodeText(int[] codeString)
            {
                StringBuilder sb = new StringBuilder();
                for (int counter = 0; counter < codeString.Length; counter++)
                {
                    sb.AppendFormat("&#{0}", codeString[counter]); 
                }
                return sb.ToString(); 
            }
            private static void showbarcode()
            {
                Console.WriteLine(getBarcode("IamABoy")); 
            }
      

  4.   

    TO: ojlovecd sb.AppendFormat("&#{0}", codeString[counter]); 这个好像不行哦.
     
      

  5.   


    最后条码的内容是&#154&#655....
    条码很长, 而且也扫不出来....