Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Copying Few Octets from Vector

Reply
Thread Tools

Copying Few Octets from Vector

 
 
smilesonisamal@gmail.com
Guest
Posts: n/a
 
      04-10-2009
Hi all,
I am facing difficulty in copying few octets from a vector to a
buffer.

typedef unsigned char uint8_t;
I have a vector std::vector<uint8_t*> m_Data;
which contains the following octets. I want to copy (30 74 64 230 0
to a buffer. I saw in the web and got code where iterator is used to
copy all the octets not partial elements. How will I retrieve 6 octets
from the vector? I am new to C++ and facing lot of problems in
conversion. I tried a lot but couldnot succeed.Could anybody help me
in this regard?
Any example source code will be great help.

Vector Elements
-------------------------
1 0 94 0 0 2 0 30 74 64 230 0 8 0 69 0 0 48 0 0 0 0 1 17 168 216 146
223 158 3 2
24 0 0 2 7 193 7 193 0 28 81 57 0 0 8 3 10 100 5 0 99 105 115 99 111 0
0 0 146 2
23 158 1

Regards
Soni
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      04-10-2009
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Hi all,
> I am facing difficulty in copying few octets from a vector to a
> buffer.


What kind of difficulty?

> typedef unsigned char uint8_t;
> I have a vector std::vector<uint8_t*> m_Data;
> which contains the following octets. I want to copy (30 74 64 230 0
> to a buffer. I saw in the web and got code where iterator is used to
> copy all the octets not partial elements. How will I retrieve 6 octets
> from the vector? I am new to C++ and facing lot of problems in
> conversion.


"In conversion"? To and from what?

> I tried a lot but couldnot succeed.Could anybody help me
> in this regard?


Yes, we can help. What have you tried? Post your code.

> Any example source code will be great help.


Show us yours and we'll show you ours. Oh, and while you're at it,
check out FAQ 5.2, please. You can find the FAQ list here:
http://www.parashift.com/c++-faq-lite/

>
> Vector Elements
> -------------------------
> 1 0 94 0 0 2 0 30 74 64 230 0 8 0 69 0 0 48 0 0 0 0 1 17 168 216 146
> 223 158 3 2
> 24 0 0 2 7 193 7 193 0 28 81 57 0 0 8 3 10 100 5 0 99 105 115 99 111 0
> 0 0 146 2
> 23 158 1


V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
 
Reply With Quote
 
 
 
 
smilesonisamal@gmail.com
Guest
Posts: n/a
 
      04-10-2009
On Apr 10, 6:25*pm, Victor Bazarov <(E-Mail Removed)> wrote:
> (E-Mail Removed) wrote:
> > Hi all,
> > * * I am facing difficulty in copying few octets from a vector to a
> > buffer.

>
> What kind of difficulty?
>
> > typedef unsigned char uint8_t;
> > I have a vector std::vector<uint8_t*> * m_Data;
> > which contains the following octets. I want to copy (30 74 64 230 0
> > to a buffer. I saw in the web and got code where iterator is used to
> > copy all the octets not partial elements. How will I retrieve 6 octets
> > from the vector? I am new to C++ and facing lot of problems in
> > conversion.

>
> "In conversion"? *To and from what?
>
> *> I tried a lot but couldnot succeed.Could anybody help me
>
> > in this regard?

>
> Yes, we can help. *What have you tried? *Post your code.
>
> > Any example source code will be great help.

>
> Show us yours and we'll show you ours. *Oh, and while you're at it,
> check out FAQ 5.2, please. *You can find the FAQ list here:http://www.parashift.com/c++-faq-lite/
>
>
>
> > Vector Elements
> > -------------------------
> > 1 0 94 0 0 2 0 30 74 64 230 0 8 0 69 0 0 48 0 0 0 0 1 17 168 216 146
> > 223 158 3 2
> > 24 0 0 2 7 193 7 193 0 28 81 57 0 0 8 3 10 100 5 0 99 105 115 99 111 0
> > 0 0 146 2
> > 23 158 1

>
> V
> --
> Please remove capital 'A's when replying by e-mail
> I do not respond to top-posted replies, please don't ask



Hi,

I have tried and could not succeed.

std::vector<uint8_t*> m_Data;

unsigned char*
GetData(unsigned long octetOffset)
{

unsigned long bufferLength = m_Data.size() - octetOffset;
uchar* buffer = new uchar[bufferLength];

// Copy the packet data buffer
const_iterator it = m_Data.begin() + octetOffset;

int octet = 0;
for (; it != m_Data.end(); ++it, ++octet)
buffer[octet] = *it;


return buffer;

}

Regards
Soni



 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      04-10-2009
(E-Mail Removed) wrote:
> On Apr 10, 6:25 pm, Victor Bazarov <(E-Mail Removed)> wrote:
>> (E-Mail Removed) wrote:
>>> Hi all,
>>> I am facing difficulty in copying few octets from a vector to a
>>> buffer.

>> What kind of difficulty?
>>
>>> typedef unsigned char uint8_t;
>>> I have a vector std::vector<uint8_t*> m_Data;
>>> which contains the following octets. I want to copy (30 74 64 230 0
>>> to a buffer. I saw in the web and got code where iterator is used to
>>> copy all the octets not partial elements. How will I retrieve 6 octets
>>> from the vector? I am new to C++ and facing lot of problems in
>>> conversion.

>> "In conversion"? To and from what?
>>
>> > I tried a lot but couldnot succeed.Could anybody help me

>>
>>> in this regard?

>> Yes, we can help. What have you tried? Post your code.
>>
>>> Any example source code will be great help.

>> Show us yours and we'll show you ours. Oh, and while you're at it,
>> check out FAQ 5.2, please. You can find the FAQ list here:http://www.parashift.com/c++-faq-lite/
>>
>>
>>
>>> Vector Elements
>>> -------------------------
>>> 1 0 94 0 0 2 0 30 74 64 230 0 8 0 69 0 0 48 0 0 0 0 1 17 168 216 146
>>> 223 158 3 2
>>> 24 0 0 2 7 193 7 193 0 28 81 57 0 0 8 3 10 100 5 0 99 105 115 99 111 0
>>> 0 0 146 2
>>> 23 158 1

>> V
>> --
>> Please remove capital 'A's when replying by e-mail
>> I do not respond to top-posted replies, please don't ask

>
>
> Hi,
>
> I have tried and could not succeed.


What exactly isn't working? Did you read the FAQ? If you haven't,
please do, and pay close attention to FAQ 5.8.

>
> std::vector<uint8_t*> m_Data;
>
> unsigned char*
> GetData(unsigned long octetOffset)
> {
>
> unsigned long bufferLength = m_Data.size() - octetOffset;
> uchar* buffer = new uchar[bufferLength];
>
> // Copy the packet data buffer
> const_iterator it = m_Data.begin() + octetOffset;
>
> int octet = 0;
> for (; it != m_Data.end(); ++it, ++octet)
> buffer[octet] = *it;
>
>
> return buffer;
>
> }


V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
 
Reply With Quote
 
smilesonisamal@gmail.com
Guest
Posts: n/a
 
      04-10-2009
On Apr 10, 6:54*pm, Victor Bazarov <(E-Mail Removed)> wrote:
> (E-Mail Removed) wrote:
> > On Apr 10, 6:25 pm, Victor Bazarov <(E-Mail Removed)> wrote:
> >> (E-Mail Removed) wrote:
> >>> Hi all,
> >>> * * I am facing difficulty in copying few octets from a vector to a
> >>> buffer.
> >> What kind of difficulty?

>
> >>> typedef unsigned char uint8_t;
> >>> I have a vector std::vector<uint8_t*> * m_Data;
> >>> which contains the following octets. I want to copy (30 74 64 230 0
> >>> to a buffer. I saw in the web and got code where iterator is used to
> >>> copy all the octets not partial elements. How will I retrieve 6 octets
> >>> from the vector? I am new to C++ and facing lot of problems in
> >>> conversion.
> >> "In conversion"? *To and from what?

>
> >> *> I tried a lot but couldnot succeed.Could anybody help me

>
> >>> in this regard?
> >> Yes, we can help. *What have you tried? *Post your code.

>
> >>> Any example source code will be great help.
> >> Show us yours and we'll show you ours. *Oh, and while you're at it,
> >> check out FAQ 5.2, please. *You can find the FAQ list here:http://www.parashift.com/c++-faq-lite/

>
> >>> Vector Elements
> >>> -------------------------
> >>> 1 0 94 0 0 2 0 30 74 64 230 0 8 0 69 0 0 48 0 0 0 0 1 17 168 216 146
> >>> 223 158 3 2
> >>> 24 0 0 2 7 193 7 193 0 28 81 57 0 0 8 3 10 100 5 0 99 105 115 99 111 0
> >>> 0 0 146 2
> >>> 23 158 1
> >> V
> >> --
> >> Please remove capital 'A's when replying by e-mail
> >> I do not respond to top-posted replies, please don't ask

>
> > Hi,

>
> > * I have tried and could not succeed.

>
> What exactly isn't working? *Did you read the FAQ? *If you haven't,
> please do, and pay close attention to FAQ 5.8.
>
>
>
>
>
> > std::vector<uint8_t*> * m_Data;

>
> > unsigned char*
> > GetData(unsigned long octetOffset)
> > {

>
> > unsigned long bufferLength = m_Data.size() - octetOffset;
> > uchar* buffer = new uchar[bufferLength];

>
> > * *// Copy the packet data *buffer
> > * const_iterator it = m_Data.begin() + octetOffset;

>
> > * *int octet = 0;
> > * *for (; it != m_Data.end(); ++it, ++octet)
> > * * * buffer[octet] = *it;

>
> > * *return buffer;

>
> > }

>
> V
> --
> Please remove capital 'A's when replying by e-mail
> I do not respond to top-posted replies, please don't ask


Actually I have written the code for copying the whole packets to the
packet buffer. What I dont know is how to extract 6 octet from the
position-0 in vector.
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      04-10-2009
(E-Mail Removed) wrote:
> [..]
> Actually I have written the code for copying the whole packets to the
> packet buffer. What I dont know is how to extract 6 octet from the
> position-0 in vector.


Let me see if I understand you. You have a vector of elements, like
several hundreds long, right? And you don't know how to get the first
six elements from that vector? That can't be right. Again, I don't
exactly understand what you're saying, most likely.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      04-10-2009
* (E-Mail Removed):
> Hi all,
> I am facing difficulty in copying few octets from a vector to a
> buffer.
>
> typedef unsigned char uint8_t;
> I have a vector std::vector<uint8_t*> m_Data;
> which contains the following octets. I want to copy (30 74 64 230 0
> to a buffer. I saw in the web and got code where iterator is used to
> copy all the octets not partial elements. How will I retrieve 6 octets
> from the vector? I am new to C++ and facing lot of problems in
> conversion. I tried a lot but couldnot succeed.Could anybody help me
> in this regard?
> Any example source code will be great help.
>
> Vector Elements
> -------------------------
> 1 0 94 0 0 2 0 30 74 64 230 0 8 0 69 0 0 48 0 0 0 0 1 17 168 216 146
> 223 158 3 2
> 24 0 0 2 7 193 7 193 0 28 81 57 0 0 8 3 10 100 5 0 99 105 115 99 111 0
> 0 0 146 2
> 23 158 1
>
> Regards
> Soni


Please explain your data structure.

You have a vector of pointers.

Is it the first pointer in the vector that points to the data listed above?

How do you know the data size?

Also, please explain exactly what you're trying to accomplish.


Cheers & hth.,

- Alf

--
Due to hosting requirements I need visits to <url: http://alfps.izfree.com/>.
No ads, and there is some C++ stuff! Just going there is good. Linking
to it is even better! Thanks in advance!
 
Reply With Quote
 
smilesonisamal@gmail.com
Guest
Posts: n/a
 
      04-10-2009
On Apr 10, 10:10*pm, "Alf P. Steinbach" <(E-Mail Removed)> wrote:
> * (E-Mail Removed):
>
>
>
> > Hi all,
> > * * I am facing difficulty in copying few octets from a vector to a
> > buffer.

>
> > typedef unsigned char uint8_t;
> > I have a vector std::vector<uint8_t*> * m_Data;
> > which contains the following octets. I want to copy (30 74 64 230 0
> > to a buffer. I saw in the web and got code where iterator is used to
> > copy all the octets not partial elements. How will I retrieve 6 octets
> > from the vector? I am new to C++ and facing lot of problems in
> > conversion. I tried a lot but couldnot succeed.Could anybody help me
> > in this regard?
> > Any example source code will be great help.

>
> > Vector Elements
> > -------------------------
> > 1 0 94 0 0 2 0 30 74 64 230 0 8 0 69 0 0 48 0 0 0 0 1 17 168 216 146
> > 223 158 3 2
> > 24 0 0 2 7 193 7 193 0 28 81 57 0 0 8 3 10 100 5 0 99 105 115 99 111 0
> > 0 0 146 2
> > 23 158 1

>
> > Regards
> > Soni

>
> Please explain your data structure.
>
> You have a vector of pointers.
>
> Is it the first pointer in the vector that points to the data listed above?

First pointer of vector points to first element of the vector.
>
> How do you know the data size?

m_Data.size()
>
> Also, please explain exactly what you're trying to accomplish.


Basically Iam trying to get the first 6 octets of the ethernet packet
(destination mac address) from the packet(decimal format) and try to
compare with the a different mac address. if they are same I need to
do some more operation. All the octets are stored in a vector.
>
> Cheers & hth.,
>
> - Alf
>
> --
> Due to hosting requirements I need visits to <url:http://alfps.izfree.com/>.
> No ads, and there is some C++ stuff! Just going there is good. Linking
> to it is even better! Thanks in advance!


For Victor
-----------
Actually I found lot of examples in vector which traverses from begin
to end. What I understand is a iterator traverses an object. Am I
right? If I will have the code as below:

for (it=m_Data.begin(); it<6; it++)
Its gonna fail in compilation.Am I right?




 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      04-10-2009
* (E-Mail Removed):
> On Apr 10, 10:10 pm, "Alf P. Steinbach" <(E-Mail Removed)> wrote:
>> * (E-Mail Removed):
>>
>>
>>
>>> Hi all,
>>> I am facing difficulty in copying few octets from a vector to a
>>> buffer.
>>> typedef unsigned char uint8_t;
>>> I have a vector std::vector<uint8_t*> m_Data;
>>> which contains the following octets. I want to copy (30 74 64 230 0
>>> to a buffer. I saw in the web and got code where iterator is used to
>>> copy all the octets not partial elements. How will I retrieve 6 octets
>>> from the vector? I am new to C++ and facing lot of problems in
>>> conversion. I tried a lot but couldnot succeed.Could anybody help me
>>> in this regard?
>>> Any example source code will be great help.
>>> Vector Elements
>>> -------------------------
>>> 1 0 94 0 0 2 0 30 74 64 230 0 8 0 69 0 0 48 0 0 0 0 1 17 168 216 146
>>> 223 158 3 2
>>> 24 0 0 2 7 193 7 193 0 28 81 57 0 0 8 3 10 100 5 0 99 105 115 99 111 0
>>> 0 0 146 2
>>> 23 158 1
>>> Regards
>>> Soni

>> Please explain your data structure.
>>
>> You have a vector of pointers.
>>
>> Is it the first pointer in the vector that points to the data listed above?

> First pointer of vector points to first element of the vector.


The first vector element points to itself?

The first vector element points to the first element of some raw array?

What?


>> How do you know the data size?

> m_Data.size()


You have a vector of pointers.

The number of pointers is quite likely not the size of your numerical data shown
above.


>> Also, please explain exactly what you're trying to accomplish.

>
> Basically Iam trying to get the first 6 octets of the ethernet packet
> (destination mac address) from the packet(decimal format) and try to
> compare with the a different mac address.


What do you mean by "decimal format".


Cheers & hth.,

- Alf

--
Due to hosting requirements I need visits to <url: http://alfps.izfree.com/>.
No ads, and there is some C++ stuff! Just going there is good. Linking
to it is even better! Thanks in advance!
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      04-10-2009
On Apr 10, 3:18 pm, (E-Mail Removed) wrote:

> I am facing difficulty in copying few octets from a vector to a
> buffer.


> typedef unsigned char uint8_t;
> I have a vector std::vector<uint8_t*> m_Data;


I'm not sure about the rest, but this vector doesn't contain
octets; it contains pointers.

> which contains the following octets.


And none follow. So we have a vector which contains
pointers, and there are no octets. So far, so good.

> I want to copy (30 74 64 230 0 to a buffer.


Where do these octets come from?

> I saw in the web and got code where iterator is used to
> copy all the octets not partial elements. How will I
> retrieve 6 octets from the vector?


If the vector contains pointers, you can't. All you can get
from it is pointers.

Supposing you have a vector of octets:
std::vector< uint8_t > data ;
*and* supposing that offset plus length are less than the
size, you can copy the first length octets, starting from
offset, using std::copy:
std::copy( data.begin() + offset,
data.begin() + offest + length,
destination ) ;
You'd best check that offset + length <= data.size()
beforehand, though.

--
James Kanze (GABI Software) email:(E-Mail Removed)
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
DBD::mysql used to take octets into the utf8 texts but no more inmariadb Peter Vereshagin Perl Misc 3 03-17-2011 12:28 PM
Reading from a socket: first characters, then octets Stefan Ram Java 8 07-18-2009 03:44 AM
cannot calculate octets davide.papagno@gmail.com Cisco 9 07-17-2006 11:16 AM
Free memory allocate by a STL vector, vector of vector, map of vector Allerdyce.John@gmail.com C++ 8 02-18-2006 12:48 AM
Literal Escaped Octets Chason Hayes Python 11 02-08-2006 11:53 PM



Advertisments