Thread: Simple issue with bytes

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    13

    Simple issue with bytes

    Hi,

    I need a byte representation that will allow me to shift right, AND, and compare bytes. I've managed to find this: http://www.informit.com/guides/conte...lus&seqNum=163
    which says to use type unsigned char.

    For testing purposes, I wish to print the byte in either binary or decimal, it doesn't matter. How would I do that? I've tried using atoi, but it always returns 0. ie:

    unsigned char blah = 00000111;
    printf("%d", atoi(&blah));

    will print 0.

    Thanks.

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    It prints 0 because 0b00000111 is not a valid printable ascii number. Valid ascii numbers are 0x30 (0b00110000) through 0x39 (0b00111001).
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    13
    Quote Originally Posted by Todd Burch View Post
    It prints 0 because 0b00000111 is not a valid printable ascii number. Valid ascii numbers are 0x30 (0b00110000) through 0x39 (0b00111001).
    Yes, but I'm not after the ASCII representation, I want to print either the decimal or binary representation. So how would I do that?

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Decimal: printf("%d\n", blah) ;

    For binary, you'll have to loop 8 times, checking to see if each bit is 1 or zero, and print the appropriate character.

    Todd
    Mainframe assembler programmer by trade. C coder when I can.

  5. #5
    Registered User
    Join Date
    Feb 2008
    Posts
    13
    Quote Originally Posted by Todd Burch View Post
    Decimal: printf("%d\n", blah) ;

    For binary, you'll have to loop 8 times, checking to see if each bit is 1 or zero, and print the appropriate character.

    Todd
    Ack, I hate the feeling I get when I struggle with something and in the end, the answer is so simple. I thought I tried this, but I must have been only trying to print it as a char. Thanks!

  6. #6
    Registered User Tommo's Avatar
    Join Date
    Jun 2007
    Location
    Scotland
    Posts
    101
    Code:
    unsigned char blah = 00000111;
    I doubt you can do this. This gave me decimal 73. Nowhere in that link you provided does it say you can do this.

  7. #7
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    There are numerous .c programs that do just this. The key is which method would you like to use:

    1. Bitwise method
    2. Recursion
    3. using the shift operators '<<' or , '>>'

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bytes lost with partial read in UDP
    By mynickmynick in forum Networking/Device Communication
    Replies: 3
    Last Post: 03-31-2009, 02:06 AM
  2. Need help on c programming bytes handling
    By boris1979 in forum C Programming
    Replies: 5
    Last Post: 05-29-2007, 12:48 AM
  3. Rendering 32-bit images - alpha is ignored
    By ulillillia in forum Windows Programming
    Replies: 59
    Last Post: 04-21-2007, 03:50 PM
  4. Simple issue but I am STUCK
    By jedispy in forum C++ Programming
    Replies: 2
    Last Post: 12-01-2006, 02:02 AM
  5. socket question
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 07-19-2002, 01:54 PM