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
  #21 (permalink)  
Old March 30th, 2004, 05:27 PM
Authorized User
 
Join Date: Feb 2004
Posts: 76
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Sorry.

As a matter of fact, I don't usually spend so much time online (not continuously, anyhow). I probably won't be around much longer today, but will check back from time to time.

Dave
Reply With Quote
  #22 (permalink)  
Old March 31st, 2004, 02:12 PM
Authorized User
 
Join Date: Mar 2004
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Finally got past the Problem with Calculating for the Correct length thanks to Dave:

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

void TimeHeader(),StoreINSLength();

struct tm *area;
char *tzstr = "TZ=EST5EDT";
char RawPacket[227];
int INSLength;

int main()
{ 
    fstream Log("Log.txt",ios::in);
    fstream Parse("Parsed.txt",ios::out|ios::app);
    Log.getline(RawPacket,227);

    StoreINSLength();
    TimeHeader();
    Parse << asctime(area) << endl;
    Parse << "> " << RawPacket[0] << RawPacket[1] << " "
          << RawPacket[2] << RawPacket[3] << " "
          << RawPacket[4] << RawPacket[5] << " "
          << RawPacket[6] << RawPacket[7] << " "
          << RawPacket[8] << RawPacket[9] << endl
          << "< " << RawPacket[10] << RawPacket[11] << endl
          << "> ";
    Log.close();
    Parse.close();

return 0;
}
/***************************************
 Display Time Header
****************************************/
void TimeHeader()
{
   time_t t;

   putenv(tzstr);
   tzset();

   t = time(NULL);
   area = localtime(&t);
}
/***************************************
 Store INS Length
****************************************/
void StoreINSLength()
{
    INSLength = (RawPacket[0]-'0')*10+(RawPacket[1]-'0');
    cout << INSLength << endl;
}
Reply With Quote
  #23 (permalink)  
Old March 31st, 2004, 02:50 PM
Authorized User
 
Join Date: Mar 2004
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Ok, Now I am up to this problem:

I need to turn this:

7F11237C57BDA3DB80D4B5D4859A3B323B2C319024B0230191 0B1E79024182070006420001B5F90333420003620D006708F7 EE7E8A4C76CC12

Which is in the array "RawPacket" into 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
and display it on screen.

Also the Length of the bytes that I want to display are in "INSLength" in HEX.

Any Help?

Reply With Quote
  #24 (permalink)  
Old April 1st, 2004, 09:22 AM
Authorized User
 
Join Date: Feb 2004
Posts: 76
Thanks: 0
Thanked 0 Times in 0 Posts
Default

If you are still dealing with the original input file you told me about,
you should use bytes 8 and 9 for the calculation of INSLength.
Furthermore, these bytes would be the hexadecimal values, so, as
in my previous post:

Code:
INSLength = (16 * atohex(RawPacket[ 8])) + atohex(RawPacket[ 9]);
Where atohex() is a function you write to convert an ascii hex digit
char to his hex value. I told you one way to do this.

Now, as for displaying the body of the file. Again, if you are
still using the original input file, you have following things to
consider:

The first byte you want to print out starts at RawPacket[12].

You will print out a total of INSLength bytes.

Each byte is printed by taking the next two characters from RawPacket
directly to the output file.

You want to print 16 bytes on a line, separated by a single space
character.

Suppose you didn't have any limit on the number of bytes on a
line. Then you would make a loop that does the following:

Starting at RawPacket[12]
  transfer two characters from RawPacket to the output file.
  transfer a space character (' ') to the output file.
  continue until INSLength bytes have been transferred.

How would you do this?

You could have two "counters".
One counter starts with a value of 12, and is used as an
index into RawPacket for the next input character. That
is, it is incremented each time you get a character.

Another counter starts at 0, and is incremented every time the
second input character of each byte is transferred. You
could make a loop with this counter starting at 0 and ending
at (what --- you can figure it out). Now every time you
increment this counter, put a space character to the output
file, so that the output bytes will be separated as required.

Now, the way that I have described is not the only way to approach
the problem; maybe not the most elegant way in some people's
opinion, but it is a way. If you read this and you think
of a better way, go for it. But do something that gets you to
this point.

Please, please, please do this, and get it exactly right before
you continue.




Now, you need to put an endl to the output file after each 16
output bytes to break the lines. A straightforward way is to
make a nested loop that counts from 0 to 15 (increments every
time the outer loop counter is incremented). Whenever the 16th
output byte is printed, then instead of a space, put an endl
to the output file.

Try this, and you are almost home. Still a few details to clean
up.


Dave
Reply With Quote
  #25 (permalink)  
Old April 1st, 2004, 11:27 AM
Authorized User
 
Join Date: Mar 2004
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Dave:

How is this for the atohex function:

Code:
BYTE atohex(char c)

{
    if(c>='0'&&c<='9') return c-'0';
    if(c>='a'&&c<='f') return c-'a'+10;
    if(c>='A'&&c<='F') return c-'A'+10;
}
Reply With Quote
  #26 (permalink)  
Old April 1st, 2004, 02:39 PM
Authorized User
 
Join Date: Mar 2004
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Ok Dave Here is the lastest Version. I finally got it to do the rest of the Packet. It will take this:

4840200039407F11237C57BDA3DB80D4B5D4859A3B323B2C31 9024B02301910B1E79024182070006420001B5F90333420003 620D006708F7EE7E8A4C76CC129020

and format it into this:

Code:
Thu Apr 01 13:39:11 2004
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
As you can see, I haven't as of yet been able to figure out how to get it to 16 bytes per line, Maybe you can check my code and see where I am going wrong.

Thanks:

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

void TimeHeader();

struct tm *area;
char *tzstr = "TZ=EST5EDT";
char RawPacket[227];

int main()
{ 
    int x = 0;
    int y = 0;
    int z = 0;
    int INSLength;
/**************************************
 Open Files for Reading and Writing
 *************************************/
    fstream Log("Log.txt",ios::in);
    fstream Parse("Parsed.txt",ios::out);
/**************************************
 Get Packet From Stream
 *************************************/
    Log.getline(RawPacket,227);
/**************************************
 Get and Display Time/Date Header
 *************************************/
    TimeHeader();
    Parse << asctime(area);
/**************************************
 Get Packet Length Byte
 *************************************/
INSLength = (RawPacket[8]-'0')*16+(RawPacket[9]-'0'); 
/*************************************
 Display 5 Byte Packet Header
 ************************************/
      do {
            Parse << RawPacket[x] << RawPacket[x+1] << " ";
            x++; x++;y++;
        }while (y!=5);

/*************************************
 Display Packet Acknowledgement Byte
 ************************************/
    Parse << endl << RawPacket[10] << RawPacket[11] << endl;
/*************************************
 Display Rest Of Packet Bytes Based on Length
 ************************************/
    x = 12;y = 0;
      do {
            Parse << RawPacket[x] << RawPacket[x+1] << " ";
            x++; x++;y++;
        }while (y!=INSLength);
/**************************************
 Display 2 Byte Packet Response
 *************************************/
    Parse << endl << RawPacket[x] << RawPacket[x+1] << " " << RawPacket[x+2] << RawPacket[x+3] << " ";
/**************************************
 Close Files
 *************************************/
    Log.close();
    Parse.close();

return 0;
}
/***************************************
 Display Time Header
****************************************/
void TimeHeader()
{
   time_t t;

   putenv(tzstr);
   tzset();

   t = time(NULL);
   area = localtime(&t);
}
Reply With Quote
  #27 (permalink)  
Old April 1st, 2004, 02:44 PM
Authorized User
 
Join Date: Feb 2004
Posts: 76
Thanks: 0
Thanked 0 Times in 0 Posts
Default

What if this function is called with, say 'Z' as an argument? You must
take all possible cases into account. So, for example, after your
last if statement, you could put
Code:
else {
  cout << "Error in atohex(), illegal argument <" << c << ">" << endl;
  return -1; // or return anything that the calling routine can test
}
Dave
Reply With Quote
  #28 (permalink)  
Old April 1st, 2004, 03:17 PM
Authorized User
 
Join Date: Mar 2004
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Oh sorry Dave my fault. I forgot to mention that the Data I am getting is coming in through a Serial Port from another Device. The Data will be from 0 - F, not beyond that,.

Sorry, forgot to mention that.

Reply With Quote
  #29 (permalink)  
Old April 1st, 2004, 07:59 PM
Authorized User
 
Join Date: Feb 2004
Posts: 76
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Nevertheless, your function should take care of all possibilities.

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.