View Single Post
  #2 (permalink)  
Old March 7th, 2004, 03:52 PM
davekw7x davekw7x is offline
Authorized User
 
Join Date: Feb 2004
Posts: 76
Thanks: 0
Thanked 0 Times in 0 Posts
Default

First of all, your statement that "the string is not ended with \0" is incorrect. In C there is no "string" data type. A string constant, defined by something between pairs of " marks has a number of bytes equal to one more than the number of characters between the " marks.

I assume that BYTE is "#define"d as a char.

Then your statement
Code:
BYTE deliminator[] = "PORT 10,247,56,67,9,46\r\n";
defines a char array that has 25 bytes (the 24 chars between the
" marks and an additional byte that is initialized to '\0'.


How do I know this?

Try the following:

Code:
#include <stdio.h>
#include <string.h>
#define BYTE char


int main()
{
  BYTE mystring[] = "PORT 10,247,56,67,9,46\r\n";
  int i;

  printf("\n\n");

  printf("  sizeof(BYTE) = %d\n", sizeof(BYTE));
  printf("  sizeof(mystring) = %d\n", sizeof(mystring));
  printf("  strlen(mystring) = %d\n", strlen(mystring));

  printf("\n\n");
  for (i = 0; i < sizeof(mystring); i++) {
    printf("<%02X> ", mystring[i]);  }
  printf("\n\n");

  return 0;
}
Now as to your program: Your first call to strtok returns a pointer to the string "10", since it has skipped over the "PORT ". Successive calls return pointers to strings "247", "56", etc.

Note that i is equal to 3 when the pointer to "9" is returned, and i is equal to 4 when the pointer to "46" is returned.

Now you want to convert the strings to integers, so you could use sscanf(), atoi(), or other functions to do so.

 The statement
Code:
hi_byte = *str_ptr;
sets hi_byte equal to the ASCII value of the first digit. Not what you want.

I say this, because your function make16() operates on two BYTEs.

By the way, what is an Int8 ? I hope it's not supposed to be an eight-bit integer, since your function must return a 16-bit value.

Dave

Dave
Reply With Quote