473,320 Members | 1,957 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 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 5020
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: collinm | last post by:
hi here my code FILE *fp; char *line; #define LINE_MAX 30 fp = fopen("test1.txt", "r");
0
by: Andrew | last post by:
Right now, the HashAlgorithm.ComputeHash method always creates a new byte array to return the computed hash. Creating a new byte array is wasteful in the case where the caller has an array available...
5
by: ttan | last post by:
How do I remove new line in constant if I have a string like this string st1 ="\abc\def"; I use string trim but it doesn't work. string st2; for (int i = 0; i<st1.Lenght; i++) { st2 =...
0
by: Johan | last post by:
Hi I'm using WMI to set and remove folderpermissions and it sems to work fine, sometimes. I start by having the folderpermissons manuly set to Everyone and Everone has full rights. When I'm...
0
by: Johan | last post by:
Hi I'm using WMI to set and remove folderpermissions and it sems to work fine, sometimes. I start by having the folderpermissons manuly set to Everyone and Everone has full rights. When I'm...
2
by: wanglei0214 | last post by:
I compiles a program in SLOS, but there is a warning i donot know how to remove? here is the framework of the code: typedef struct device_tree { ...... union {
74
by: Zytan | last post by:
I have a struct constructor to initialize all of my private (or public readonly) fields. There still exists the default constructor that sets them all to zero. Is there a way to remove the...
11
by: cody | last post by:
Is there a method to replace special characters like Ä (A-Umlaut) with A, Ö (O-Umlaut) with O, and so on? Sure, I could look for each character separately and replace it with its...
0
by: icebeta | last post by:
hi, I'm doing a project in c# that removes the green background in a picture, like a green screen effect in movies. i already made it partially working. but the problem is that it only remove...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.