Binary Representation of String

This is a discussion on Binary Representation of String within the C++ Programming forums, part of the General Programming Boards category; Hi everbody, I have a newbie question for you, I have c++ string in my hand, and i want to ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    5

    Binary Representation of String

    Hi everbody,
    I have a newbie question for you, I have c++ string in my hand, and i want to have the bit representation of each letter of this string, how can i do this?

    Thanks

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Do you know how to do it for a single character? If so, then just do that for each character in the string.

    If not . . .

    You can use
    Code:
    number & 1
    to get the value of the rightmost bit. Then you can use
    Code:
    number >>= 1
    to shift the bits to the right. But using that method will generate the bits in reverse order. If you know how many bits there are (ie, 8), then it might be better to use
    Code:
    mask = UCHAR_MAX;
    n = 7;
    loop
        number & mask >> n;
        mask /= 2
        n --
    or something.

    Also note that the MSB (rightmost digit) will probably be 1 for negative numbers, depending on your compiler's implementation.

    Also see this (somewhat advanced) FAQ: http://faq.cprogramming.com/cgi-bin/...&id=1044780608
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Unless this is an assignment intended to teach bit manipulation, you can just put the char into a bitset and then output the bitset.

  4. #4
    Registered User
    Join Date
    Dec 2005
    Posts
    5
    Thank you very much dwks, then to get the LSB of the character, i can say
    bool temp=char & 1;
    Did i understand i right?

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Exactly. (Except that you can't have a variable called char. )

    Then you can use
    Code:
    variable = variable >> 1;
    to eliminate the LSB, shifting all of the bits to the right.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    902
    The bitset idea looks really good.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  3. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM

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