| | |
trying to use substr to extract data from a string of bytes, help would be great
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jun 2008
Posts: 16
Reputation:
Solved Threads: 0
trying to use substr to extract data from a string of bytes, help would be great
0
#1 Jun 19th, 2008
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!
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");
}
//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";
}•
•
Join Date: Jan 2008
Posts: 3,837
Reputation:
Solved Threads: 503
Re: trying to use substr to extract data from a string of bytes, help would be great
0
#2 Jun 19th, 2008
•
•
•
•
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"); } //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"; }
C++ doesn't have a byte type. Do you mean unsigned char? And hex.h is something you wrote yourself, correct? So you are trying to extract the "0001" from the string using substr, but that doesn't work. But why doesn't it work? Is it that you need the 0, 0, 0, and 1 stored as integers and you are getting the ASCII values for 0, 0, 0, and 1 instead?
Last edited by VernonDozier; Jun 19th, 2008 at 11:21 pm.
•
•
Join Date: Jun 2008
Posts: 16
Reputation:
Solved Threads: 0
Re: trying to use substr to extract data from a string of bytes, help would be great
0
#3 Jun 20th, 2008
•
•
•
•
C++ doesn't have a byte type. Do you mean unsigned char? And hex.h is something you wrote yourself, correct? So you are trying to extract the "0001" from the string using substr, but that doesn't work. But why doesn't it work? Is it that you need the 0, 0, 0, and 1 stored as integers and you are getting the ASCII values for 0, 0, 0, and 1 instead?
Yeah I think I am probably trying to extract from a string of unsigned chars.
Basically all the data from the packets, such as payload, header length's etc are appended to the payload in a long binary string. The specific data is then extracted using the substr command.
I want to use a 4 byte (32 bit) sequence number. When I convert my sequence number, of say 1, to byte format (unsigned char etc?) I use the following code:
unsigned char temp[4];
for( int i=0;i<4;i++)
temp[i]= '\0';
memcpy(temp,&seq,4);
which gives me 01000000 for number 1 which is 4 bytes (they give 2 characters per byte for some reason)...etc...
I then append this to my payload and then want to extract the sequence number from the correct place from this string, and then convert it back to an integer, for comparison purposes. Ie this would take place at a receiver.
But having problems using substr for this. Because the only examples of substr I have seen extract from a string str = "hello" etc...and not from a binary string of unsigned chars.
Thanks.
p.s yeah hex.h was put in by myself but not sure it is being used in this code though, just copied from another header.
An alternative method I was using to convert the integer into a binary string was:
(i think they byte comes from a library somewhere i have then)
i= 4 //seq num length
byte *p;
p= new byte [i];
memcpy (p, &seq, i) ;
maybe it is during this initial converstion I am doing something wrong, rather than just using &seq im memcpy maybe I should be doing something else?
Last edited by adamj2; Jun 20th, 2008 at 9:22 am.
•
•
Join Date: Jan 2008
Posts: 3,837
Reputation:
Solved Threads: 503
Re: trying to use substr to extract data from a string of bytes, help would be great
0
#4 Jun 20th, 2008
•
•
•
•
which gives me 01000000 for number 1 which is 4 bytes (they give 2 characters per byte for some reason)...etc...
C++ Syntax (Toggle Plain Text)
2F
which is the string representation of the hexadecimal representation of 47.
This earlier quote:
•
•
•
•
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..
The ASCII value of '1' is 0x31, the ASCII value of '2' is 0x32, the ASCII value of '3' is 0x33, so it appears like you are dealing with ASCII values but are one off.
•
•
•
•
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
I'm not sure we are using the same definition of "binary string". In your definition, does the number 1 get stored as one byte of 0x1 or as one byte of 0x31 (ASCII representation of '1') or as two bytes (one byte of 0x30, which is ASCII representation of '0', followed by one byte of 0x31, which is ASCII representation of '1')? Do you ever have bytes that are not in the range of 0x30 through 0x39 or 0x41 through 0x46 (Ascii representations of '0' through '9' and 'A' through 'F')?
Last edited by VernonDozier; Jun 20th, 2008 at 10:28 am.
•
•
Join Date: Jun 2008
Posts: 16
Reputation:
Solved Threads: 0
Re: trying to use substr to extract data from a string of bytes, help would be great
0
#5 Jun 20th, 2008
•
•
•
•
Who are they? Is this a function somewhere? The fact that you are given 2 characters per byte (what "gives" you this, by the way?) suggests to me that there is some function that you pass a number from 0 to 255, say 47, and you are returned the following two character string:
C++ Syntax (Toggle Plain Text)
2F
which is the string representation of the hexadecimal representation of 47.
This earlier quote:
suggests that you 1 is being converted to "32", 2 is converted to "33", 3 is converted to "34", etc.
The ASCII value of '1' is 0x31, the ASCII value of '2' is 0x32, the ASCII value of '3' is 0x33, so it appears like you are dealing with ASCII values but are one off.
Why is this not 00 00 00 01?
I'm not sure we are using the same definition of "binary string". In your definition, does the number 1 get stored as one byte of 0x1 or as one byte of 0x31 (ASCII representation of '1') or as two bytes (one byte of 0x30, which is ASCII representation of '0', followed by one byte of 0x31, which is ASCII representation of '1')? Do you ever have bytes that are not in the range of 0x30 through 0x39 or 0x41 through 0x46 (Ascii representations of '0' through '9' and 'A' through 'F')?
I have been given some code for an new packet header processing. And all the data is appended to the payload as one long string. I am trying to add a seq num bit to it and just testing it out in a single prog. first.
These are some methods that are used for conversion to the format representing the data as strings of bytes (or whatever the correct definition is?)
i= this->_payload.length();
byte *p;
p= new byte [i];
memcpy (p, this->_payload.c_str(),i) ;
int ULE_SID=4444;
int uli_netval=htonl(ULE_SID); //convert host to network for integers
unsigned char temp[4];
for( int i=0;i<4;i++)
temp[i]= '\0';
memcpy(temp,&uli_netval,4);
I have tried adapting these methods to convert my seq number into a binary string of 4 bytes.
for some reason for say seq num 0001 I always get 01 00 00 00 from those methods.
I then want to append my 4 byte seq num to a payload (which is a string of data...ie header length first 4 bytes, seq num is next 4 bytes), I have just created a imaginery one of 12345678:
byte payload[8] = {"1234567"}; >> but maybe that is the wrong way to go about it?
I dont really know what is going on with the bytes and how they are represented etc so sorry am not much help with providing the necessary info. WIll just have to go back to my supervisor at Uni for further help and to find out exactly how the payload data is stored. I know its a string, and the seq num should be represented over 4 bytes, so guess it is over 4 bytes as unsigned chars.
Re: trying to use substr to extract data from a string of bytes, help would be great
0
#6 Jun 20th, 2008
•
•
•
•
I have been given some code for an new packet header processing. And all the data is appended to the payload as one long string. I am trying to add a seq num bit to it and just testing it out in a single prog. first.
These are some methods that are used for conversion to the format representing the data as strings of bytes (or whatever the correct definition is?)
i= this->_payload.length();
byte *p;
p= new byte [i];
memcpy (p, this->_payload.c_str(),i) ;
int ULE_SID=4444;
int uli_netval=htonl(ULE_SID); //convert host to network for integers
unsigned char temp[4];
for( int i=0;i<4;i++)
temp[i]= '\0';
memcpy(temp,&uli_netval,4);
I have tried adapting these methods to convert my seq number into a binary string of 4 bytes.
for some reason for say seq num 0001 I always get 01 00 00 00 from those methods.
I then want to append my 4 byte seq num to a payload (which is a string of data...ie header length first 4 bytes, seq num is next 4 bytes), I have just created a imaginery one of 12345678:
byte payload[8] = {"1234567"}; >> but maybe that is the wrong way to go about it?
I dont really know what is going on with the bytes and how they are represented etc so sorry am not much help with providing the necessary info. WIll just have to go back to my supervisor at Uni for further help and to find out exactly how the payload data is stored. I know its a string, and the seq num should be represented over 4 bytes, so guess it is over 4 bytes as unsigned chars.
http://www.cppreference.com/cppbitset/all.html
•
•
Join Date: Jun 2008
Posts: 16
Reputation:
Solved Threads: 0
Re: trying to use substr to extract data from a string of bytes, help would be great
0
#7 Jun 20th, 2008
Thanks for responces, will check out that link in a bit.
Sorry for not making my questions clear.
I now know it is a string of chars that I want to append my 4 byte sequence number to.
I then want to extract the sequence number from that string of chars.
At the moment am just stuck on how to extract from a string of chars rather than a normal string. Any example code will be great. Thanks.
Sorry for not making my questions clear.
I now know it is a string of chars that I want to append my 4 byte sequence number to.
I then want to extract the sequence number from that string of chars.
At the moment am just stuck on how to extract from a string of chars rather than a normal string. Any example code will be great. Thanks.
•
•
Join Date: Jan 2008
Posts: 3,837
Reputation:
Solved Threads: 503
Re: trying to use substr to extract data from a string of bytes, help would be great
0
#8 Jun 20th, 2008
•
•
•
•
Thanks for responces, will check out that link in a bit.
Sorry for not making my questions clear.
I now know it is a string of chars that I want to append my 4 byte sequence number to.
I then want to extract the sequence number from that string of chars.
At the moment am just stuck on how to extract from a string of chars rather than a normal string. Any example code will be great. Thanks.
Give this a try. Basically you have four integers from 0 to 255 (i.e. bytes) that are stored as unsigned chars (C++'s equivalence of a byte). I have it so the unsigned chars are converted to chars, then added to your string. The last four characters are then taken off of the back of the payload string, converted back from chars to to unsigned chars, then converted to integers so they can be displayed. Line 24 is there to prove that they are truly extracted when they are later extracted using substr. Line 1 is to define byte as unsigned char. Lines 7 to 11 is to make sure that a char is represented as one byte in the compiler. If it's not, this won't work. I think (not 100% positive) that some compilers set aside more than one byte for a char.
C++ Syntax (Toggle Plain Text)
typedef unsigned char byte; #include <iostream> using namespace std; int main () { if (CHAR_MAX - CHAR_MIN != 255) { cout << "char does not take one byte of storage. Exiting." << endl; exit(1); } string payload = "Hello world"; byte sequence[4]; sequence[0] = 134; sequence[1] = 6; sequence[2] = 200; sequence[3] = 234; for (int i = 0; i < 4; i++) { char aChar = sequence[i] + CHAR_MIN; payload = payload + aChar; sequence[i] = 0; // zero out sequence array } string charSequence = payload.substr(payload.length() - 4, 4); for (int i = 0; i < 4; i++) { sequence[i] = charSequence[i] - CHAR_MIN; int number = sequence[i]; cout << "Sequence byte " << i << " of 4 = " << number << endl; } return 0; }
Re: trying to use substr to extract data from a string of bytes, help would be great
0
#9 Jun 20th, 2008
I think you're right on that one. I read something interesting about why chars have different types and that some Operating Systems (if not compilers) will interpret chars differently.
Re: trying to use substr to extract data from a string of bytes, help would be great
1
#10 Jun 20th, 2008
>I think you're right on that one.
It depends on what you mean by "char". If you're talking about the char data type, it's always equivalent to one byte in C++, for whatever the definition of a byte is. I think Vernon is assuming that a byte always equates to an 8-bit entity, which is not true.
On the other hand, if you're talking about a logical character, such as with Unicode, then a single character could very well be represented by more than one octet[1].
[1] Which is the correct term for an 8-bit entity.
It depends on what you mean by "char". If you're talking about the char data type, it's always equivalent to one byte in C++, for whatever the definition of a byte is. I think Vernon is assuming that a byte always equates to an 8-bit entity, which is not true.
On the other hand, if you're talking about a logical character, such as with Unicode, then a single character could very well be represented by more than one octet[1].
[1] Which is the correct term for an 8-bit entity.
Last edited by Narue; Jun 20th, 2008 at 3:54 pm.
New members chased away this month: 4
![]() |
Other Threads in the C++ Forum
- Previous Thread: Loading image Borland c++ 5.5
- Next Thread: Array Names as Pointers
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll dynamiccharacterarray email encryption error file format forms fstream function functions game givemetehcodez graph gui homeworkhelp iamthwee ifstream input int java lib library lines list loop looping loops map math matrix memory newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sorting spoonfeeding string strings struct temperature template templates text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






