what is int()?

This is a discussion on what is int()? within the C Programming forums, part of the General Programming Boards category; hey all, just reading some code I got off of planet-source-code, and ran into this statement: Code: b = int(a); ...

  1. #1
    Madly in anger with you
    Join Date
    Nov 2005
    Posts
    211

    what is int()?

    hey all, just reading some code I got off of planet-source-code, and ran into this statement:

    Code:
    b = int(a);  // store the ASCII value of a into b as an int value
    what is int(a)? is it a malformed cast or is it a function? the writer says he compiled it in Borland C++, and I know Borland Delphi has cast-like functions like StrToInt and IntToStr, hence the reason why I am asking if it is a function (I've never seen a cast written like that). I went looking through the entire source for it, and if it is a function, or even a macro-function, its not declared in any of his code.

    however I would think of it as a malformed cast, better written as:

    Code:
    b = (int)a;
    b is a char, and a is a char by the way. would the above cast not assign the ASCII value of a to b?

    as always, your help is greatly appreciated.


    thank you in advance.
    Last edited by Bleech; 11-08-2006 at 01:25 AM.

    Intel Core 2 Quad Q6600 @ 2.40 GHz
    3072 MB PC2-5300 DDR2
    2 x 320 GB SATA (640 GB)
    NVIDIA GeForce 8400GS 256 MB PCI-E

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    If it's a cast, it's incorrect, as you've pointed out. Also, if both are type char then the cast doesn't do anything at all, and should be removed. Even if one of them were an int the cast would be pointless.


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

  3. #3
    Even death may die... Dante Shamest's Avatar
    Join Date
    Apr 2003
    Location
    Malaysia
    Posts
    970
    Quote Originally Posted by sl34k
    what is int(a)?
    int() is an example of a constructor for built-in types.

    Quote Originally Posted by sl34k
    b is a char, and a is a char by the way. would the above cast not assign the ASCII value of a to b?
    Yes, I believe the int() is unnecessary here.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It's unnecessary because this isn't C++. If it were, you still wouldn't need the whole "int" portion, because you can just do:
    Code:
    int x(5);
    Which is still pointless when an assignment operator takes less typing.


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

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    That may be C++ code.
    Code:
    b = int(a);  // store the ASCII value of a into b as an int value
    Notice the C++ style comment, added to C with the C99 standard.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by swoopy
    That may be C++ code.
    Right, but this isn't. This is the C forum. That was my point. We don't care about C++ code over here, except for the fact that it keeps cluttering up the forum.

    Anyway, it's still not needed, and still is pointless to do so, and still is more typing than a regular assignment.


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

  7. #7
    Madly in anger with you
    Join Date
    Nov 2005
    Posts
    211
    my mistake, my few mistakes.

    first of all the code is infact C++, it has the .cpp extension, and uses C++ streams and functions for file management. sorry about that, I thought the code I posted was infact C, but just a different style of writing a C style cast (as I know most C/C++ compilers are lenient on code style).

    secondly, b is declared as an int, not a char.

    this is not your ordinary simple XOR encrypt routine, although it is still simple, it utilizes a different technique of adding ASCII values of each char in the input string and the key together. here it is, even though I am aware that this is the C forum, moderators please move this if you see fit, and I am sorry about this (this is the first time I've posted C++ in the C forum, simply because like I said I thought the section I was looking at was ordinary C code written in an odd style), here is the snippet I am interested in:

    Code:
    /*
    				Here's how the encryption works.  It takes the ASCII number for each character of
    				the key and of the file, and adds them together.  Say the key is "patrick", then
    				to encrypt the line "isverycool" it would look like this:
    		
    				patrickpat
    				++++++++++
    				isverycool
    		
    				The key simply repeats itself to accomodate encrypting a line that is longer than
    				the key.
    		
    				Then, we do a bitwise XOR on the 2 chars with the logic that if you XOR 2 things,
    				then XOR them again you'll end up back where you started.
    				Ex:
    				00100101  <-- Key
    				01010101  <-- Data
    				--------
    				10001111  <-- Result
    				
    				Now, let's place result against the key
    				00100101  <-- Key
    				10001111  <-- Encrypted data
    				--------
    				01010101  <-- It matches our original data!!!
    				
    				The result is a fairly complex encryption, which can be cracked, but not
    				without a lot of time and effort.
    						
    				*/
    	
    				i = 0;    // for whatever reason the program seems to crash without these lines
    				fsize = 0;
    		
    				while (!fin.eof())
    				{
    					fin.get(a); // get the character to be encrypted
    					b = int(a);  // store the ASCII value of a into b as an int value
    					b = b^key[i]; // our bitwise XOR
    					b += int(key[i]);
    					if (i < limit) i++; else i = 0;
    					a = char(b); // convert b back in an ASCII value
    					fout << a;
    					//gotoxy(col+13,6);
    					//cout << "                       ";
    					//gotoxy(col+13,6);
    					//cout << int(float(fsize)/float(f1size)*100) << "%";
    					//cout << fsize << "/" << f1size;
    					fsize++;		
    				}
    are the casts still unnecessary here even though the ASCII values of the characters from the file are added to the ASCII values of the characters in the key?

    thank you in advance, and again, sorry for the mix up here, it won't happen again.
    Last edited by Bleech; 11-10-2006 at 12:57 AM.

    Intel Core 2 Quad Q6600 @ 2.40 GHz
    3072 MB PC2-5300 DDR2
    2 x 320 GB SATA (640 GB)
    NVIDIA GeForce 8400GS 256 MB PCI-E

  8. #8
    Madly in anger with you
    Join Date
    Nov 2005
    Posts
    211
    just wrote up this code, which appears to work, however I haven't added the addition of the ASCII values of the input and the key yet. as I said, it appears to work (surprisingly, usually when I attempt stuff like this with pointers it goes horribly wrong, and this was my first attempt at this function, I guess I'm getting the hang of it) however it might not work properly:

    Code:
    void EncryptRecord(char *szRec, unsigned long nLen, char *szKey)
    {
    	unsigned long i;
    	char *p;
    
    	p = szKey;
    
    	for(i = 0; i < nLen; i++) {
    		if(*p == szKey[4])
    			p = szKey;
    
    		*szRec = *szRec ^ *p;
    
    		szRec++;
    		p++;
    	}
    }
    the key is only 4 characters long, hence that if statement testing the address in p against the address of the 4th element in szKey (this may be incorrect, I've never written such a pointer comparison before). obviously p gets reset to szKey[0] here if it is pointing to szKey[4].

    as for the function parameters, szRec is (normally) a record size (256 byte) chunk of a file, however it may be smaller as the file may be of any length, hence the nLen parameter, which is the size in bytes of szRec.

    does this look ok? as I said it appears to work, but looks can be decieving.

    if anyone could please answer my original question about the casts being necessary, along with my question about the above code I wrote, it would be GREATLY appreciated. thanks in advance!

    EDIT: couldn't get to sleep, so I decided to optimize it.

    Code:
    void EncryptRecord(char *szRec, unsigned long nLen, char *szKey)
    {
    	unsigned long i;
    	char *p;
    
    	p = szKey;
    
    	for(i = 0; i < nLen; i++) {
    		if(!(*p)) /* szKey is guaranteed to be NULL terminated */
    			p = szKey;
    
    		*szRec++ = *szRec ^ *p++;
    	}
    }
    small and fast, just the way I like it. now just don't ask how I am going to get the ASCII value addition in there, without my mind exploding. lol.
    Last edited by Bleech; 11-10-2006 at 04:05 AM.

    Intel Core 2 Quad Q6600 @ 2.40 GHz
    3072 MB PC2-5300 DDR2
    2 x 320 GB SATA (640 GB)
    NVIDIA GeForce 8400GS 256 MB PCI-E

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory leak
    By aruna1 in forum C++ Programming
    Replies: 3
    Last Post: 08-17-2008, 10:28 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. getting a headache
    By sreetvert83 in forum C++ Programming
    Replies: 41
    Last Post: 09-30-2005, 05:20 AM
  4. Quack! It doesn't work! >.<
    By *Michelle* in forum C++ Programming
    Replies: 8
    Last Post: 03-01-2003, 11:26 PM
  5. easy if you know how to use functions...
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 01-31-2002, 06:34 AM

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