6 Replies - 1133 Views - Last Post: 21 June 2008 - 01:22 PM Rate Topic: -----

#1 adamj2  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 02-June 08

Help with using substr to extract data of type byte

Posted 19 June 2008 - 04:09 PM

Hi there,

What I am trying to do is implement my own sequence number program.

I have a sequence number i.e 0001 and then I try to convert this to a binary string in byte format of 4 bytes: 00 01 00 00

And then I am appending this in the middle of a byte payload, which is just a group of numbers represented as bytes: ie 32 33 34 35 36 etc.

I then want to extract the sequence number portion, which is bytes 4-8 of the 8 byte payload. This is where I am stuck and not sure how to progress.

I am trying to use the substr command but am not just trying to extract data from a string, rather a binary string of bytes. I am just extracting the sequence number part of the last calculated sequence number for now to try and get it working.

Here is my code, any help would be great, Many thanks!

#include <stdio.h>

#include <iostream>

#include <string>
using std::string;


#include "hex.h"

unsigned char temp[4];
unsigned int seq;
bool flag = true;
unsigned int currentseq;
unsigned int previousseq = 0;

int testing =2;

int main()

{

byte payload[8] = {"1234567"};

for (int i=0; i<10; i++)
	{

if (flag == true)
{
	seq = 0000;
	flag = false;
}

seq=seq++;

printf("Sender sequence number = %0004x", seq);
printf("\n");

// BYTE CONVERSION


	for( int i=0;i<4;i++)
	temp[i]= '\0';
	memcpy(temp,&seq,4);
	printf("TEMP = %02x %02x %02x %02x \n", temp[0], temp[1], temp[2], temp[3]);


	payload[4] = temp[0];
	payload[5] = temp[1];
	payload[6] = temp[2];
	payload[7] = temp[3];
	printf ("payload[4] =  %02x \n", temp[0]);
	printf("\n");


	} 

[b]	//this is where my problems are, am trying to extract by sequence number in byte format from the binary string, but not sure how
	string str = payload;
	const char *p = str.substr(2,4).c_str();
	std::cout << "\n" << p << "\n";[/b]
	

}


This post has been edited by adamj2: 19 June 2008 - 04:10 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Help with using substr to extract data of type byte

#2 perfectly.insane  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 70
  • View blog
  • Posts: 644
  • Joined: 22-March 08

Re: Help with using substr to extract data of type byte

Posted 19 June 2008 - 06:41 PM

It's probably not a very good idea to use a std::string for this purpose. While you can technically do it if you allocate the string prior and use std::copy to copy the items into the string, there is little advantage here over std::vector. If you use assignment like you did at the end, you most likely will not get what you want, as it's only going to copy items up until the first zero byte (as strings are null terminated).

#include <iostream>
#include <iomanip>
#include <vector>

typedef unsigned char byte;

int main()
{
   byte payload[8] = { '1', '2', '3', '4', 0, 0, 0, 0 };

   for(int x = 0; x < 10; x++) {
      byte* p = reinterpret_cast<byte*>(&x);
      std::vector<byte> bv(payload, payload + (sizeof(payload) / sizeof(payload[0])));
      std::copy(p, p+sizeof(int), bv.begin() + 4);

      // Display vector to screen.
      for(std::vector<byte>::iterator i = bv.begin(); i != bv.end(); i++)
          std::cout << std::setfill('0') << std::setw(2) <<
                       std::hex << (int)*i << " ";
      std::cout << std::endl;
   }
   return 0;
}



Results in:

31 32 33 34 00 00 00 00
31 32 33 34 01 00 00 00
31 32 33 34 02 00 00 00
31 32 33 34 03 00 00 00
31 32 33 34 04 00 00 00
31 32 33 34 05 00 00 00
31 32 33 34 06 00 00 00
31 32 33 34 07 00 00 00
31 32 33 34 08 00 00 00
31 32 33 34 09 00 00 00

I'm not sure if this is what you were looking for or not.
Was This Post Helpful? 1
  • +
  • -

#3 adamj2  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 02-June 08

Re: Help with using substr to extract data of type byte

Posted 20 June 2008 - 05:33 AM

View Postperfectly.insane, on 19 Jun, 2008 - 06:41 PM, said:

It's probably not a very good idea to use a std::string for this purpose. While you can technically do it if you allocate the string prior and use std::copy to copy the items into the string, there is little advantage here over std::vector. If you use assignment like you did at the end, you most likely will not get what you want, as it's only going to copy items up until the first zero byte (as strings are null terminated).

#include <iostream>
#include <iomanip>
#include <vector>

typedef unsigned char byte;

int main()
{
   byte payload[8] = { '1', '2', '3', '4', 0, 0, 0, 0 };

   for(int x = 0; x < 10; x++) {
      byte* p = reinterpret_cast<byte*>(&x);
      std::vector<byte> bv(payload, payload + (sizeof(payload) / sizeof(payload[0])));
      std::copy(p, p+sizeof(int), bv.begin() + 4);

      // Display vector to screen.
      for(std::vector<byte>::iterator i = bv.begin(); i != bv.end(); i++)
          std::cout << std::setfill('0') << std::setw(2) <<
                       std::hex << (int)*i << " ";
      std::cout << std::endl;
   }
   return 0;
}



Results in:

31 32 33 34 00 00 00 00
31 32 33 34 01 00 00 00
31 32 33 34 02 00 00 00
31 32 33 34 03 00 00 00
31 32 33 34 04 00 00 00
31 32 33 34 05 00 00 00
31 32 33 34 06 00 00 00
31 32 33 34 07 00 00 00
31 32 33 34 08 00 00 00
31 32 33 34 09 00 00 00

I'm not sure if this is what you were looking for or not.


Many thanks for the reply.

Yeah I managed to get those results above for when I appended my sequence numbers to my payload but want I want to do now is extract the sequence number part from 3132333401000000 etc, and then convert it back to an int. This would happen at the receiver and then the receiver can then compare this with the last received seq num int for validation purposes.

But not sure how to go about that extraction. Am trying to use substring but am getting errors from converting between strings and unsigned chars*'s/bytes etc.

Unless what youwere suggesting now makes it possible to extract using substr?

I am not sure if I am going wrong during the conversion of an int to an unsigned char or with the substr bit or both!

Sorry if I am not making what I am trying to do clear, am slightly confused myself!;

Have a sequence number of unsigned int = 1 etc...

Then convert this into a binary string (not sure the correct definition?) to be appended to my payload string (representing bytes).

As my sequence number is 4 bytes long, I want to extract the second half of the payload, ie the last 4 bytes which is my sequence number. I guess it will then be in 01000000 form, so then convert it back to 1, etc.

I am sligthly confused about definitions of binary string, string of unsigned chars, bytes, etc, but basically am trying to extract my 4 byte sequence number from a string representing other packet data.
Was This Post Helpful? 0
  • +
  • -

#4 adamj2  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 02-June 08

Re: Help with using substr to extract data of type byte

Posted 20 June 2008 - 05:58 AM

Another method I used to convert my integer to a binary string was:

i= 4 //this is the seq num length
byte *p;
p= new byte [i];
memcpy (p, &seq),i) ;

which gave same 01 00 00 00 I think anyway

I basically just want to append that transformation onto the payload string and the extract the sequence number back from it (ie to emulate the receiver), then convert back to an integer.

The payload can be anything, just an empty variable etc..I dont mind how its done as long as I can just use substr to extract the 4 byte sequence number from something!

How should I do that??!

The sequence number will be appended onto an extended char array. I think this is different to a normal string as I am having trouble
with substr as it just says conversion problems between const char* and string etc etc...



Thanks!

This post has been edited by adamj2: 20 June 2008 - 10:18 AM

Was This Post Helpful? 0
  • +
  • -

#5 perfectly.insane  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 70
  • View blog
  • Posts: 644
  • Joined: 22-March 08

Re: Help with using substr to extract data of type byte

Posted 20 June 2008 - 04:42 PM

If you wanted to get the integer, you could probably do:

int i = *(reinterpret_cast<int*>(&bv[4]));
Was This Post Helpful? 0
  • +
  • -

#6 adamj2  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 02-June 08

Re: Help with using substr to extract data of type byte

Posted 21 June 2008 - 09:17 AM

View Postperfectly.insane, on 20 Jun, 2008 - 04:42 PM, said:

If you wanted to get the integer, you could probably do:

int i = *(reinterpret_cast<int*>(&bv[4]));



Excellent, that does work, many thanks for that.

int i = *(reinterpret_cast<int*>(&temp[4])); extracts the integer number 7 correctly from the 4 byte temp char array of 07 00 00 00. (temp is of type: unsigned char temp[4] )

Before that conversion to the integer part I would now like to try and transfer the value of temp (07000000) to a string (ie an extended char array) that may contain other data already (or just empty if simpler), and then extract the 07000000 part from that string using substr and THEN convert it to the integer as with the code you gave me.

The whole point of that is just to emulate appending the sequence number to a payload at the sending end and then extracting that sequence number again at the receiver.

Any example code you could kindly provide to demonstrate?! Thanks alot.

This post has been edited by adamj2: 21 June 2008 - 09:25 AM

Was This Post Helpful? 0
  • +
  • -

#7 perfectly.insane  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 70
  • View blog
  • Posts: 644
  • Joined: 22-March 08

Re: Help with using substr to extract data of type byte

Posted 21 June 2008 - 01:22 PM

Well, looking at your original code, here is the main problem (as stated in my first post):

// This won't work, as you'll have a string that is 5 characters, not 8.
// std::string is not meant for working with binary data, but null terminated strings.
string str = payload;

// This will.  You will have to explicitly give the bounds.  There are other ways of injecting nulls into the string besides this one.
string str(payload, payload + 8);

// There are many ways of doing this: 
char* p = str.substr(4, 4).c_str();

// Copy it into as many buffers as desired
// Convert
int i = *(reinterpret_cast<int*>(somebuf)));


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1