By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,876 Members | 1,478 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,876 IT Pros & Developers. It's quick & easy.

Remove BOF and EOF from byte[]

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
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.