Integer to Binary

Discussion in 'C Programming' started by Andrew, Sep 30, 2003.

  1. Andrew

    Andrew Guest

    Hi I don't know if this is a common question or not

    I was wondering How I could convert an Integer to a byte in C is there a
    function that does this and if not could someone explain the math to me to
    be able to do this

    Thanks in Advance
    Andrew, Sep 30, 2003
    #1
    1. Advertising

  2. Andrew

    Ben Pfaff Guest

    "Andrew" <> writes:

    > I was wondering How I could convert an Integer to a byte in C is there a
    > function that does this and if not could someone explain the math to me to
    > be able to do this


    int integer = 123; /* A variable of an integer type. */
    char byte = integer; /* Convert it to a byte. */
    --
    "Give me a couple of years and a large research grant,
    and I'll give you a receipt." --Richard Heathfield
    Ben Pfaff, Sep 30, 2003
    #2
    1. Advertising

  3. Andrew

    Micah Cowan Guest

    "Andrew" <> writes:

    > Hi I don't know if this is a common question or not
    >
    > I was wondering How I could convert an Integer to a byte in C is there a
    > function that does this and if not could someone explain the math to me to
    > be able to do this


    I don't think the above is what you're actually asking. Judging
    by the subject line, I'll hazard a guess that what you really
    want is to convert an int variable into a string representation
    of the binary value. I'll interpret your "to a byte" as a request
    to represent this value to 8 places (bytes may have much more
    than 8 binary places: I'm assuming you meant "octet").

    Without giving you any code (our policy on this newsgroup is
    generally not to write code, but to comment on it), I'd recommend
    an algorithm such as: walk down from 7 to 0, checking the
    corresponding bit in your variable using the left-bitwise-shift and
    bitwise-and operators; output "1" if the bit is set, "0"
    otherwise.

    Note that the above will only work for values less than or equal
    to two to the power of seven; but I'm assuming this was an input
    requirement to your problem.

    Post your attempt, and we'll be happy to comment on it.
    Micah Cowan, Oct 1, 2003
    #3
    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. =?Utf-8?B?Sm9l?=

    CType(x,Integer) vs. Integer.Parse(x)

    =?Utf-8?B?Sm9l?=, Feb 6, 2006, in forum: ASP .Net
    Replies:
    7
    Views:
    5,947
    =?Utf-8?B?RGF2aWQgQW50b24=?=
    Feb 7, 2006
  2. =?ISO-8859-1?Q?Thomas_Gagn=E9?=

    No Math.min(Integer, Integer)?

    =?ISO-8859-1?Q?Thomas_Gagn=E9?=, Jul 29, 2003, in forum: Java
    Replies:
    0
    Views:
    502
    =?ISO-8859-1?Q?Thomas_Gagn=E9?=
    Jul 29, 2003
  3. Sebastian Stelzer

    How do I add an Integer to another Integer?

    Sebastian Stelzer, Oct 14, 2004, in forum: Java
    Replies:
    2
    Views:
    488
    Yu SONG
    Oct 15, 2004
  4. Sebastian Stelzer

    How do I add an Integer to another Integer?

    Sebastian Stelzer, Oct 14, 2004, in forum: Java
    Replies:
    6
    Views:
    45,391
    JavaBean2010
    Apr 7, 2010
  5. Johannes Zellner
    Replies:
    22
    Views:
    3,576
Loading...

Share This Page