Thread: converting a string into binary for data transfer

  1. #1
    Registered User
    Join Date
    Oct 2010
    Location
    San Francisco, CA
    Posts
    5

    converting a string into binary for data transfer

    I am working on an embedded project that requires me to transmit data via SPI using a processor that doesn't have built in SPI ports.

    I was wondering if there was a simple library/algorithm/scheme in C++ that can allow me to convert a string into its binary form so I can clock each bit in on a rising clock edge. Thanks!!!!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What exactly do you mean by the binary form of a string? When you say a string, are you talking about std::string or a null terminated C-style string?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    This can easily be generalized to an entire string:
    Code:
    uint8_t text[] = "Hello";
    uint8_t i;
    uint8_t *p = &text[0];
    
    /* Transmit the first character. */
    for (i = 0; i < 8; ++i)
    {
        /* Wait for next edge */
        if ((uint8_t)(1 << i) & *p)
        {
            OUT_PORT |= 1 << PIN_NUMBER;
        }
        else
        {
            OUT_PORT &= ~(1 << PIN_NUMBER);
        }
    }
    You can replace the for loop with a static counter if you need to do the sending inside an ISR.
    Last edited by Memloop; 10-15-2010 at 02:06 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting a binary string to hex
    By maxhavoc in forum C++ Programming
    Replies: 6
    Last Post: 07-25-2006, 12:46 PM
  2. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Converting a string to binary
    By laasunde in forum C++ Programming
    Replies: 11
    Last Post: 07-01-2003, 11:16 AM
  5. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM