Panda也疯狂
2017-02-15 07:27
采纳率: 100%
浏览 983

一段JAVA代码转C#代码问题

JAVA代码:

 public static String getCode128h(String strnum)
    {
            byte[] bs = {0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39};
            int length = strnum.length();
            byte[] b = new byte[18+length];
            b[0] = 0x1b;
            b[1] = 0x64;
            b[2] = 0x02;
            b[3] = 0x1d;
            b[4] = 0x48;
            b[5] = 0x32;
            b[6] = 0x1d;
            b[7] = 0x68;
            b[8] = 0x50;
            b[9] = 0x1d;
            b[10] = 0x77;
            b[11] = 0x02;
            b[12] = 0x1d;
            b[13] = 0x6b;
            b[14] = 0x49;//code128
            b[15] = (byte)(length+2);
            b[16] = 0x7b;
            b[17] = 0x42;
            for (int i = 0; i < length; i++){
                String s = String.valueOf(strnum.charAt(i));
                int index = Integer.parseInt(s);
                b[18 + i] = bs[index];
            }
            String strcode = null;
        try {
            strcode = new String(b,"ISO-8859-1");
        } catch (Exception e) {
            e.printStackTrace();
        }
            return strcode;
    }

转C#代码我是这样写的,但拼接到参数里就是乱码,导致请求失败- -、哪位大大能够帮忙看看是不是我的C#代码哪写错了?

 public String getCode128h(String strnum)
        {
            byte[] bs = { 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39 };
            int length = strnum.Length;
            byte[] b = new byte[18 + length];
            b[0] = 0x1b;
            b[1] = 0x64;
            b[2] = 0x02;
            b[3] = 0x1d;
            b[4] = 0x48;
            b[5] = 0x32;
            b[6] = 0x1d;
            b[7] = 0x68;
            b[8] = 0x50;
            b[9] = 0x1d;
            b[10] = 0x77;
            b[11] = 0x02;
            b[12] = 0x1d;
            b[13] = 0x6b;
            b[14] = 0x49;//code128
            b[15] = (byte)(length + 2);
            b[16] = 0x7b;
            b[17] = 0x42;
            for (int i = 0; i < length; i++)
            {
                string s = (CharAt(strnum,i)).ToString();
                int index = Convert.ToInt32(s); 
                b[18 + i] = bs[index];
            }
            String strcode = null;

            try
            {
                            m.Text.Encoding.UTF8.GetString(b);//new String(b, "ISO-8859-1");
                strcode = Encoding.GetEncoding("ISO-8859-1").GetString(b);
            }
            catch (Exception e)
            {
            }
            return strcode;
        }

         public static string CharAt(string s,int index){
             if((index >= s.Length)||(index<0))
                 return "";
             return s.Substring(index,1);
         }
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • 无法支持的类型 2017-02-15 09:03
    已采纳

    int length = strnum.Length;这个在c#里面不会报错吗?再一个 直接复制进去就行了

    点赞 打赏 评论

相关推荐 更多相似问题