HELP Please - Convert binary number to string

Discussion in 'C Programming' started by Todd_Goselin@mentor.com, Mar 16, 2007.

  1. Guest

    Hello,

    I've got a problem that I can't fix by myself, I'm fairly new to
    programming in C and in Unix and so I'm having a tough time doing a
    simple task of converting a binary number to a string. Can anyone help
    me with that?

    Here is the problem:

    vals2 = ldap_get_values(ld,e,b);
    if ( vals2 != NULL)
    {
    strcpy(sid,vals2[0]);
    }

    The strcpy function isn't working since vals2[0] is a binary number
    and not a string. I need to find away to make vals2 a string in order
    to copy it to the sid variable.

    Thanks,
    Todd
    , Mar 16, 2007
    #1
    1. Advertising

  2. <> wrote in message news:
    > Hello,
    >
    > I've got a problem that I can't fix by myself, I'm fairly new to
    > programming in C and in Unix and so I'm having a tough time doing a
    > simple task of converting a binary number to a string. Can anyone help
    > me with that?
    >
    > Here is the problem:
    >
    > vals2 = ldap_get_values(ld,e,b);
    > if ( vals2 != NULL)
    > {
    > strcpy(sid,vals2[0]);
    > }
    >
    > The strcpy function isn't working since vals2[0] is a binary number
    > and not a string. I need to find away to make vals2 a string in order
    > to copy it to the sid variable.
    >

    sprintf(sid, "%d", vals2[0]);

    make sure sid is big enough to hold the number plus terminating nul. Make
    sure vals2 is an array of integers. If val2[0] is double or float use "%f"
    instead.
    --
    Free games and programming goodies.
    http://www.personal.leeds.ac.uk/~bgy1mm
    Malcolm McLean, Mar 16, 2007
    #2
    1. Advertising

  3. Default User Guest

    wrote:

    > Hello,
    >
    > I've got a problem that I can't fix by myself, I'm fairly new to
    > programming in C and in Unix and so I'm having a tough time doing a
    > simple task of converting a binary number to a string. Can anyone help
    > me with that?



    What do you mean by a "binary number"? Do you mean just an integer,
    represented in binary form? Or number that can only take on the values
    of 0 and 1?

    If the former, what do you want the result to look like? The number in
    decimal? The bit pattern? Give an example of input and expected output.




    Brian
    Default User, Mar 16, 2007
    #3
  4. On Mar 16, 1:27 pm, wrote:
    >
    > I've got a problem that I can't fix by myself, I'm fairly new to
    > programming in C and in Unix and so I'm having a tough time doing a
    > simple task of converting a binary number to a string. Can anyone help
    > me with that?
    >
    > Here is the problem:
    >
    > vals2 = ldap_get_values(ld,e,b);
    > if ( vals2 != NULL)
    > {
    > strcpy(sid,vals2[0]);
    > }
    >
    > The strcpy function isn't working since vals2[0] is a binary number
    > and not a string. I need to find away to make vals2 a string in order
    > to copy it to the sid variable.


    The term "binary number" could mean any of several things, so you need
    to tell us what you mean by it. You also need to tell us the types of
    your variables 'vals2' and 'sid', and tell us what format you need the
    resulting string to be in (for example "a printable character string
    containing the value in decimal" or whatever it is you need).
    J. J. Farrell, Mar 17, 2007
    #4
    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. makok
    Replies:
    1
    Views:
    6,771
    Anders Hellerup Madsen
    Feb 23, 2004
  2. KK
    Replies:
    2
    Views:
    557
    Big Brian
    Oct 14, 2003
  3. Replies:
    5
    Views:
    523
    Pete Becker
    May 21, 2005
  4. davidj411
    Replies:
    11
    Views:
    1,298
    Kam-Hung Soh
    May 29, 2008
  5. Bob Sanders
    Replies:
    5
    Views:
    217
    Ryan Lewis
    May 30, 2008
Loading...

Share This Page