473,568 Members | 2,929 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Leng h +
bof.Length ];

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

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

System.Buffer.B lockCopy(
eof, 0,
combinedBytes, bof.Length + myobjbytes.Leng th, 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(str ing 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(E OF);

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

return Encoding.ASCII. GetBytes(s);
}

Nov 16 '05 #1
3 5049
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.BlockCop y (bytes, BOF.Length, ret, 0, ret.Length);
return ret;

--
Jon Skeet - <sk***@pobox.co m>
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.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
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.BlockCop y (bytes, BOF.Length, ret, 0, ret.Length);
return ret;

--
Jon Skeet - <sk***@pobox.co m>
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******** ********@tk2msf tngp13.phx.gbl. ..
I was thinking the same thing. I'll try it.

"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
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.BlockCop y (bytes, BOF.Length, ret, 0, ret.Length);
return ret;

--
Jon Skeet - <sk***@pobox.co m>
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
9445
by: collinm | last post by:
hi here my code FILE *fp; char *line; #define LINE_MAX 30 fp = fopen("test1.txt", "r");
0
1362
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 for receiving the hash. How about, in a future version of .NET, having a variant of ComputeHash that accepts arguments that specify where the...
5
28165
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 = st1.trim("\n".ToCharArray());
0
1181
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 setting and removing permissons on a mapped folder in the network it works fine but when doing the same thing on a folder om my harddrive Everyone does...
0
3624
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 setting and removing permissons on a mapped folder in the network it works fine but when doing the same thing on a folder om my harddrive Everyone does...
2
9716
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
15911
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 creation of this implicit default constructor, to force the creation of a struct via my constructor only? Zytan
11
14862
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 ascii-counterpart, but there are also such special characters in French and Swedish and many other languages which I also want to catch. Is there a generic way to do...
0
1556
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 1 shade of green at a time. so if i have a 3 shades of green i need to process it 3 times. but my prof. want it to be done only once. when i load...
0
8115
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7961
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5492
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5216
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3649
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3630
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2093
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1206
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
930
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.