Simple issue with bytes

This is a discussion on Simple issue with bytes within the C Programming forums, part of the General Programming Boards category; Hi, I need a byte representation that will allow me to shift right, AND, and compare bytes. I've managed to ...

  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
    Katy, Texas
    Posts
    2,309
    It prints 0 because 0b00000111 is not a valid printable ascii number. Valid ascii numbers are 0x30 (0b00110000) through 0x39 (0b00111001).
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  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
    Katy, Texas
    Posts
    2,309
    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
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  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
    602
    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, 01:02 AM
  5. socket question
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 07-19-2002, 01:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21