473,657 Members | 2,489 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Creating Buffer

Hello
I want to create a byte[] and use it as a buffer, then access it with a
binaryreader or streamreader.
what is important is to be able to read different variables form that, like
readint16, readbytes(),rea dint32.... . so if there is a method to read byte
arrays that would also help.
(i couldn' use convert class )
is that possible? if yes how?
Nov 16 '05 #1
6 7237
You may instantiate a System.IO.Binar yReader/Writer by passing a
MemoryStream
(which in turn is instantiated with the buffer you want to work with)
to the constructor as the base stream.

Have a look at System.BitConve rter for conversions from byte sequences into
primitive data types.
--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"BMax" <sd@asd.c> wrote in message
news:uw******** ******@TK2MSFTN GP10.phx.gbl...
Hello
I want to create a byte[] and use it as a buffer, then access it with a
binaryreader or streamreader.
what is important is to be able to read different variables form that, like readint16, readbytes(),rea dint32.... . so if there is a method to read byte arrays that would also help.
(i couldn' use convert class )
is that possible? if yes how?

Nov 16 '05 #2
Thanks.
didn't use MemoryStream , BitConvertor solved all problems.
But for a better code, do you know how to creat template prototype for
classes? in c# I mean. then i can pass the buffer to a function with desired
data type and it returns a TypeOf object?
"Dennis Myrén" <de****@oslokb. no> wrote in message
news:_O******** **********@news 4.e.nsc.no...
You may instantiate a System.IO.Binar yReader/Writer by passing a
MemoryStream
(which in turn is instantiated with the buffer you want to work with)
to the constructor as the base stream.

Have a look at System.BitConve rter for conversions from byte sequences into primitive data types.
--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"BMax" <sd@asd.c> wrote in message
news:uw******** ******@TK2MSFTN GP10.phx.gbl...
Hello
I want to create a byte[] and use it as a buffer, then access it with a
binaryreader or streamreader.
what is important is to be able to read different variables form that,

like
readint16, readbytes(),rea dint32.... . so if there is a method to read

byte
arrays that would also help.
(i couldn' use convert class )
is that possible? if yes how?


Nov 16 '05 #3
I suppose you mean something like:
int i = (int) GetPrimitive(my Buffer, typeof(int));
object GetPrimitive ( byte sourceBuffer, System.Type returnType )
{
....
}

You could do it like that, but i think you will gain performance
and efficiency by avoiding the type casts that you must then do.

Did i understand your question correct?
If not, let me know, and i will try once again to help you.
--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"BMax" <sd@asd.c> wrote in message
news:%2******** ********@tk2msf tngp13.phx.gbl. ..
Thanks.
didn't use MemoryStream , BitConvertor solved all problems.
But for a better code, do you know how to creat template prototype for
classes? in c# I mean. then i can pass the buffer to a function with desired data type and it returns a TypeOf object?
"Dennis Myrén" <de****@oslokb. no> wrote in message
news:_O******** **********@news 4.e.nsc.no...
You may instantiate a System.IO.Binar yReader/Writer by passing a
MemoryStream
(which in turn is instantiated with the buffer you want to work with)
to the constructor as the base stream.

Have a look at System.BitConve rter for conversions from byte sequences

into
primitive data types.
--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"BMax" <sd@asd.c> wrote in message
news:uw******** ******@TK2MSFTN GP10.phx.gbl...
Hello
I want to create a byte[] and use it as a buffer, then access it with a binaryreader or streamreader.
what is important is to be able to read different variables form that,

like
readint16, readbytes(),rea dint32.... . so if there is a method to read

byte
arrays that would also help.
(i couldn' use convert class )
is that possible? if yes how?



Nov 16 '05 #4
Not really.
what I asked was a function that can return deferent types depends on
parameters.
like :
myfunction(buff er buff , short i) //returns a short
myfunction(buff er buff , myobject p)//returns myobject
for this I defined different version of a same function within a class:

private int func(buffer, int i){}
private byte[] func(buffer,byt e[]) {}
private sting ....

so in main class i just pass buffer and desired type to function and it
returns the same type.
but can i avoid writing different version of the same function? here the
template thing can help. then i don't need to handle all types. your code
will work, but is there another way without type cast?
Thank you very much for your helps, it really helps.

"Dennis Myrén" <de****@oslokb. no> wrote in message
news:2W******** **********@news 2.e.nsc.no...
I suppose you mean something like:
int i = (int) GetPrimitive(my Buffer, typeof(int));
object GetPrimitive ( byte sourceBuffer, System.Type returnType )
{
...
}

You could do it like that, but i think you will gain performance
and efficiency by avoiding the type casts that you must then do.

Did i understand your question correct?
If not, let me know, and i will try once again to help you.
--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"BMax" <sd@asd.c> wrote in message
news:%2******** ********@tk2msf tngp13.phx.gbl. ..
Thanks.
didn't use MemoryStream , BitConvertor solved all problems.
But for a better code, do you know how to creat template prototype for
classes? in c# I mean. then i can pass the buffer to a function with desired
data type and it returns a TypeOf object?
"Dennis Myrén" <de****@oslokb. no> wrote in message
news:_O******** **********@news 4.e.nsc.no...
You may instantiate a System.IO.Binar yReader/Writer by passing a
MemoryStream
(which in turn is instantiated with the buffer you want to work with)
to the constructor as the base stream.

Have a look at System.BitConve rter for conversions from byte sequences

into
primitive data types.
--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"BMax" <sd@asd.c> wrote in message
news:uw******** ******@TK2MSFTN GP10.phx.gbl...
> Hello
> I want to create a byte[] and use it as a buffer, then access it with a
> binaryreader or streamreader.
> what is important is to be able to read different variables form

that, like
> readint16, readbytes(),rea dint32.... . so if there is a method to read byte
> arrays that would also help.
> (i couldn' use convert class )
> is that possible? if yes how?
>
>



Nov 16 '05 #5
Ok, now I understand;
you have to do it that way because C# does not allow you
to have overloaded methods with the same parameters that only differ in
return type.
So the last parameters of these functions are really just dummy parameters
to get around that
overloading limitation.

My advice on this one is to use output parameters.
That way you can use the same function names, and you may gain some
efficiency
and clearness by not having to send a dummy parameter to indicate the return
type.
You define an output parameter by the keyword "out".

This is how you do it:

private void getPrimitive ( byte [] sourceBuffer, int offset, out int
result )
{
result = System.BitConve rter.ToInt32(so urceBuffer, offset);
}

private void getPrimitive ( byte [] sourceBuffer, int offset, out float
result )
{
result = System.BitConve rter.ToSingle(s ourceBuffer, offset);
}

private void getPrimitive ( byte [] sourceBuffer, int offset, out string
result )
{
result = System.BitConve rter.ToString(s ourceBuffer, offset);
}

Now you can call this functions with only necessary parameters:

int resultInt32; // Initialization not needed as it will be passed as out
parameter.
float resultSingle; // Initialization not needed as it will be passed as out
parameter.
string resultString; // Initialization not needed as it will be passed as
out parameter.
byte [] buff = LoadBuffer(); // Should contain your working byte buffer.

// Get a string from the buffer at position 2.
// We must use the out keyword even in the calling context.
getPrimitive(bu ff, 2, out resultString);

// Get an Int32 from the buffer at position 8:
// We must use the out keyword even in the calling context.
getPrimitive(bu ff, 8, out resultInt32);

// Get a Single from the buffer at position 82:
// We must use the out keyword even in the calling context.
getPrimitive(bu ff, 82, out resultSingle);
--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"Bmax" <BluemanREMOVET HISANDPUTATSIGH Ngmail.com> wrote in message
news:Oc******** ******@TK2MSFTN GP10.phx.gbl...
Not really.
what I asked was a function that can return deferent types depends on
parameters.
like :
myfunction(buff er buff , short i) //returns a short
myfunction(buff er buff , myobject p)//returns myobject
for this I defined different version of a same function within a class:

private int func(buffer, int i){}
private byte[] func(buffer,byt e[]) {}
private sting ....

so in main class i just pass buffer and desired type to function and it
returns the same type.
but can i avoid writing different version of the same function? here the
template thing can help. then i don't need to handle all types. your code
will work, but is there another way without type cast?
Thank you very much for your helps, it really helps.

"Dennis Myrén" <de****@oslokb. no> wrote in message
news:2W******** **********@news 2.e.nsc.no...
I suppose you mean something like:
int i = (int) GetPrimitive(my Buffer, typeof(int));
object GetPrimitive ( byte sourceBuffer, System.Type returnType )
{
...
}

You could do it like that, but i think you will gain performance
and efficiency by avoiding the type casts that you must then do.

Did i understand your question correct?
If not, let me know, and i will try once again to help you.
--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"BMax" <sd@asd.c> wrote in message
news:%2******** ********@tk2msf tngp13.phx.gbl. ..
Thanks.
didn't use MemoryStream , BitConvertor solved all problems.
But for a better code, do you know how to creat template prototype for
classes? in c# I mean. then i can pass the buffer to a function with

desired
data type and it returns a TypeOf object?
"Dennis Myrén" <de****@oslokb. no> wrote in message
news:_O******** **********@news 4.e.nsc.no...
> You may instantiate a System.IO.Binar yReader/Writer by passing a
> MemoryStream
> (which in turn is instantiated with the buffer you want to work with) > to the constructor as the base stream.
>
> Have a look at System.BitConve rter for conversions from byte sequences into
> primitive data types.
>
>
> --
> Regards,
> Dennis JD Myrén
> Oslo Kodebureau
> "BMax" <sd@asd.c> wrote in message
> news:uw******** ******@TK2MSFTN GP10.phx.gbl...
> > Hello
> > I want to create a byte[] and use it as a buffer, then access it with
a
> > binaryreader or streamreader.
> > what is important is to be able to read different variables form

that, > like
> > readint16, readbytes(),rea dint32.... . so if there is a method to read > byte
> > arrays that would also help.
> > (i couldn' use convert class )
> > is that possible? if yes how?
> >
> >
>
>



Nov 16 '05 #6
Bingo!
I didn't know anything about this Output parameter.now I can optimize my
code.
don't even know how to thank you :)
Tak So mycket.

"Dennis Myrén" <de****@oslokb. no> wrote in message
news:Kz******** **********@news 2.e.nsc.no...
Ok, now I understand;
you have to do it that way because C# does not allow you
to have overloaded methods with the same parameters that only differ in
return type.
So the last parameters of these functions are really just dummy parameters
to get around that
overloading limitation.

My advice on this one is to use output parameters.
That way you can use the same function names, and you may gain some
efficiency
and clearness by not having to send a dummy parameter to indicate the
return
type.
You define an output parameter by the keyword "out".

This is how you do it:

private void getPrimitive ( byte [] sourceBuffer, int offset, out int
result )
{
result = System.BitConve rter.ToInt32(so urceBuffer, offset);
}

private void getPrimitive ( byte [] sourceBuffer, int offset, out float
result )
{
result = System.BitConve rter.ToSingle(s ourceBuffer, offset);
}

private void getPrimitive ( byte [] sourceBuffer, int offset, out string
result )
{
result = System.BitConve rter.ToString(s ourceBuffer, offset);
}

Now you can call this functions with only necessary parameters:

int resultInt32; // Initialization not needed as it will be passed as out
parameter.
float resultSingle; // Initialization not needed as it will be passed as
out
parameter.
string resultString; // Initialization not needed as it will be passed as
out parameter.
byte [] buff = LoadBuffer(); // Should contain your working byte buffer.

// Get a string from the buffer at position 2.
// We must use the out keyword even in the calling context.
getPrimitive(bu ff, 2, out resultString);

// Get an Int32 from the buffer at position 8:
// We must use the out keyword even in the calling context.
getPrimitive(bu ff, 8, out resultInt32);

// Get a Single from the buffer at position 82:
// We must use the out keyword even in the calling context.
getPrimitive(bu ff, 82, out resultSingle);
--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"Bmax" <BluemanREMOVET HISANDPUTATSIGH Ngmail.com> wrote in message
news:Oc******** ******@TK2MSFTN GP10.phx.gbl...
Not really.
what I asked was a function that can return deferent types depends on
parameters.
like :
myfunction(buff er buff , short i) //returns a short
myfunction(buff er buff , myobject p)//returns myobject
for this I defined different version of a same function within a class:

private int func(buffer, int i){}
private byte[] func(buffer,byt e[]) {}
private sting ....

so in main class i just pass buffer and desired type to function and it
returns the same type.
but can i avoid writing different version of the same function? here the
template thing can help. then i don't need to handle all types. your code
will work, but is there another way without type cast?
Thank you very much for your helps, it really helps.

"Dennis Myrén" <de****@oslokb. no> wrote in message
news:2W******** **********@news 2.e.nsc.no...
> I suppose you mean something like:
> int i = (int) GetPrimitive(my Buffer, typeof(int));
> object GetPrimitive ( byte sourceBuffer, System.Type returnType )
> {
> ...
> }
>
> You could do it like that, but i think you will gain performance
> and efficiency by avoiding the type casts that you must then do.
>
> Did i understand your question correct?
> If not, let me know, and i will try once again to help you.
>
>
> --
> Regards,
> Dennis JD Myrén
> Oslo Kodebureau
> "BMax" <sd@asd.c> wrote in message
> news:%2******** ********@tk2msf tngp13.phx.gbl. ..
> > Thanks.
> > didn't use MemoryStream , BitConvertor solved all problems.
> > But for a better code, do you know how to creat template prototype
> > for
> > classes? in c# I mean. then i can pass the buffer to a function with
> desired
> > data type and it returns a TypeOf object?
> >
> >
> > "Dennis Myrén" <de****@oslokb. no> wrote in message
> > news:_O******** **********@news 4.e.nsc.no...
> > > You may instantiate a System.IO.Binar yReader/Writer by passing a
> > > MemoryStream
> > > (which in turn is instantiated with the buffer you want to work with) > > > to the constructor as the base stream.
> > >
> > > Have a look at System.BitConve rter for conversions from byte sequences > > into
> > > primitive data types.
> > >
> > >
> > > --
> > > Regards,
> > > Dennis JD Myrén
> > > Oslo Kodebureau
> > > "BMax" <sd@asd.c> wrote in message
> > > news:uw******** ******@TK2MSFTN GP10.phx.gbl...
> > > > Hello
> > > > I want to create a byte[] and use it as a buffer, then access it

with
> a
> > > > binaryreader or streamreader.
> > > > what is important is to be able to read different variables form

that,
> > > like
> > > > readint16, readbytes(),rea dint32.... . so if there is a method to

read
> > > byte
> > > > arrays that would also help.
> > > > (i couldn' use convert class )
> > > > is that possible? if yes how?
> > > >
> > > >
> > >
> > >
> >
> >
>
>



Nov 16 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
1595
by: Polvere | last post by:
Hi, I need to create a txt file every time the loop of a for cicle is executed, and these files have to be named as the current for variable. Something like this (in pseudo code, I know that's fully wrong): for(a=0; a<VAR; a++) { fopen (a.txt); fprintf ( whatever);
7
7847
by: mike | last post by:
Hi, I am having difficulty in creating a thread using pthread_create. It seems that pthread_create does not execute 'program', and returns -1; I have checked the API but I am not sure why this isn't working. #include <stdio.h> #include <pthread.h>
1
5567
by: Arcnet | last post by:
Using MemoryStream I Have a problem to create a new Image from byte array that originaly was created from an older image (Everything is being preformed in the same Thread) //Get Image From File byte arr;
2
6155
by: bobrics | last post by:
Hi, I would like to create a packet for a RAW socket transfer. Please let me know if this is the right approach. 1. First, I am creating a header structure where I store all the information I would like to put in the header of a packet. 2. I know the size of data I will send and the header size in bytes, so I allocate the space in memory for both and create two pointers: for data part (dataptr) and header part. Header pointer coinsided...
1
2519
by: MAB | last post by:
I need to create a service (or something else) to monitor the coming and goings of files (& possible folders) beneath a root folder. This service need to know the path/FileName of any new files so it can add data to a database. If files are deleted the service needs to know the path/FileName too for it can delete data from the database. The service will (or execute another application that will) query inside the added documents for other...
13
2013
by: Justcallmedrago | last post by:
How would you declare and assign a variable inside a function THAT HAS THE NAME OF A PARAMETER YOU PASSED example: when you call createvariable("myvariable") it will declare the variable "myvariable" and then maybe assign it something. myvariable = "this is a real variable"
4
1850
by: Jack | last post by:
Hi, I do a webrequest and it returns some text data in a stream. I want to put this text data into a string. I've got it working just fine, but I have to put the text data into into a fixed-size buffer BEFORE I put it into a string (ConstBufferByteSize=1000000). This fixed size buffer wastes space. Is
4
3010
by: Philipp.Weissenbacher | last post by:
Hi all! I wrote the following as a test case for a menu based programme: #include <iostream> #include <cstring> using namespace std; #define cls system("cls") const int SIZE = 10;
1
2916
by: stevedub | last post by:
I am having some trouble configuring my array to read from a sequential file, and then calling on that to fill an array of interests. I think I have the class set up to read the file, but when I run my program the rates array does not get the information. I think my problem is where I am actuall calling the array index, but I am not sure how to do this. Here is my code: /* * MortFrame.java * * Created on February 24, 2008, 7:28 PM */...
0
8305
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8823
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8726
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8503
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8603
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7320
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6163
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4151
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2726
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

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.