Thread: Searching strings

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    30

    Searching strings

    Hi

    I have a particular problem where I need to feed two types of inputs into my program, a hex number and a binary number.

    Example of input:

    Binary
    Bi = 1100100
    Bi = 0110101
    Bi = 0101100

    Hexadecimal
    3A001341
    CA095815
    1A596317

    How would I be able to distinguish the two?

    I usually do:

    Code:
    while(scanf("Bi = %d", &binary) == 1) {
    
           //blah
    }
    But obviously it wouldn't work since the loop would be broken at the string "Binary" since it's not in the correct format as the input in scanf. So basically I need some kind of method where the program will know whether the inputs are in binary or hex form, after reading in the headings, or if there's a simpler way.

    thanks

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You can't in all cases. If you aren't prefixing your hex with 0x, and all input is the same length, then you don't know if 01010101 is hex, or binary. Unless you are saying that the binary strings all start with "Bi = ", in which case, just check for that at the start.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    30
    Actually my bad, I changed the question slightly, didn't know it would make a difference.

    Say the binary is really a hex number i.e Bi = 1A596317. So they all have 8 bits.

    OK, but if you put a scanf in a while loop to read the "binary" values, the first input would be the character string "Binary". Wouldn't this break the while loop since it's not in the form "Bi = %d"? I hope you get what I mean. Basically I want to ignore the "binary" and read in the following values of correct form.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So read in the word "Binary".
    Code:
    scanf("Binary ");

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    30
    Could you be more specific? Maybe a sample code...

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The bits in the boxes are sample code.

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    30
    can you explain how that line addresses my problem? how does it work?

    i'd prefer someone else answer.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Post three lines of the sample file, exactly as they appear. Then, post your code as to what you have tried, and what problem you are having. The keywords here are: small example.


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by nasser View Post
    Hi

    I have a particular problem where I need to feed two types of inputs into my program, a hex number and a binary number.

    Example of input:

    Binary
    Bi = 1100100
    Bi = 0110101
    Bi = 0101100

    Hexadecimal
    3A001341
    CA095815
    1A596317

    How would I be able to distinguish the two?

    I usually do:

    Code:
    while(scanf("Bi = %d", &binary) == 1) {
    
           //blah
    }
    But obviously it wouldn't work since the loop would be broken at the string "Binary" since it's not in the correct format as the input in scanf. So basically I need some kind of method where the program will know whether the inputs are in binary or hex form, after reading in the headings, or if there's a simpler way.

    thanks
    Ok... you want someone else to answer... can do!

    Look up the formatting strings for fscanf() in your C library documentation...
    Seriously... that's where your answer lies... in understanding what it can and cannot do.

    What you are going to discover is that it *cannot* directly convert a binary value from a file... You're going to have to read it as a string and convert it yourself.

  10. #10
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by nasser View Post
    But obviously it wouldn't work since the loop would be broken at the string "Binary" since it's not in the correct format as the input in scanf. So basically I need some kind of method where the program will know whether the inputs are in binary or hex form, after reading in the headings, or if there's a simpler way.

    thanks
    Quote Originally Posted by tabstop View Post
    So read in the word "Binary".
    Code:
    scanf("Binary ");
    I believe what tabstop is eluding to, is that if your input is as indicated with headers delimiting your sections in your file, you simply have to setup a basic state machine. Grab line by line, check for your delimiters (in this case either "Binary" or "hexadecimal"), if it isn't one of those then simply convert based on the current state. Once you hit one of those "headers" change the state of your machine.

    Shouldn't be too hard to work out.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  11. #11
    Registered User
    Join Date
    Mar 2011
    Posts
    30
    Actually I think I figured it out. I think I'll use a scanf inside an If statement to see if the input is in the form I want. Scanf will return 1 if the input is in correct form, or 0 if its a character. I'll try that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Searching within strings
    By Poincare in forum C Programming
    Replies: 3
    Last Post: 04-24-2009, 08:53 PM
  2. Searching Strings
    By kinghajj in forum C Programming
    Replies: 12
    Last Post: 05-13-2004, 04:32 AM
  3. Searching for strings in a file
    By Stephen in forum C Programming
    Replies: 1
    Last Post: 11-05-2001, 01:40 PM
  4. Searching through strings...
    By C-Duddley in forum C Programming
    Replies: 1
    Last Post: 09-14-2001, 04:26 PM
  5. Searching arrays for strings
    By Zaarin in forum C++ Programming
    Replies: 14
    Last Post: 09-03-2001, 06:13 PM