Thread: string length question

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    58

    string length question

    Hi everyone,
    just having a couple of question here. I had an unsigned char array, which suppose to store in hex format, which I usually print it out using %02X. I tried to do a strlen since it an char array, but I think the result was not wat I expected. So therefore I would like to check whether there is a way to caluated the length of the item store in the unsigned char array?

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Don't you know the length when you fill the array?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    58
    Hi vart,
    Actually I called the function through a dll file that was not coded by me. So therefore I wouldn't know the actual length of the output data.

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    I don't understand how it is possible to work with the array without knowledle about its length...
    What is the API you are using?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    58
    I'm using vc ver 6. so I suppose should be win32

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    No - I mean what is the function prototype you are using to fill the array,
    it should in some way indicate the array length used, or by returning this value or by putting some special stop symbol in the first unused element etc
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    If it's an actual array (a global one, maybe), you can use sizeof() on it; but that doesn't work with pointers.
    Code:
    char s[BUFSIZ];
    fgets(s, sizeof(s), stdin);
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    [QUOTE=dwks]If it's an actual array (a global one, maybe), you can use sizeof() on it; but that doesn't work with pointers.

    but if function fills it not till the end it will do no good
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  9. #9
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    I had an unsigned char array, which suppose to store in hex format, which I usually print it out using %02X.
    Right did u had your code something like as follow while calling your routine

    Code:
    unsigned char HexSte[10];
        
    somefucnction(HexStr);
    ?????

    atleast if u could find out the unsigned char array name u could iterate through the array and find out the length. Is that something like this u doing

    Code:
    somefucntion("0X1254F");
    if it is something like that, then it is quite difficult.

    ssharish2005

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Quote Originally Posted by vart
    Quote Originally Posted by dwks
    If it's an actual array (a global one, maybe), you can use sizeof() on it; but that doesn't work with pointers.
    but if function fills it not till the end it will do no good
    True. However, if the function is reasonable, it has to give the user a way to determine how much data was stored. Either it fills the whole thing like I suggested, or it leaves a terminator of some kind (likely a NULL character). If it doesn't leave a terminator, perhaps it always returns the same number of characters.

    There are other options; the function might return the size separately, for example.

    Of course, the function could be doing something I haven't thought of, or it might not be reasonable.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  11. #11
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by dwks
    However, if the function is reasonable, it has to give the user a way to determine how much data was stored.
    That's why I tried to ask what is the functionn prototype to understand what is it doing
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Right, and if the OP answered that question we might be better off . . . .

    Hi everyone,
    just having a couple of question here. I had an unsigned char array, which suppose to store in hex format, which I usually print it out using %02X. I tried to do a strlen since it an char array, but I think the result was not wat I expected. So therefore I would like to check whether there is a way to caluated the length of the item store in the unsigned char array?
    Well, %X is used for numbers, so you can't print a string with it. Print a string normally, with %s. If the string contains a number like "27" that you would like printed in hex as 1B, use sscanf("%X").

    Post your code. Then we can stop making [semi-]educated guesses.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  2. C++ FTP class won't work
    By lord mazdak in forum C++ Programming
    Replies: 8
    Last Post: 12-18-2005, 07:57 AM
  3. can anyone see anything wrong with this code
    By occ0708 in forum C++ Programming
    Replies: 6
    Last Post: 12-07-2004, 12:47 PM
  4. Length of string changes when passed with pointer
    By BungleSpice in forum C Programming
    Replies: 3
    Last Post: 03-21-2004, 12:52 PM
  5. Next Question...
    By Azmeos in forum C++ Programming
    Replies: 3
    Last Post: 06-06-2003, 02:40 PM