CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Join Date
    Nov 2005
    Posts
    281

    Hex Conversion? (HELP PLEASE!)

    Hello I've been working on some encryption, and I have to buy the output encryption into a .cpp file, so you can imagine this has been a massive hassle since some encrypted characters are from the windows-1251 charset and everything else.

    Can someone help me? I need to be able to convert the output from the encryption into something I can easily implement into another .cpp as a string and have it compile fine.

    I was thinking converting it to hex would be the easiest. So if someone could help, that'd be great.

    And how would I convert it back from hex when the encrypted string is used in the .cpp?

  2. #2
    Join Date
    Nov 2007
    Location
    Birmingham, England
    Posts
    157

    Re: Hex Conversion? (HELP PLEASE!)

    Well converting to hex isnt too hard:
    Just read in 1,2,3 or 4 bytes at a time and use the itoa() function to do the conversion for you.

    In terms of putting it into a c++ file, why not just quote it and put a "\x" before every pair. so:
    Code:
    0619ab01
    becomes the quoted string:
    "\x06\x19\xab\x01"
    \x tells c++ to interpret the next two characters as a hex representation for a single byte.
    So the compiler will convert everything back to single bytes for you at compile time and the string will have the same byte sequence as the original encrypted string.

    Hope this helps
    Last edited by couling; November 13th, 2009 at 08:32 PM. Reason: My spelling sucks
    Signature
    Please use: [ code ][/ code ] tags and reasonably correct tabbing. They really help us read your code
    End Signature

  3. #3
    Join Date
    Nov 2005
    Posts
    281

    Re: Hex Conversion? (HELP PLEASE!)

    Alright I'm a bit lost on how to do that. How would I have the code do that for the following example?:

    S-t¨:&#165äï64-8t
    Would it be able to handle strings like that with the crazy characters?
    Last edited by Rehorav; November 13th, 2009 at 08:55 PM.

  4. #4
    Join Date
    Nov 2007
    Location
    Birmingham, England
    Posts
    157

    Re: Hex Conversion? (HELP PLEASE!)

    Quote Originally Posted by Rehorav View Post
    Would it be able to handle strings like that with the crazy characters?
    A string is just a sequence of numbers. They only become charicters when you try to display them to a user.

    You mentioned that you need to put these into a c++ file and you're right to realise that you can't just put any charicter in one of those. But c++ was written in such a way that you can use byte sequences in strings that c++ cant read in a .cpp file. It uses "escape sequences" to let you do so.

    Commonly used escape sequences are things such as \n for new line and \" for double quote.
    Code:
    So the string:
    "Hello\nWorld\nThis is\"beuty\""
    
    Is compiled as:
    
    Hello
    World
    This is "beuty"
    Fortunetly, for the really ugly strings c++ lets you just name the charicter number in hex after the escape sequence \x.
    Code:
    So the string:
    "\x21\x2b"
    
    Will be compiled as
    
    !+
    Thats because "!" has the byte code 33 (21 in hex) and "+" is 43 (2B in hex). this will let you encode any byte codes you need.

    You will want to do this programatically otherwise its going to be a pain to find all the charicter codes by hand.
    Last edited by couling; November 13th, 2009 at 09:41 PM.
    Signature
    Please use: [ code ][/ code ] tags and reasonably correct tabbing. They really help us read your code
    End Signature

  5. #5
    Join Date
    Apr 2004
    Location
    England, Europe
    Posts
    2,492

    Re: Hex Conversion? (HELP PLEASE!)

    Quote Originally Posted by Rehorav View Post
    I was thinking converting it to hex would be the easiest.
    Each byte can be converted to hex one at a time using sprintf:

    Code:
    char buffer[3];
    sprintf(&buffer[0], "%0X", byte);
    If that's what you meant.
    Last edited by Zaccheus; November 14th, 2009 at 06:00 AM.
    My hobby projects:
    www.rclsoftware.org.uk

  6. #6
    Join Date
    Nov 2005
    Posts
    281

    Re: Hex Conversion? (HELP PLEASE!)

    Quote Originally Posted by couling View Post
    A string is just a sequence of numbers. They only become charicters when you try to display them to a user.

    You mentioned that you need to put these into a c++ file and you're right to realise that you can't just put any charicter in one of those. But c++ was written in such a way that you can use byte sequences in strings that c++ cant read in a .cpp file. It uses "escape sequences" to let you do so.

    Commonly used escape sequences are things such as \n for new line and \" for double quote.
    Code:
    So the string:
    "Hello\nWorld\nThis is\"beuty\""
    
    Is compiled as:
    
    Hello
    World
    This is "beuty"
    Fortunetly, for the really ugly strings c++ lets you just name the charicter number in hex after the escape sequence \x.
    Code:
    So the string:
    "\x21\x2b"
    
    Will be compiled as
    
    !+
    Thats because "!" has the byte code 33 (21 in hex) and "+" is 43 (2B in hex). this will let you encode any byte codes you need.

    You will want to do this programatically otherwise its going to be a pain to find all the charicter codes by hand.

    Do you think you can show me example code like you said? I've been trying to sort itoa() to use with the example string I gave ( S-t¨:¥)äï64-8t ) and I can't get it to work. I think I'm doing something very wrong.

  7. #7
    Join Date
    Nov 2005
    Posts
    281

    Re: Hex Conversion? (HELP PLEASE!)

    Alright I got it working with the following code:

    Code:
    char* TurnToHex ( char* string )
    {
            int length;
            printf ( "Hexadecimal Output: " );
            for(length = 0; length < strlen ( string ); length++ )
            {
                printf ( "&#37;.X ", string [ length ] );
                LogFile << string [ length ];
            }
    
            return string;
    }
    But when I use it on the string:
    S-t&#168;:&#165&#228;&#239;64-8t
    The output is:
    53 2D 74 FFFFFFA8 3A FFFFFFA5 29 FFFFFFE4 FFFFFFEF 36 34 2D 38 74
    I just need to know how to get rid of the FFFFFF's, because the following 2 characters are correct. Then it'd basically be perfect.

    Also, when I try to get it to write to a file it comes out as:
    S-t&#168;:&#165&#228;&#239;64-8t
    So how could I preserve it in hex format to be put into a .txt so I could just put it into the .cpp rather than having to copy and paste it all from the cmd prompt?
    Last edited by Rehorav; November 16th, 2009 at 04:06 AM.

  8. #8
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    Re: Hex Conversion? (HELP PLEASE!)

    You can use convert a hex string to stringstream then to int or whatever you want.
    Thanks for your help.

  9. #9
    Join Date
    Nov 2005
    Posts
    281

    Re: Hex Conversion? (HELP PLEASE!)

    Quote Originally Posted by Peter_APIIT View Post
    You can use convert a hex string to stringstream then to int or whatever you want.
    Can you please elaborate a little more? And will that take care of the excessive F's with the odd unicode characters?

  10. #10
    Join Date
    Apr 2004
    Location
    England, Europe
    Posts
    2,492

    Re: Hex Conversion? (HELP PLEASE!)

    Quote Originally Posted by Rehorav View Post
    I just need to know how to get rid of the FFFFFF's, because the following 2 characters are correct. Then it'd basically be perfect.
    Quote Originally Posted by Zaccheus View Post
    Code:
    char buffer[3];
    sprintf(&buffer[0], "&#37;0X", byte);
    My hobby projects:
    www.rclsoftware.org.uk

  11. #11
    Join Date
    Nov 2005
    Posts
    281

    Re: Hex Conversion? (HELP PLEASE!)

    Quote Originally Posted by Zaccheus View Post
    Well, when I tried using printf with "%0X" it gave me the exact same output. But when I tried using the following code:

    Code:
    char* TurnToHex ( char* string )
    {
            int length;
            for(length = 0; length < strlen(string); length++)
            {
                sprintf( &string[length], "%0X" );
            }
            return string;
    }
    it just crashed. And I can't have byte as the 3rd param in sprintf because the compiler tells me:
    1>.\Main.cpp(30) : error C2275: 'byte' : illegal use of this type as an expression
    1> c:\program files\microsoft sdks\windows\v6.0a\include\rpcndr.h(154) : see declaration of 'byte'
    How do you have byte defined?

  12. #12
    Join Date
    Apr 2004
    Location
    England, Europe
    Posts
    2,492

    Re: Hex Conversion? (HELP PLEASE!)

    Oh, sorry, my mistake ... does "&#37;02X" work?

    As for the crash, you are using the input buffer as the output buffer. It all looks very wrong.

    The 'byte' in my example was just a variable name.
    Last edited by Zaccheus; November 16th, 2009 at 06:18 AM.
    My hobby projects:
    www.rclsoftware.org.uk

  13. #13
    Join Date
    Nov 2005
    Posts
    281

    Re: Hex Conversion? (HELP PLEASE!)

    It gives the exact same output .

    But maybe the compiler will recognize FFFFFFA8 as A8 anyways? I'll test as soon as I can, but I can't get it to write to a .txt. So how could I have it write to a .txt in the format couling showed above? I know how to have anything else write to file, but the hex just refuses to write.

    Example:
    "\x21\x2b"
    I can make it come out as x<whatever>x<whatever>, but how do I make it come out as \x<whatever> etc?
    Last edited by Rehorav; November 16th, 2009 at 11:43 AM.

  14. #14
    Join Date
    Apr 2004
    Location
    England, Europe
    Posts
    2,492

    Re: Hex Conversion? (HELP PLEASE!)

    Odd, I cannot get printf to format it correctly either. Perhaps I've spent too much time in C#.

    So I've written a function which does it instead:

    Code:
    void ctox(unsigned char byte, char* buffer)
    {
        struct Local
        {
            static char convert(unsigned int nibble)
            {
                if(nibble < 0xA) return '0' + (nibble - 0x0);
                else             return 'A' + (nibble - 0xA);
            }
        };
    
        unsigned int hiNibble = (byte & 0xF0) >> 4;
        unsigned int loNibble = (byte & 0x0F) >> 0;
    
        buffer[0] = Local::convert(hiNibble);
        buffer[1] = Local::convert(loNibble);
        buffer[2] = 0;
    }
    Code:
    void f()
    {
        char byte = '¥';
        char buffer[3];
    
        ctox(byte, &buffer[0]);
    
        printf("%s", &buffer[0]);
    }
    My hobby projects:
    www.rclsoftware.org.uk

  15. #15
    Join Date
    Nov 2005
    Posts
    281

    Re: Hex Conversion? (HELP PLEASE!)

    Nevermind, I got it settled. I just used fprintf and "\\" to show the \. Thank you all for the help, I'm about to test and make sure it works with the decryption.

    Edit:\\ Zaccheus I just saw what you posted ( I had the new reply page open for a long time ). I just made the code like:


    Code:
    void ctox(unsigned char byte, char* buffer)
    {
    	struct Local
    	{
    		static char convert(unsigned int nibble)
    		{
    			if(nibble < 0xA) return '0' + (nibble - 0x0);
    			else             return 'A' + (nibble - 0xA);
    		}
    	};
    
    	unsigned int hiNibble = (byte & 0xF0) >> 4;
    	unsigned int loNibble = (byte & 0x0F) >> 0;
    
    	buffer[0] = Local::convert(hiNibble);
    	buffer[1] = Local::convert(loNibble);
    	buffer[2] = 0;
    }
    
    
    int main() 
    {
    	//ofstream LogFile( "Log.txt", fstream::in | fstream::out | fstream::app | fstream::ate );
    	char* Test = "S-t&#168;:&#165;)&#228;&#239;64-8t";
    	int length;
    	char buffer[3];
    
    	for ( length = 0; length < strlen ( Test ); ++length )
    	{
    		ctox(Test[length], &buffer[0]);
    		printf("\\x&#37;s ", &buffer[0]);
    	}
    
    	//LogFile.close();
    	cin.get();
    	return 0; 
    }
    And now it works perfectly . Thank you dear sir!
    Last edited by Rehorav; November 16th, 2009 at 05:22 PM.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured