Converting byte counted strings to null-terminated strings

Does anyone know an easy way to convert byte-counted strings to null-terminated strings I'm trying to pass a filename as an argument in a custom made worksheet function defined in an xll created in C++ 2005 express. I think the problem I'm encountering is that excel is passing a byte-counted string and the function in C++ needs a null-terminated string. So it comes down to finding an easy way to convert byte counted to null-terminated in my C++ code. Any ideas are appreciated. Thanks.

Answer this question

Converting byte counted strings to null-terminated strings

  • Timbo

    It would help with an example.  Do you get "5brian" for instance

    I'm assuming that you can rely on the first real character being a non-digit. e.g. we won't try to encode "5" as "15".

    Case 1: you're getting null terminated strings ("5brian\0") (Check for this:  this is still a possibility; the length header is just an optimizaton.)

    char* pos = thestring;
    while( isdigit( *pos ) )  // walk past the digits
    {
       pos++;
    }
    char* newstring = strdup( pos ); // later, free this.

    Case 2: no null terminated string (i.e. the value of the character after the n in "5brian" is undefined.)

    int len = atoi( thestring );
    assert( len >= 0);  // negative values disallowed
    char* newstring = (char*)malloc( len * sizeof(char) + 1); // later, free this
    if( len > 0 )
    {
      while( isdigit( *pos ) ) // walk past the digits
      {
         pos++;
      }
      strcpy( newstring, pos, len ); // copy over the number of characters specified
    }
    newstring[len] = '\0';  // add null-terminator

    Brian


  • iping

    One approach is to first extract the byte count, then traverve to the first element in the string.

    Kuphryn


  • Paul_P

    I'm trying to basically trim all the numbers off of the start of a char * type variable. I know the full path name will never start with a number, so if I could just trim all the numbers off the beginning of a char * object, that should work. I just don't know how to do that. Somehow, even though I've included string.h in my program, various string functions don't seem to work. It may be because the xll file is created as a win32 console application. I'm not sure. Any further ideas Thanks.
  • Converting byte counted strings to null-terminated strings