Explain this C code in english

This is a discussion on Explain this C code in english within the C Programming forums, part of the General Programming Boards category; Hey, first time poster, and I have never even programmed with C so go easy on me, you'll see what ...

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    19

    Explain this C code in english

    Hey, first time poster, and I have never even programmed with C so go easy on me, you'll see what I'm after in a sec.

    I need someone to look at this c code and explain what is going on in plain english. I would like to maybe convert the C code into something identical that could be used in visual basic (the programming level im currently at -_-).

    The code is:
    Code:
    int gs_chresp_num(char *challenge) {
        int     num = 0;
    
        while(*challenge) {
            num = *challenge - (num * 0x63306CE7);
            challenge++;
        }
        return(num);
    }
    I know the code is supposed to take a string of text known as 'challenge' and process it into a new number. And I know for a fact that if you made 'challenge' the string ABCDEFGHIL, the code would output 884373937. I know this because I got the code from here: http://aluigi.altervista.org/papers/gs_chresp_num.h

    What I am looking for is some Visual Basic code that will do exactly what this c code does. Or maybe just an explanation in plain english of what is going on in the C code, so I could write my own VB code to duplicate it.

    Thank you very much for the help!

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,531
    > while(*challenge) challenge++;
    This steps through the string, one character at a time.

    > *challenge
    This returns the numeric value of the current character, say 'A' being 65

    > (num * 0x63306CE7)
    0x is used to signify a number written in base-16
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    19
    Thanks for the reply. That helped a tiny bit, but I still don't have an idea of where to start on the VB side.

    If I understand you correctly, this code goes through the 'challenge' string (we'll say the string is ABCDEFGHIL) 1 character at a time, and converts each character to a number.

    And the conversion happens here I assume: num * 0x63306CE7

    I know you said 0x represents base16, but what is that 63306CE7?

    I guess im still completely lost on how it get's ABCDEFGHIL to 884373937.

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    I know you said 0x represents base16, but what is that 63306CE7?
    It's just a number. It's 1664117991 in base-10 (decimal).

    Just step through it in your head or on paper. The first time through the loop, num is 0 and *challenge is 'A' or 65.

    It sets the num to 65 - (0 * 1664117991) which is just 65. The second time through the loop, num is 65 and *challenge is 'B' or 66. Keep going from there until you reach the end of the challenge string.
    If you understand what you're doing, you're not learning anything.

  5. #5
    Registered User
    Join Date
    Aug 2006
    Posts
    19
    Ok so if the first pass is: 65 - (0*1664117991)

    Would the second pass be: 66 - (65*1664117991)?

    If so, then my output on the second pass would be a huge number.

    And once I do each pass, am I adding up the numbers then? I've been scribbling on paper like you said for the last 10 mins trying to figure this out x_x ..sorry.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    It would indeed be a very large (negative) number.
    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.

  7. #7
    Registered User
    Join Date
    Aug 2006
    Posts
    19
    What am I doing with each of these numbers on every pass after they are calculated?

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    You're assigning the result of the expression to num.
    Code:
    num = *challenge - (num * 0x63306CE7);
    in pseudo code might look like this:
    Code:
    bignum = num * 1664117991
    result = letter - bignum
    num = result
    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.

  9. #9
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by soadlink
    Ok so if the first pass is: 65 - (0*1664117991)

    Would the second pass be: 66 - (65*1664117991)?

    If so, then my output on the second pass would be a huge number.

    And once I do each pass, am I adding up the numbers then? I've been scribbling on paper like you said for the last 10 mins trying to figure this out x_x ..sorry.
    Something to remember is that program always returns an int, which can only be of
    a certain size depending on the computers word length for an integer, this can vary
    on different computers, it might be 16 bit on on some older computers, 32 bit (probably)
    on current computers, and maybe 64 bit on 64 bit machines, AMD64 for example.

    Anyway you need to sort out the word lengths and bit sizes etc (not too sure of them my
    self!!) and ensure they are the same in visual basic (or make modifications to enuse this).
    Basically it takes an input string and generates a code based upon the 'key' 0x63306CE7
    and the input string (I think this is known as a 'hash key' but, whatever it generates
    a code as output).

    It should be fairly easy to get the loop going in VB you just need to make sure the
    data sizes work to produce the same result.

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Yes, generating such large numbers would cause the value to wrap pretty quickly, so you'd probably want to use a 32-bit integer in VB (assuming that's what the original author of the C code intended).
    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.

  11. #11
    Registered User
    Join Date
    Aug 2006
    Posts
    19
    Ok I wrote up some code in VB to calculate the string for ABCDEFGHIL:

    Code:
    Text2.Text = 65 - (0 * 1664117991)
    Text2.Text = 66 - (Text2.Text * 1664117991)
    Text2.Text = 67 - (Text2.Text * 1664117991)
    Text2.Text = 68 - (Text2.Text * 1664117991)
    Text2.Text = 69 - (Text2.Text * 1664117991)
    Text2.Text = 70 - (Text2.Text * 1664117991)
    Text2.Text = 71 - (Text2.Text * 1664117991)
    Text2.Text = 71 - (Text2.Text * 1664117991)
    Text2.Text = 72 - (Text2.Text * 1664117991)
    Text2.Text = 76 - (Text2.Text * 1664117991)
    The code works perfectly, and it returns a final value of: -6.36165918672933E+84 in the text2.text box. This is the same value I got when doing it on my calculator manually.

    Now that is a huge number as we were all saying it would be, but I am clueless on how to get it to "return a 32bit integer." I hate having to ask someone to just do it for me, but I think it's coming to that . Does anyone know what VB code would be used to get it to return smaller values (32 bit integers?)? Is my calculation correct, but I am just not writing it to return a 32 bit integer? Thanks for all the help so far

  12. #12
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by soadlink
    Ok I wrote up some code in VB to calculate the string for ABCDEFGHIL:

    Code:
    Text2.Text = 65 - (0 * 1664117991)
    Text2.Text = 66 - (Text2.Text * 1664117991)
    Text2.Text = 67 - (Text2.Text * 1664117991)
    Text2.Text = 68 - (Text2.Text * 1664117991)
    Text2.Text = 69 - (Text2.Text * 1664117991)
    Text2.Text = 70 - (Text2.Text * 1664117991)
    Text2.Text = 71 - (Text2.Text * 1664117991)
    Text2.Text = 71 - (Text2.Text * 1664117991)
    Text2.Text = 72 - (Text2.Text * 1664117991)
    Text2.Text = 76 - (Text2.Text * 1664117991)
    The code works perfectly, and it returns a final value of: -6.36165918672933E+84 in the text2.text box. This is the same value I got when doing it on my calculator manually.

    Now that is a huge number as we were all saying it would be, but I am clueless on how to get it to "return a 32bit integer." I hate having to ask someone to just do it for me, but I think it's coming to that . Does anyone know what VB code would be used to get it to return smaller values (32 bit integers?)? Is my calculation correct, but I am just not writing it to return a 32 bit integer? Thanks for all the help so far
    I have never used VB so I can't help you a lot, however the value you came up with is no
    good to you, what you want is the lower order numbers.
    You have someting like
    636165918672933??????????????????????????????????? ?????????????????????????????????????????????????? ?????????????????????????XXXXXXXX

    The bit you are interested in is the XXXXXXXXX, however you have lost those bits because you are using floating point, I presume, you need to define your result as an integer (a 32 bit one)
    then you might be getting somewhere (assuming the rest of the code is correct!!).
    If you subtract the number by which the result is over the max value of a 32 bit int on each iteration of the loop you might be getting somewhere!! (That is 4294967269 (I think not far off anyway!!)). But I don't know VB's data types or your program so I can't be of much more help.
    however you will get nowhere using floating point numbers, try defining your result variable as an integer and it just might give you the correct answer!!

    What is actualy happening is a form of encyption (or it can be used as). A good understanding of maths would be helpful to you. Actually I am not even sure if you can have an iinterger variable in VB but I am not sure, however, if you were really good at maths (like me ) you could probably simulate a way of doing it!!
    If I get really bored I will tell you how (poker tables a bit quiet at the moment) anyway good luck!!
    You need good luck in poker too!!!

  13. #13
    Registered User
    Join Date
    Aug 2006
    Posts
    19
    I was looking for a way to convert the end result to a 'long' integer in visual basic so i could see the XXXXXXXX part of the number as you were talking about.

    I tried:
    Code:
    text3.text = CLng(text2.text)
    ^ which would make the text3 textbox display the long version of my end result. It gave me an 'overflow' error.

    So i tried:
    Code:
    text3.text = CInt(text2.text)
    and I got the same thing.

    I think I'm getting close, just need the right code, or a way to display it at 32bit . Any more tips!?

    Thanks

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    CLng() perhaps? Few seconds on the MSDN gives you all you need to know.


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

  15. #15
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    The 'overflow' means the VB data type you specified cannot hold the specified value. In C this would not flag an error, but you would lose precision in your value and when you printed it out.....it would not be what you expected.

    VB catches this and flags an error. The crappy solution is to use VB's crappy error handling and use this:

    Code:
    sub Foo(<param_list>)
      on local error goto ERR:
    
      'attempt to convert to long here
      'due to previous line any error will transfer execution to ERR: label
    
    end sub
    ERR:
      'Find error number
       int errornum=<some function to get number designation of error>;
    
       select case errornum:
         case <overflow error number>:
           'handle error and resume
           resume
       end select
    VB data types and conversions from one type to another are not as powerful, flexible, or easy as C.

    There are times when VB will simply allow the loss in precision and not flag an error. However, I found with very large values it does flag an overflow error.

    There should be a VB function to convert from double to long and long double to long but you will lose precision. The correct approach in VB would be to compute the value, isolate the needed value, place that value into a long.

    Also note that ALL numeric data types in VB are SIGNED. VB and BASIC do not allow UNSIGNED data types. So the PRINT functions and all VB functions interpret the numeric types as SIGNED.

    32-bit integers in 32-bit C:
    • DWORD (unsigned int/long)
    • unsigned/signed int
    • unsigned/signed long


    32-bit integers in 32-bit VB:
    • dim value as long


    I don't think VB changes the data width of int to correspond to the default integer of the platform.

    Actually I am not even sure if you can have an iinterger variable in VB but I am not sure, however, if you were really good at maths (like me )
    Ok...
    Last edited by VirtualAce; 08-31-2006 at 12:17 AM.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can someone explain this code?
    By Sharke in forum C++ Programming
    Replies: 3
    Last Post: 06-18-2009, 12:12 PM
  2. Could someone explain this code for me please...
    By JoshR in forum C++ Programming
    Replies: 89
    Last Post: 06-26-2005, 01:20 AM
  3. Seems like correct code, but results are not right...
    By OmniMirror in forum C Programming
    Replies: 4
    Last Post: 02-13-2003, 12:33 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 4
    Last Post: 01-15-2002, 11:04 PM

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