void*, char*, unsigned char*, signed char*

Discussion in 'C Programming' started by Steffen Fiksdal, May 8, 2005.

  1. I have a function that base64 decodes some data. The incoming data is
    received as "const char*" (BASE64 characters are always safe ASCII
    characters, meaning they will always fit in a signed char positive range).

    The resulting decoded data is placed in memory, and the function exposes
    an "unsigned char*" to the caller.

    What does ANSI C say (if anything) about what kind of pointer is the
    correct one to use for passing/returning/processing binary data locations ?

    Best regards
    Steffen
     
    Steffen Fiksdal, May 8, 2005
    #1
    1. Advertising

  2. Steffen Fiksdal

    Jack Klein Guest

    On Sun, 8 May 2005 22:41:25 +0200, Steffen Fiksdal
    <> wrote in comp.lang.c:

    >
    > I have a function that base64 decodes some data. The incoming data is
    > received as "const char*" (BASE64 characters are always safe ASCII
    > characters, meaning they will always fit in a signed char positive range).
    >
    > The resulting decoded data is placed in memory, and the function exposes
    > an "unsigned char*" to the caller.
    >
    > What does ANSI C say (if anything) about what kind of pointer is the
    > correct one to use for passing/returning/processing binary data locations ?
    >
    > Best regards
    > Steffen


    Binary data is best read as unsigned characters, and this is a
    particularly good idea to use them for base64 encoding/decoding as you
    need to do bit shifting. Bit shifting can be questionable using
    signed types.

    Use a pointer to unsigned char to access the data as unsigned
    characters, and use unsigned longs to accumulate and shift them.

    There is no need to cast away the const if you are only reading the
    bytes.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
     
    Jack Klein, May 9, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ollej Reemt
    Replies:
    7
    Views:
    551
    Jack Klein
    Apr 22, 2005
  2. Stig Brautaset

    `void **' revisited: void *pop(void **root)

    Stig Brautaset, Oct 25, 2003, in forum: C Programming
    Replies:
    15
    Views:
    795
    The Real OS/2 Guy
    Oct 28, 2003
  3. Replies:
    5
    Views:
    842
    S.Tobias
    Jul 22, 2005
  4. Ioannis Vranos
    Replies:
    11
    Views:
    765
    Ioannis Vranos
    Mar 28, 2008
  5. Ioannis Vranos

    Padding bits and char, unsigned char, signed char

    Ioannis Vranos, Mar 28, 2008, in forum: C Programming
    Replies:
    6
    Views:
    620
    Ben Bacarisse
    Mar 29, 2008
Loading...

Share This Page