Velocity Reviews > HELP Please - Convert binary number to string

# HELP Please - Convert binary number to string

Todd_Goselin@mentor.com
Guest
Posts: n/a

 03-16-2007
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

Malcolm McLean
Guest
Posts: n/a

 03-16-2007

<(E-Mail Removed)> 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"
--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Default User
Guest
Posts: n/a

 03-16-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) 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

J. J. Farrell
Guest
Posts: n/a

 03-17-2007
On Mar 16, 1:27 pm, (E-Mail Removed) 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).