-4

私は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)

4

3 に答える 3

1

バッファがのByte[]変換であるString場合、テキストを元に戻すには、次を使用します。

String myString = Encoding.UTF8.GetString(buffer);
于 2013-01-20T16:54:30.550 に答える
1

JavaにあるByteBufferは、.NETのバイト配列と同じではないため、コードを再検討する必要があります。あなたはJavaコードがすることをすることにさえ近づいていません。

1つの手がかりがあります

int start = buf[0];
于 2013-01-20T17:56:19.193 に答える
0

Tnx Michael!

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;

これまでのところ、ターミネーターは正しく削除されています

于 2013-01-20T20:26:26.507 に答える