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 that I have created, which is just a group of numbers. ie choose 12345678 which got converted to something like 32 33 34 35 etc for byte format..
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.
ie my payload looks like 32 33 34 35 00 01 00 00
and I want to extract 00 01 00 00 from this and then convert this back to my original integer.
I am trying to use the substr command but am not just trying to extract data from a string, rather a 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]
}