ITeye博客

C#中byte对于负数的处理

ljl.java · 2011-11-24 15:27 · C# · 编程语言 · Markdown

C#调用Java webSevice 中 问题

C#调Javawebserivce 中byte 取负数时 由于C#中的机制byte只能存入无符号数据,如果遇到负数问题,把负数转化为十六进制 无符号数据,底层是以流来读取这不要担心 将b & 0xFF将一个byte和 0xFF进行了与运算 代码如下: private void Form1_Load(object sender, EventArgs e) {

        htc.HTCWebService h = new htc.HTCWebService();
        string s = h.QueryOrder("201109051402", "voFYbZmlEErGTh6sNBEFEw==", "VOiyOrbvG5fKRrwjrJPIxw==");
        string s2 = s.Split(',');
        //Convert.ToByte(
        byte b = new byte;
        //System.IO.FileStream fs1 = new System.IO.FileStream("c:\\b.xml", System.IO.FileMode.Open, System.IO.FileAccess.Read);


        for (int i = 0; i < s2.Length; i++)
        {
            //如果是负数转化成无符号数据
            if (int.Parse(s2*.ToString()) < 0)
                b* = (byte)(0xff & int.Parse(s2*.ToString()));
            else
                b* = byte.Parse(s2*.ToString());
        }

        //System.IO.MemoryStream ms = new System.IO.MemoryStream();
        //ms.Read(b, 0, b.Length);
        System.IO.FileStream fs = new System.IO.FileStream("c:\\aaa.xml", System.IO.FileMode.Append);
        fs.Write(b, 0, b.Length);
        fs.Close();


    }

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。