How am I doing so far (Making a function)

This is a discussion on How am I doing so far (Making a function) within the C++ Programming forums, part of the General Programming Boards category; Code: #include <string> #include <atlenc.h> Encrypt(string Encrypt) { /* The part where we convert the ASCII values to Hexadecimal*/ /*We ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    241

    How am I doing so far (Making a function)

    Code:
    #include <string>
    #include <atlenc.h>
    Encrypt(string Encrypt)
    {
    	/* The part where we convert the ASCII values to Hexadecimal*/
    	/*We must declare all our variables*/
    	int Length;
    	Length=(string length(Encrypt));
    	int NewLength;
    	NewLength=(Length*2+1);
    	int SecondEncodeLength;
    	SecondEncodeLength=(NewLength*2+1);
    	string NewString;
    	string SecondEncode;
    	/*End of declaring variables*/
    	/*Now to the encoding Ascii to Hex values*/
    	int Encrypted
    
    	inline bool AtlHexEncode(
    				Encrypt,
    				Length,
    				NewString,
    				NewLength
    				)throw();
    	/*End of Ascii to Hex, now for Hex to Hex which will encode in to an even
     	longer string*/
    	inline bool AtlHexEncode(
    				NewString,
    				NewLength,
    				SecondEncode,
    				SecondEncodeLength
    				)throw();
    	/*Now to convert it from Hex to Oct so then it is all intiger values, I 
    	just hope it doesnt drop the 0 from the beginning of the string 
     	(If it translates to that)when its converted to a int value*/	
    	/*int Encrypted=SecondEncode;  We don't need this so I'm just going to save
    	it for future refrence*/
    I want the string that was inputed to be treated as a c++ string since its easier.
    Wait it wont be converted all into intigers, but if I convert the Hex to Octadecimal (base 8) it WILL become all intiger values.
    but how?
    EDIT: Wait, if I convert it to Oct first then Hex, it will be all intigers and it will become even more obscured
    Last edited by bikr692002; 03-07-2006 at 02:44 PM.

  2. #2
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735

    Lightbulb Not Good

    You don't seem to be getting very far so I helped you out
    Code:
    #include <string>
    #include <iostream>
    using namespace std;
    static char table[] = "0123456789ABCDEF";
    string CharToHex(char Take)
    {
    	int Number = (int)Take; //ascii value
    	string returnv;
    	returnv += table[Number % 16];
    	Number/=16;
    	if (Number !=0)
    		returnv += table[Number];
    	return returnv;
    }
    int main()
    {
    	string input = "Go";
    	string temp;
    	for (unsigned int i = 0; i < input.size(); i++)
    	{
    		temp += CharToHex(input[i]);
    	}
    	cout << temp.c_str();
    }
    EDIT
    I apoligize, the hex is backwards. Just remembered that
    Last edited by MadCow257; 03-07-2006 at 03:26 PM.

  3. #3
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    Code:
    Encrypt(string Encrypt)
    no return type
    Code:
    void Encrypt(string Encrypt) // better
    pass strings by reference
    Code:
    void Encrypt(string& Encrypt) // better still
    don't give functions and variables the same name
    Code:
    void Encrypt(string& srcString) // better still
    pass strings you don't want to change by const reference
    Code:
    void Encrypt(const string& srcString) // now we're talking!
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Well, you need to be able to do a couple of other things first:

    1) Write a function, so that when you call it, it displays "Hello world.".

    2) Write a function that accepts an integer argument. The function should multiply the integer by 2, and then return it's value. Display the new value.
    Last edited by 7stud; 03-07-2006 at 05:13 PM.

  5. #5
    Registered User
    Join Date
    Sep 2005
    Posts
    241
    lol 7stud I'm not that much of a newbie :-P thanks ChaosEngine and Madcow. The problem is right now I can't find a function that will convert from Ascii characters to the Octadecimal equivilants

  6. #6
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    >> The problem is right now I can't find a function that will convert from Ascii characters to the Octadecimal equivilants

    An octadecimal? Kind of like an oxymorodecimal? Well, there's not a function or "tutorials" for everything.

  7. #7
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,156
    Quote Originally Posted by bikr692002
    lol 7stud I'm not that much of a newbie :-P thanks ChaosEngine and Madcow. The problem is right now I can't find a function that will convert from Ascii characters to the Octadecimal equivilants
    So write one. Do you know how to convert ASCII characters to Hex or Decimal equivalents? If so, use 0 to 7 instead of 0 to 15/F and 0 to 9.

    And it's Octal... not Octadecimal
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  8. #8
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379
    Heres how octal works:

    Add one, if the number ends with a 7 and is not between 63 and 95, add 2 more. Add 33 once if the number is 63 or greater, but between 63 and 95 just add one period. After that the pattern is goes back to what it started with.

    Bit strange, and thats only up to 127.

    Observe the pattern yourself:
    http://www.lookuptables.com/
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

  9. #9
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    lol 7stud I'm not that much of a newbie :-P thanks ChaosEngine and Madcow. The problem is right now I can't find a function that will convert from Ascii characters to the Octadecimal equivilants
    The code I gave you only needs 3 changes and then it will work for octal (besides the thing about needing to reverse the output)

  10. #10
    Registered User
    Join Date
    Sep 2005
    Posts
    241
    Ehh, I've been broken by someone, he convinced me this is out of my reach due to extreamly large numbers, I think I can solve that by using loops and such and diffrent memory locations or in scientific notation, but alas, not at my skill level.

  11. #11
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    Have you considered using stringstreams to do this conversion? it may make life easier...

    Code:
    #include <iostream>
    #include <iomanip>
    #include <string>
    #include <sstream>
    
    int main()
    {
        int num(15);
        std::stringstream ss;
        std::string result;
    
        ss << std::hex << num << " "
           << std::oct << num << " "
           << std::dec << num << "\n";
        std::getline(ss, result);
        std::cout << result << std::endl;
    }

  12. #12
    Registered User
    Join Date
    Sep 2005
    Posts
    241
    I think I understand that, but like how are you inputting?

  13. #13
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    Quote Originally Posted by bikr692002
    I think I understand that, but like how are you inputting?
    I simplified my example so that it didn't get input. Instead, I initialised "num" with the value of 15. You can get "num" from anywhere you like - The way you normally ask the user for input ( cin >> num; ) ... or from an ifstream... or from another string.

    But as you were originally creating a function, its pretty simple to turn the example into a function which takes an int or a char.


    Note - if your function accepts a char, you will need to convert to an int to get the ASCII value of the char. ie:
    Code:
    string convert(char ch)
    {
       int num = ch;
    
          // do conversion...
    }
    Last edited by Bench82; 03-07-2006 at 07:34 PM.

  14. #14
    C/C++ homeyg's Avatar
    Join Date
    Nov 2004
    Location
    Louisiana, USA
    Posts
    209
    Quote Originally Posted by bikr692002
    Code:
    #include <string>
    #include <atlenc.h>
    Encrypt(string Encrypt)
    {
    	/* The part where we convert the ASCII values to Hexadecimal*/
    	/*We must declare all our variables*/
    	int Length;
    	Length=(string length(Encrypt));
    	int NewLength;
    	NewLength=(Length*2+1);
    	int SecondEncodeLength;
    	SecondEncodeLength=(NewLength*2+1);
    	string NewString;
    	string SecondEncode;
    	/*End of declaring variables*/
    	/*Now to the encoding Ascii to Hex values*/
    	int Encrypted
    
    	inline bool AtlHexEncode(
    				Encrypt,
    				Length,
    				NewString,
    				NewLength
    				)throw();
    	/*End of Ascii to Hex, now for Hex to Hex which will encode in to an even
     	longer string*/
    	inline bool AtlHexEncode(
    				NewString,
    				NewLength,
    				SecondEncode,
    				SecondEncodeLength
    				)throw();
    	/*Now to convert it from Hex to Oct so then it is all intiger values, I 
    	just hope it doesnt drop the 0 from the beginning of the string 
     	(If it translates to that)when its converted to a int value*/	
    	/*int Encrypted=SecondEncode;  We don't need this so I'm just going to save
    	it for future refrence*/
    Umm, no offense but wtf.. Do you even know C++?
    Last edited by homeyg; 03-08-2006 at 07:51 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 12:03 PM
  3. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 08:10 AM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM
  5. Replies: 5
    Last Post: 02-08-2003, 06:42 PM

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