Thread: HTTP Tunnelling suggestions

  1. #1
    Living to Learn
    Join Date
    Feb 2006
    Location
    Aberdeen, UK
    Posts
    33

    Question Comparing 16-bit integer values in C

    Hello all.

    I am new here, but am hoping that someone may be able to help me with this one. I've moved over to C from Java, am

    following a few tutorials and have started learning about binary, hex and data manipulation.

    One of the examples calls for a program that will identify a 16-bit number and report true if that number is 1.

    So far I have:

    Code:
    int bitident(int n, int b)
    if(b == 000000000000001) {
    n = 1 }
    return n
    else {
    n = 0 }
    return n
    but this seems to me to be too simple and vague. I'm not sure how to code into the 16-bit idea, as what is to stop anyone just putting in "1" and the program returning that as true?

    I'm partially sighted and do find net navigation difficult at times, so if this has been posted and answered elsewhere on the board please forgive the discretion.

    Your help would be verey much appreciated. I'm keen to understand and learn.

    Thanks very much in advance.

    Hussein - aka PCHolic.
    Last edited by hpteenagewizkid; 02-16-2006 at 12:57 PM. Reason: Incorrect Title

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    The computer sees all data as binary, so comparing an int b to 000000000000001 is the same as comparing it to 1 or 0x1.

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    43
    Code:
    int bitident(int b)
    {
          int n;
    
          if(b == 1) 
                n = 1;
          else
                n = 0;
          return (n);
    }
    This should do what you want it to I think. C is a very low language in some respects, but in others it is not, and one of those others is that it is more complicated than you'd think to read a binary input, you need arrays and whatnot to do that. But what this will do, is it will take the input of int b and then compare it to one. If the value of b is equal to one, it will set n, a variable declared inside the function making it a local variable that will not work if you try to use it in any other place other than this function (unless you declare another variable n in that part of the code). If b is not equal to one n will be declared to equal zero. After it has assigned a value to n it returns that value as an integer to wherever you called the function. The way you had it before, it would return n after the if statement even if b wasn't equal to one. Also, because of that I believe that the code would not compile properly because the else statement would have another statement between it and the if statement which would cause the compiler to choke....I think.
    Hope this helps
    -Crazed

    p.s. as you can see with the code I posted above it is indented to help with the readability. This both helps others understand your code and also helps yourself to realize if there is an error sometimes.

  4. #4
    Living to Learn
    Join Date
    Feb 2006
    Location
    Aberdeen, UK
    Posts
    33
    Hello and thanks for both of your replies.

    Just so that I understand:

    The computer reads everything at the lowest level as binary. Hex, octal etc are just shorthand version of binary, e.g 0x9C (h) instead of 10011101 (b), 157 (dec). So it makes no difference in the end.

    just one point, using the example of the 157 (dec) above, would you have to hard code all the variations of that if you wanted to make a comparision, or would the program recognize 0x9C just as well as the long binary form?

    Thanks also for the revised code. I can see now that it is just a simple comparison check. The tutorial question made it seem much more complicated with it's 16-bit integer format. Also, I can see now that indenting the various parts of the code makes it much easier to read. Thanks for that and I'll keep it in mind in the future.

    I'll keep on learning and gathering knowledge, and will most likely be back soon.

    Thanks much again, both of you. Sorry for the title of this thread, I'll see if I can rename it, the autocomplete must have run away with itself when I was creating the post.

    Hussein. - aka PCHolic.

  5. #5
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    The C-compiler doesn't recognize any "long binary form".
    the number
    00000001 would be recognized as octal 1or 0x01 hex
    00000010 would be octal 10 that translates to dec 8 or hex 0x08
    Kurt

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    These two peices of code would compile to the exact same output
    Code:
    if (b==157)
    Code:
    if (b==0x9D)

  7. #7
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by Quantum1024
    These two peices of code would compile to the exact same output
    Code:
    if (b==157)
    Code:
    if (b==0x9D)
    another one
    Code:
    if (b==0235)
    Kurt

  8. #8
    Registered User
    Join Date
    Jan 2006
    Location
    Provo, UT, USA, Earth
    Posts
    2

    Lightbulb

    Of course, you can always play with the bits by using <<, >>, &, |, ~.

    http://www.cs.cf.ac.uk/Dave/C/node13.html

  9. #9
    Living to Learn
    Join Date
    Feb 2006
    Location
    Aberdeen, UK
    Posts
    33
    Thanks I think I start to understand now how it works. Maths is not the love of my life but this I think I can cope with

    Cheers.

    Hussein.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Http 502 and 503 errors
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 07-18-2008, 03:21 AM
  2. Error stop Http Listener
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 06-04-2008, 02:14 AM
  3. my HTTP request handler code correct?
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 04-25-2008, 04:01 AM
  4. C# HTTP request/response
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 04-02-2008, 06:00 AM
  5. Writing all HTTP requests from a website to a log file
    By goomyman in forum C# Programming
    Replies: 1
    Last Post: 07-29-2005, 09:18 AM