472,133 Members | 1,042 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,133 software developers and data experts.

Remove BOF and EOF from byte[]

TJO
Can someone recomend a good technique for removing BOF and EOF markers from
a byte[]? I am passing a byte[] thru an FTP socket. I am serializing an
object into a byte[] and then adding an EOF marker and a BOF marker to the
byte[]. On the receiving end I need to remove the markers and then
deserialize my object. My object does not seem to deserialize so I am not
sure I am removing the EOF and BOF properly. All advice welcome. Thank
you.

Here is the code snippet.

byte[] bof = Encoding.Ascii.GetBytes("<BOF>");
byte[] eof = Encoding.Ascii.GetBytes("<EOF>");
byte[] myobjbytes = myobj.Serialize();

// Create empty byte[] of combined objects
byte[] combinedBytes = new byte[ eof.Length + myobjbytes.Lengh +
bof.Length ];

// Copy contents of objects to empty byte[]
System.Buffer.BlockCopy(
bof, 0,
myobjbytes, 0, bof.Length);

System.Buffer.BlockCopy(
myobjbytes , 0,
combinedBytes, bof.Length, myobjbytes .Length);

System.Buffer.BlockCopy(
eof, 0,
combinedBytes, bof.Length + myobjbytes.Length, eof.Length);

// Send byte[] thru the socket

// Now I want to trim off the EOF and BOF then Deserialize my object.
// Here is my attept but the remaining object does not like to be
deserialized :(

private byte[] TrimBOF_EOF(string BOF, string EOF, byte[] bytes)
{
string s = new string(Encoding.ASCII.GetChars(bytes));

s = s.Remove(0, BOF.Length);

// Get position of EOF
int EOFPos = s.LastIndexOf(EOF);

// Trim EOF mark thru the end of the string
if(EOFPos > -1)
s = s.Remove(s.Length-EOF.Length, EOF.Length);

return Encoding.ASCII.GetBytes(s);
}

Nov 16 '05 #1
3 4846
TJO <no@spam.com> wrote:
Can someone recomend a good technique for removing BOF and EOF markers from
a byte[]? I am passing a byte[] thru an FTP socket. I am serializing an
object into a byte[] and then adding an EOF marker and a BOF marker to the
byte[]. On the receiving end I need to remove the markers and then
deserialize my object. My object does not seem to deserialize so I am not
sure I am removing the EOF and BOF properly. All advice welcome. Thank
you.


The fact that you're turning into a string and back is probably what's
going wrong - the top bit is going to be removed from every byte.

Why not just use (for your TrimBOF_EOF method):

byte[] ret = new byte[bytes.Length-BOF.Length-EOF.Length];
Buffer.BlockCopy (bytes, BOF.Length, ret, 0, ret.Length);
return ret;

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
TJO
I was thinking the same thing. I'll try it.

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
TJO <no@spam.com> wrote:
Can someone recomend a good technique for removing BOF and EOF markers
from
a byte[]? I am passing a byte[] thru an FTP socket. I am serializing an
object into a byte[] and then adding an EOF marker and a BOF marker to
the
byte[]. On the receiving end I need to remove the markers and then
deserialize my object. My object does not seem to deserialize so I am
not
sure I am removing the EOF and BOF properly. All advice welcome. Thank
you.


The fact that you're turning into a string and back is probably what's
going wrong - the top bit is going to be removed from every byte.

Why not just use (for your TrimBOF_EOF method):

byte[] ret = new byte[bytes.Length-BOF.Length-EOF.Length];
Buffer.BlockCopy (bytes, BOF.Length, ret, 0, ret.Length);
return ret;

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #3
TJO
Works Great thanks!
"TJO" <no@spam.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
I was thinking the same thing. I'll try it.

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
TJO <no@spam.com> wrote:
Can someone recomend a good technique for removing BOF and EOF markers
from
a byte[]? I am passing a byte[] thru an FTP socket. I am serializing
an
object into a byte[] and then adding an EOF marker and a BOF marker to
the
byte[]. On the receiving end I need to remove the markers and then
deserialize my object. My object does not seem to deserialize so I am
not
sure I am removing the EOF and BOF properly. All advice welcome. Thank
you.


The fact that you're turning into a string and back is probably what's
going wrong - the top bit is going to be removed from every byte.

Why not just use (for your TrimBOF_EOF method):

byte[] ret = new byte[bytes.Length-BOF.Length-EOF.Length];
Buffer.BlockCopy (bytes, BOF.Length, ret, 0, ret.Length);
return ret;

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Nov 16 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by collinm | last post: by
reply views Thread by Andrew | last post: by
5 posts views Thread by ttan | last post: by
74 posts views Thread by Zytan | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.