【问题标题】:C# version of 2 Java methods with ByteBuffer [closed]带有 ByteBuffer 的 2 个 Java 方法的 C# 版本 [关闭]
【发布时间】:2013-01-20 16:47:46
【问题描述】:

我是 C# 新手,非常感谢我需要在 C# 中实现的 2 个 Java 方法的帮助

public static String getTerminatedString(ByteBuffer buf) {
                return new String(getTerminatedArray(buf));
        }



public static byte[] getTerminatedArray(ByteBuffer buf) {
            int start = buf.position();

            while(buf.get() != 0) {}
            int end = buf.position();

            byte[] bytes = new byte[end - start - 1]; //don't include terminator
            buf.position(start);
            buf.get(bytes);

            //put position after array
            buf.position(end); //skip terminator

            return bytes;
    }

提前致谢!!!

更新:

到目前为止,我写了这个:

 public static String getTerminatedString(byte[] buf) 
        {
            try
            {
                unsafe
                {
                    byte[] res = getTerminatedArray(buf);

                    fixed (byte* ptr_byte = &res[0]) // NullRef exception here
                    {
                        sbyte* ptr_sbyte = (sbyte*)ptr_byte;
                        return new String(ptr_sbyte);
                    }
                }
            }
            catch (NullReferenceException nre)
            {
                Console.Error.WriteLine(nre.Message + Environment.NewLine + nre.StackTrace);
                return null;
            }
        }

        public static byte[] getTerminatedArray(byte[] buf) 
        {
            try
            {

                int start = buf[0];
                int end = 0;
                for (int i = 0; i < buf.Length; i++)
                {
                    if (buf[i] == 0)
                    {
                        end = i;
                        break;
                    }
                }

                byte[] bytes = new byte[end - start - 1]; //overflow exeption here

                for (int i = 0; i < bytes.Length; i++)
                {
                    bytes[i] = buf[i];
                }

                return bytes;
            }
            catch (OverflowException oe)
            {
                Console.Error.WriteLine(oe.Message + Environment.NewLine + oe.StackTrace);
                return null;
            }
        }

但我得到了上面标记的 2 个例外。我检查了这些 ByteBuffer 方法背后的逻辑,但不知道我做错了什么>.>

所以 nullreferenceexception 是 catch 块返回 null 的结果。 问题出在 byte[] bytes = new byte[end - start - 1]; (如果我的实现是正确的......是吗?:D)

【问题讨论】:

  • 鉴于 NIO 在 .NET 中并没有任何直接的等价物,您真的应该退后一步,考虑一下实现大局的惯用方式。
  • 您自己似乎没有付出任何努力来解决这个问题。请不要指望我们为您这样做。
  • 试着问一些更具体的问题。
  • please-translate-this-code-for-me 在 stackoverflow 上不受欢迎
  • @MichaelViktorStarberg 抱歉,我也是新来的,不仅在 c#...

标签: c# java arrays bytebuffer


【解决方案1】:

Java 中的 ByteBuffer 与 .NET 中的字节数组不同 因此,您需要重新考虑您的代码。你甚至还没有完成 Java 代码所做的事情。

一条线索在

int start = buf[0];

【讨论】:

    【解决方案2】:

    如果您的缓冲区是StringByte[] 转换,则只需使用即可取回您的文本:

    String myString = Encoding.UTF8.GetString(buffer);
    

    【讨论】:

      【解决方案3】:

      Tnx 迈克尔!

      我查看了 Java 中的 ByteBuffer 文档,还发现了一些非常有趣且有用的 C# ByteBuffer 实现 - http://www.codeforge.com/read/92854/ByteBuffer.cs__html

      我加了

      public virtual byte[] Get(byte[] bytes)
          {
              long oldpos = Position;
              bytes = reader.ReadBytes(bytes.Length);
              Position = oldpos;
      
              return bytes;
          }
      

      所以我可以使用 buf.get(bytes); //使用此方法后,MemoryStream 位置不应该改变,是吗?

      我用它测试过

      string str = "Test_tesT-Test" + char.MinValue;
      

      到目前为止,它正确地删除了终止符

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-02-10
        • 2011-06-18
        • 1970-01-01
        • 1970-01-01
        • 2014-08-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多