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

New Topic/Question
Reply




MultiQuote





|