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
  #11 (permalink)  
Old March 30th, 2004, 02:44 PM
Authorized User
 
Join Date: Mar 2004
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Ok Update, I got past that problem and am to this point:

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

int main()
{ 
    char RawPacket[200];
    int INSLength;
    fstream Log("Log.txt",ios::in);
    fstream Parse("Parsed.txt",ios::out);
    while(!Log.eof()) {
    Log.getline(RawPacket,150);
}
    Parse << RawPacket[00] << RawPacket[01] << " "
          << RawPacket[02] << RawPacket[03] << " "
          << RawPacket[04] << RawPacket[05] << " "
          << RawPacket[06] << RawPacket[07] << " "
          << RawPacket[8] << RawPacket[9] << endl
          << RawPacket[10] << RawPacket[11] << endl;
    INSLength = RawPacket[8,9];
    Log.close(); 
    Parse.close();

return 0;
}
The output is now:

48 40 20 00 39
40


The 4th Byte is stored in "INSLength" so now I have to figure out how to use that to output the right amount of bytes with 16bytes per line.

Thanks for all the help so far as you can see it is paying off. :)

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

Update, I just found out something:

The Data will not be in this format:

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
With A White space between each Byte, It will in this format without white space between each byte:

Code:
4840200039407F11237C57BDA3DB80D4B5D4859A3B323B2C319024B02301910B1E79024182070006420001B5F90333420003620D006708F7EE7E8A4C76CC129020
So far with the updated code I have been able to parse the first 6 bytes into this format:

48 40 20 00 39
40

Now as I said in my last post, to get the correct # of bytes into 16 byte line like this:

Code:
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
I cannot do these bytes like I did the ones above, because unlike those bytes these bytes are never the same amount, so that is why I had to store the packet byte length, so now I need to figure out how to use it.
Reply With Quote
  #13 (permalink)  
Old March 30th, 2004, 03:26 PM
Authorized User
 
Join Date: Feb 2004
Posts: 76
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Let me get this straight: Is your input file like you showed in
your original post: 2-hex-digit byte values separated by spaces?
Or is it just a continuous stream of 2-hex-digit byte values?

Looks like you have the first thing (but my previous post gave
index values assuming the second).

Now, for the program:

You have the following
[code]
    while(!Log.eof()) {
    Log.getline(RawPacket,150);
}

Now, I have no way of knowing this, but if the input file has a
newline at the end of the bytes, then the first time through the
loop, getline() returns after the newline is encountered, so the
eof flag is not set, and it goes through the loop again (which
probably overwrites the first byte in the buffer with '\0').

I suggest that you declare RawPacket to have a size greater than
the maximum file size (which is not too large, since there can
be no more than 255 bytes in the body).

Then don't do the loop.

The place where you have real work remaining is the following:

Code:
    INSLength = RawPacket[8,9];
Now RawPacket is a one-dimensional array, so the thing in the []
brackets must be an integer.

In C (and C++) the comma is a sequencer: so the expression "8,9" has
value equal to 9.

You must use the two ascii values to calculate a numerical value
for the length (I pretty much indicated how you might do such a
thing in a previous post.)

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

Sorry: looks like you have the second; I was working from the first.

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

Ok Dave I understand what you are saying, but:

#1 here is my new code that is doing what I just posted it is doing:

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

int main()
{ 
    char RawPacket[227];                               //make Array for Raw packet Bytes
    int INSLength;                                     //Make Int Variable for Packet Length Byte
    fstream Log("Log.txt",ios::in);                    //Open "Log.txt" For Reading
    fstream Parse("Parsed.txt",ios::out);              //Open "Parsed.txt" for Writing
    Log.getline(RawPacket,227);                        //Get Packet String
    INSLength = RawPacket[8,9];                        //Get Packet Length Byte and store in "INSLength"
    Parse << RawPacket[0] << RawPacket[1] << " "       //Output "48"
          << RawPacket[2] << RawPacket[3] << " "       //Output "40"
          << RawPacket[4] << RawPacket[5] << " "       //Output "20"
          << RawPacket[6] << RawPacket[7] << " "       //Output "00"
          << RawPacket[8] << RawPacket[9] << endl      //Output "39"
          << RawPacket[10] << RawPacket[11] << endl;   //Output "40"
                                                       //Output looks like this:
                                                       //48 40 20 00 39
                                                       //40
    Log.close();                                       //Close Log.txt
    Parse.close();                                     //Close Parsed.txt

return 0;
}
Now I debugged the storage of the packet length by outputting it to screen and when I did so it outputted "39" with the multi demensional array I used.

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

I tried to respond, but the response didn't show up. So I'll try
again.

your statement
Code:
INSLength = RawPacket[8,9];
sets INSLength to the value stored in RawPacket[9].

For your file, the character is '9', which is equal to 0x39.

It just happens that the length of the body is 0x39 for your
file.

Please do the following:

Temporarily change your Log.txt (save a copy of the original)
so that RawPacket[ 8] will be '4' and RawPacket[ 9] will be '0'.

This means that there should be 40 (hexadecimal) bytes (64 decimal)
in the body.

Now print out the value of INSLength as you did before.

You will see that INSLength is equal to 48 decimal (30 hexadecimal).
This is because RawPacket[ 9] will be 0x30 (the value of '0').

You are reading characters. You must operate on the two characters
in RawPacket[ 8] and RawPacket[ 9] to get the numerical value
represented by these digits.

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

Ok Dave, I understand what you are saying now.

Do you have any suggestions on how I could do that?

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

Well, you could probably find code floating aroung the web somewhere, or
figure how to use some C standard library functions, but it's easy to do,
and, once you get the picture, it will be with you forever.

First of all, not all systems use ASCII representations (although, these
days, almost all do), so I wouldn't recommend anything that has ASCII
programmed into the algorithm.

I will present what I think is a simple way to do it. You can try to
think of more elegant methods if you are really interested.

There are three cases of characters for hex digits that we must consider:
'0' to '9'
'a' to 'f'
'A' to 'F'

(since hex digits may be represented by either upper- or lower-case a-f)

I'll give you words; you write code:

If the ASCII character is
(greater than or equal to '0') and (less than or equal to '9') then

the decimal value can be obtained by subtracting the character '0'.

You can see that '0' becomes 0,
                 '1' becomes 1,
                  etc.

Now if the ascii character is
(greater than or equal to 'a') and (less than or equal to 'f') then

the decimal value can be found by subtracting 'a' and adding 10.

That is 'a' becomes 10
         'b' becomes 11
           etc

You could convert 'A' - 'F' to lower case (using standard library function
tolower()), then use the 'a' - 'f' test, or you could continue as above

If the ascii character is
(greater than or equal to 'A') and (less than or equal to 'F') then

the decimal value can be found by subtracting 'A' and adding 10.

That is 'A' becomes 10
         'B' becomes 11
           etc.

So you could write a function, say atohex(), to convert as shown.

Then you could have something like
Code:
INSLength = (16 * atohex(RawPacket[ 8])) + atohex(RawPacket[ 9];
Dave
Reply With Quote
  #19 (permalink)  
Old March 30th, 2004, 04:59 PM
Authorized User
 
Join Date: Feb 2004
Posts: 76
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Oops --- dropped a parenthesis
Code:
INSLength = (16 * atohex(RawPacket[ 8])) + atohex(RawPacket[ 9]);
Dave
Reply With Quote
  #20 (permalink)  
Old March 30th, 2004, 05:20 PM
Authorized User
 
Join Date: Mar 2004
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Dave, do you have IM?

I would like to talk with you about this in real time if possible

Thanks

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.