Wrox Programmer Forums
|
C++ Programming General discussions for the C++ language. For questions specific to Microsoft's Visual C++ variant, see the Visual C++ forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C++ Programming section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old March 29th, 2004, 07:45 PM
Authorized User
 
Join Date: Mar 2004
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Default Number String Question

Hi,

Nice forum you have here. I think I'm going to like it. Now onto my problem:

I am trying to manipulate a Byte string, but first taking the string and putting it into Memory, then Parsing it. The problem is I cant seem to find the correct way to use the array and find the bytes I am looking for.

The following is the data in the "Log.txt" file that is referenced in my code:

Code:
48 40 20 00 39 40 7F 11 23 7C 57 BD A3 DB 80 D4 B5 D4 85 9A 3B 32 3B 2C 31 90 24 B0 23 01 91 0B 1E 79 02 41 82 07 00 06 42 00 01 B5 F9 03 33 42 00 03 62 0D 00 67 08 F7 EE 7E 8A 4C 76 CC 12 90 20
Now, What I am trying to do is take those bytes and parse it into this:

Code:
48 40 20 00 39 
40 
7F 11 23 7C 57 BD A3 DB 80 D4 B5 D4 85 9A 3B 32 
3B 2C 31 90 24 B0 23 01 91 0B 1E 79 02 41 82 07 
00 06 42 00 01 B5 F9 03 33 42 00 03 62 0D 00 67 
08 F7 EE 7E 8A 4C 76 CC 12 
90 20
Then Write it to the "Parsed.txt" file also referenced in my code.

I have been trying to get this down for the last 2 weeks now. I have been able to read and write from the log.txt to the parsed.txt file, but I cant get the parsing of the bytes down correctly. I have read books and nothing seems to point me to what I am looking for. Hopefully someone on here can help me with me problem.

Below is my code:

Code:
#include <fstream>
#include <iostream>
#include <cstring>
using namespace std;

int main()
{ 
    char filedata[200];
    fstream obj1("Log.txt",ios::in|ios::binary);
    fstream obj2("Parsed.txt",ios::out);
    while(!obj1.eof()) {
    obj1.getline(filedata,200);
    obj2 << filedata << "\n";
}
    obj1.close(); 
    obj2.close();

return 0;
}


Reply With Quote
  #2 (permalink)  
Old March 30th, 2004, 11:35 AM
Authorized User
 
Join Date: Feb 2004
Posts: 76
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Well, like most programs, there are three parts to the assignment:

Input
Processing
Output

I don't understand any of them.

Input: What is the nature of the input file?
Is it a text file (ascii characters for the numbers, separated by spaces, all on a single line)?
(This file will be 94-96 bytes long, depending on whether there is a line feed and/or a carriage return at the end.)

Is it a binary file, with 65 bytes, each with the value you have shown.
(This file will be 65 bytes long.)

Or what?

Processing: What do you mean by "Parsing it"?
Do you mean: "get the first five bytes, then get one byte, then get 65 bytes, then get two bytes"?
Or do you mean: "get the first five bytes, then get a number of bytes determined by the integer value of the combined fourth and fifth bytes, then get two bytes"?

Or what?

Output: Do you mean "put out the hexadecimal values of the first five bytes on a line, separated by spaces, then put out the sixth byte on a separate line, then ..."?

Or what?


Dave
Reply With Quote
  #3 (permalink)  
Old March 30th, 2004, 12:25 PM
Authorized User
 
Join Date: Mar 2004
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you for the reply Dave:

Thank you for the Reply. Basically the format will always be the same regardless of what the bytes are. The bytes all come in as one string as I posted previously like this:

Code:
48 40 20 00 39 40 7F 11 23 7C 57 BD A3 DB 80 D4 B5 D4 85 9A 3B 32 3B 2C 31 90 24 B0 23 01 91 0B 1E 79 02 41 82 07 00 06 42 00 01 B5 F9 03 33 42 00 03 62 0D 00 67 08 F7 EE 7E 8A 4C 76 CC 12 90 20
Yellow = Header
Blue = Acknowledgement Byte
Red = Bytes in packet
Orange = Packet Response

With the exception of the first byte "48" the bytes will always be the same. Also one other important fact I left out before; The 4th byte of the header is the Byte Length which tells us how many bytes are in the Packet.

Now What I was hoping to do is this:
  1. Take first 5 byte header and put them on the line(Store 4th byte)
  2. Get next byte from string and put then on the line
  3. Then test the 4th byte I stored previously and allow that to determine how many bytes to get and store on the next line.
  4. Finally get last 2 bytes and store that

Also, I would like to point out that I thought about doing this byte by byte instead of in strings, but It would not work out. Instead of it coming out like this:

48 40

It would come out like this:

4
8
4
0

Maybe someone can help me out there as well.

Thank You
Reply With Quote
  #4 (permalink)  
Old March 30th, 2004, 01:12 PM
Authorized User
 
Join Date: Feb 2004
Posts: 76
Thanks: 0
Thanked 0 Times in 0 Posts
Default

You can't do it using strings, since strings work on characters with
certain rules that make it impossible to process files that have
white space and certain control characters.

I suggest you use obj1.read(filedata, sizeof(filedata)) to get the
entire file into the array. Note that since the maximum file size
will be something like 263 bytes (I think), you should declare the
filedata array to be larger than that.

After the obj1.read(), the number of bytes that were read can be
found by the gcount() function:

num_bytes = obj1.gcount();

(where num_bytes is an int)


Now, you have everything in the array, so you can write whatever
you want to obj2.

Does this help?

Regards,

Dave
Reply With Quote
  #5 (permalink)  
Old March 30th, 2004, 01:30 PM
Authorized User
 
Join Date: Mar 2004
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you for the suggestions Dave.

I use getline() now to read the whole string into memory and it comes out correctly. If I use read() I get a bunchy of ASCII Text after the string. So I will keep with getline() for now.



Reply With Quote
  #6 (permalink)  
Old March 30th, 2004, 01:59 PM
Authorized User
 
Join Date: Feb 2004
Posts: 76
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Ok, I am starting to see the light (I think). I thought the file was
binary bytes, not ASCII representations.

So, use getline to read the entire file into your buffer. (How large
must the buffer be? Something like 789-794 bytes, I think, since
each file entry takes three bytes (two for the two ASCII chars)
plus a space to separate successive file entries.)

So each entry occupies three bytes (the two ASCII characters plus a
space). The last byte may or may not have a space. There may or may
not be a carriage return after the last byte.

What is the index of the two characters in the buffer that tell you
the length of the body? In your example, the length is 39(hex) (the
fifth byte).
I think the two characters in filedata[12] and filedata[13] must
be converted to an int so that you will know the length. (For debug
purposes, do cout << filedata[12] << filedata[13].)

Nothing really fancy is needed, but you have to do a little work:
What you need is the integer value corresponding to the character
in filedata[12] and the integer value corresponding to the character
in filedata[13].

For example: int1 = filedata[12] - '0';
             int2 = filedata[13] - '0';

Since these are the digits of a hexadecimal number, the number of
bytes in the body is equal to 16*int1 + int2.

Now you still have to format the output appropriately:

Each byte is two chars plus a space
endl after the first five bytes
endl after the sixth byte
Then, for the body of the file, endl after each 16 bytes.

etc.

Dave
Reply With Quote
  #7 (permalink)  
Old March 30th, 2004, 02:09 PM
Authorized User
 
Join Date: Feb 2004
Posts: 76
Thanks: 0
Thanked 0 Times in 0 Posts
Default

A note: The reason that I originally thought that you are dealing
with a binary file is that you opened it with
Code:
    fstream obj1("login.txt",ios::in|ios::binary);
Why, since this is not a binary file? It won't do any harm here,
but is misleading to future code maintainers. (And this should
be a consideration for all programs.)

Dave
Reply With Quote
  #8 (permalink)  
Old March 30th, 2004, 02:15 PM
Authorized User
 
Join Date: Mar 2004
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Dave:

I tried what you said and it worked for the most part, but I ran into a problem when dealing with the array it will not let me use filedate or filedata[9], because it said it is illegal with base 8.

Reply With Quote
  #9 (permalink)  
Old March 30th, 2004, 02:19 PM
Authorized User
 
Join Date: Mar 2004
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Here is the code that cuases the 2 errors because of the illegal array use with base '8':

Code:
#include <fstream>
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;

int main()
{ 
    char filedata[200];
    fstream obj1("Log.txt",ios::in);
    fstream obj2("Parsed.txt",ios::out);
    while(!obj1.eof()) {
    obj1.getline(filedata,150);
    obj2 << filedata[00] << filedata[01] << " "
         << filedata[02] << filedata[03] << " "
         << filedata[04] << filedata[05] << " "
         << filedata[06] << filedata[07] << " "
         << filedata[08] << filedata[09] << " "
         << filedata[10] << filedata[11] << endl;
    obj2 << filedata[12] << filedata[13] << endl;
    obj2 << filedata << "\n";
}
    obj1.close(); 
    obj2.close();

return 0;
}
Reply With Quote
  #10 (permalink)  
Old March 30th, 2004, 02:37 PM
Authorized User
 
Join Date: Feb 2004
Posts: 76
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Numbers with leading '0' are considered to be octal!

change
Code:
<< filedata[08] << filedata[09] << " "
to
Code:
<< filedata[ 8] << filedata[ 9] << " "
I suggest that you use cout << to observe the bytes that you are writing.

Dave
Reply With Quote





Similar Threads
Thread Thread Starter Forum Replies Last Post
How to count the number of occurence of a string ? khb3283 C# 12 October 5th, 2008 11:10 PM
format-number on a string? dep XSLT 1 October 31st, 2006 01:24 PM
NUMBER OR STRING pallone XSLT 2 September 8th, 2006 08:10 AM
how to replace a string with another string/number crmpicco Javascript How-To 4 March 14th, 2005 12:59 PM
Converting a string to a number steve456 Javascript How-To 2 November 17th, 2003 06:01 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.