Thread: Converting unsigned char[] to char[]

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    277

    Converting unsigned char[] to char[]

    Oki I need to do it to use a given lib so I came out with the following coded wich fails:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    void removeCharSign (unsigned char ubuffer[], char buffer[])
    {
     int i;
      for( i = 0; i < (int)(sizeof ubuffer / sizeof *ubuffer); ++i)
      {
        buffer[i] = ubuffer[i];
      }
      buffer[i] = '\0';
    }
    
    int main()
    {
      unsigned char foo[] = "testcsadfwqoekrwfko";
      char bar[30];
      removeCharSign(foo,bar);
      printf("%s\n",bar);
       return 0;
    }
    Any ideas?
    It outputs just test

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You could just use a cast!
    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.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Your problem is that arrays passed to functions degrade* to pointers, which have a sizeof() of 4. You need to pass the size separately.

    * Prelude would suggest a weaker word.
    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.

  4. #4
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Quote Originally Posted by dwks
    Your problem is that arrays passed to functions degrade to pointers, which have a sizeof() of 4* . You need to pass the size separately.
    * On many machines, but not all.

  5. #5
    Registered User
    Join Date
    Aug 2006
    Posts
    11
    Code:
    void removeCharSign (unsigned char ubuffer[], char buffer[])
    {
     int i;
      for( i = 0; i < (int)(sizeof ubuffer / sizeof *ubuffer); ++i)
      {
        buffer[i] = ubuffer[i];
      }
      buffer[i] = '\0';
    }
    First, I would prefer to declare the function as:
    void removeCharSign (unsigned char * ubuffer, char * buffer)
    For me at least is easier to read

    Second, I don't understand
    Code:
    sizeof ubuffer / sizeof *ubuffer)
    What is supposed to do ? in 32bits boxes it will give always 4 as result, no mind what you place inside ubuffer.
    Therefore, it will copy only the first 4 chars.

    Do you want to get the real size of the ubbufer ? If that is a 0x0 ended string then you can simply use strlen(ubuffer)...

    And, at the end, I don't understand this function, why you want to convert unsigned char into char ?
    you can do this simply using cast (signed char *)bufferunsigned or (unsigned char *)buffersigned

    -----------------
    http://www.uberum.com

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    First, I would prefer to declare the function as:
    void removeCharSign (unsigned char * ubuffer, char * buffer)
    For me at least is easier to read
    Well, they're the same thing, so the OP can do what [s]he wants.
    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.

  7. #7
    Registered User
    Join Date
    Jun 2004
    Posts
    277
    Thanks for all the replies folks. So is
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    void removeCharSign (unsigned char *ubuffer)
    {
      ubuffer = (char)ubuffer;
    }
    
    int main()
    {
      unsigned char foo[] = "testcsadfwqoekrwfko";
      //char bar[30];
      removeCharSign(foo);
      printf("%s\n",foo);
    
       return 0;
    }
    oki?
    Last edited by Maragato; 09-04-2006 at 07:22 PM.

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Yes, but you don't really need a function:
    Code:
    int main()
    {
      unsigned char foo[] = "testcsadfwqoekrwfko";
      //char bar[30];
      printf("%s\n",(char *)foo);
    
       return 0;
    }
    [edit] No, actually, the cast is (char*), not (char). [/edit]
    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.

  9. #9
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    There's no need for a function at all. The binary representation of the string does not change, so there is no need to alter the string in any way.

    All you need to do is cast, which explicitly tells the compiler that you wish it to interpret the binary data in a particular way.

    As dwks said, when you need to pass foo to a function, just use:

    FunctionCall((char *) foo);
    instead of
    FunctionCall(foo);
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 08-11-2008, 11:02 PM
  2. Heap corruption using zlib inflate
    By The Wazaa in forum C++ Programming
    Replies: 0
    Last Post: 03-29-2007, 12:43 PM
  3. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  4. Obtaining source & destination IP,details of ICMP Header & each of field of it ???
    By cromologic in forum Networking/Device Communication
    Replies: 1
    Last Post: 04-29-2006, 02:49 PM