Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


Converting string to hex and vice versa in C++

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
iPromise
Grandmaster Cheater
Reputation: -1

Joined: 27 Jun 2009
Posts: 529
Location: Canada

PostPosted: Wed Jun 23, 2010 9:53 pm    Post subject: Converting string to hex and vice versa in C++ Reply with quote

How would I go about doing this?
Back to top
View user's profile Send private message MSN Messenger
bhpianist
Cheater
Reputation: 1

Joined: 17 Apr 2010
Posts: 38

PostPosted: Wed Jun 23, 2010 11:08 pm    Post subject: Reply with quote

You mean you want 'A' to be converted to 0x41 (asciitable)? Just treat the string as an array of bytes (or wchars).

If you want "10F4" to be converted to {0x10, 0xF4} then that's just string parsing.
Back to top
View user's profile Send private message MSN Messenger
hcavolsdsadgadsg
I'm a spammer
Reputation: 26

Joined: 11 Jun 2007
Posts: 5801

PostPosted: Thu Jun 24, 2010 12:16 am    Post subject: Reply with quote

std::stringstream + std::dec / std::hex
Back to top
View user's profile Send private message
NoMercy
Master Cheater
Reputation: 1

Joined: 09 Feb 2009
Posts: 289

PostPosted: Thu Jun 24, 2010 1:17 am    Post subject: Reply with quote

If u googled you will have find some nice code
Back to top
View user's profile Send private message
Slugsnack
Grandmaster Cheater Supreme
Reputation: 71

Joined: 24 Jan 2007
Posts: 1857

PostPosted: Thu Jun 24, 2010 6:33 am    Post subject: Reply with quote

if for some reason you don't want to use the c++ libraries..

http://forum.cheatengine.org/viewtopic.php?t=508587

Code:
      for( int i = 0; i < nSize; i++ ) {
        sprintf_s( &szBuffer[i*3], 3, "%02X", lpData[i] );
        szBuffer[ ( i + 1 )*3 - 1 ] = ' ';
      }


for the other way, use _ultot_s()
Back to top
View user's profile Send private message
iPromise
Grandmaster Cheater
Reputation: -1

Joined: 27 Jun 2009
Posts: 529
Location: Canada

PostPosted: Thu Jun 24, 2010 8:22 am    Post subject: Reply with quote

Thanks guys, I get it now.

I actually like the stringstream method better, its more simple to get.

edit

I rather prefer using itoa() with a radix of 16 to convert my strings into hexadecimal format.

Code:
#include <Windows.h>
#include <stdlib.h>
#include <iostream>

using namespace std;

int main()
{
   char Name[] = "Hussain";
   char Buffer[] = "";

   for (int i = 0; i < _countof(Name); i++)
   {
      itoa(Name[i], Buffer, 16);

      cout << Buffer << endl;
   }

   return 0;
}
Back to top
View user's profile Send private message MSN Messenger
Jani
Grandmaster Cheater
Reputation: 2

Joined: 29 Dec 2006
Posts: 804

PostPosted: Thu Jun 24, 2010 11:45 am    Post subject: Reply with quote

iPromise wrote:
I rather prefer using itoa() with a radix of 16 to convert my strings into hexadecimal format.
Overflowww! This is what happens when you don't use the safe _s functions. So, stick to the stringstream method or learn to code C properly.
Back to top
View user's profile Send private message
Slugsnack
Grandmaster Cheater Supreme
Reputation: 71

Joined: 24 Jan 2007
Posts: 1857

PostPosted: Thu Jun 24, 2010 11:47 am    Post subject: Reply with quote

_ultot_s() as i suggested is the tchar mapped safe _s version of itoa
Back to top
View user's profile Send private message
Jani
Grandmaster Cheater
Reputation: 2

Joined: 29 Dec 2006
Posts: 804

PostPosted: Thu Jun 24, 2010 11:49 am    Post subject: Reply with quote

Also, if you want only to print the buffer, try std::hex with std::cout:
Code:
#include <iostream>
#include <string>

int main()
{
   std::string s = "Hussain";

   for(unsigned int i=0; i<s.length(); ++i)
      std::cout << std::hex << static_cast<unsigned int>(s[i]) << std::endl;

   return EXIT_SUCCESS;
}
Back to top
View user's profile Send private message
hcavolsdsadgadsg
I'm a spammer
Reputation: 26

Joined: 11 Jun 2007
Posts: 5801

PostPosted: Thu Jun 24, 2010 3:55 pm    Post subject: Reply with quote

you can also use stringstream for all kinds of type conversion.

just write a templated version and in a couple lines of code, you'll have a nice generic function that handles all kinds of conversion, easy.

Code:
template <typename out, typename in>
out isntthisshitneato(const in& t)
{
   std::stringstream   ss;
   out               dong;

   ss << t;   
   ss >> dong;
   return dong;
}

int main()
{
   std::cout << "Hello!";

   float f1         = isntthisshitneato<float, int>(int(152));
   float f2         = isntthisshitneato<float, std::string>(std::string("152.0213"));
   std::string str1   = isntthisshitneato<std::string, float>(float(12.015f));
   std::string str2   = isntthisshitneato<std::string, int>(int(12));

   return 0;
}
Back to top
View user's profile Send private message
iPromise
Grandmaster Cheater
Reputation: -1

Joined: 27 Jun 2009
Posts: 529
Location: Canada

PostPosted: Thu Jun 24, 2010 5:07 pm    Post subject: Reply with quote

@slovach

I know what you mean, but my objective is not to turn a int into a readable string for instance.

the character 'a' is if i'm correct 0x91 in hexadecimal format, so, my objective is to convert a array of strings into a hexadecimal for output as fast as possible.
Back to top
View user's profile Send private message MSN Messenger
hcavolsdsadgadsg
I'm a spammer
Reputation: 26

Joined: 11 Jun 2007
Posts: 5801

PostPosted: Thu Jun 24, 2010 5:33 pm    Post subject: Reply with quote

just apply the std::hex modifier like i said earlier. it can't be any simpler.
Back to top
View user's profile Send private message
Deltron Z
Expert Cheater
Reputation: 1

Joined: 14 Jun 2009
Posts: 164

PostPosted: Fri Jun 25, 2010 12:14 am    Post subject: Reply with quote

You can easily write your own function to do so:
Code:
#include <stdio.h>

int Convert(unsigned int Decimal, int Base, char* Converted)
{
   static const char Charset[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";

   int Index = 0;

   do
   {
      Converted[Index++] = Charset[Decimal % Base];
   } while (Decimal /= Base);

   for (int i = Index / 2 - 1; i >= 0; i--)
   {
      Converted[i] ^= Converted[Index - i - 1];
      Converted[Index - i - 1] ^= Converted[i];
      Converted[i] ^= Converted[Index - i - 1];
   }

   Converted[Index] = '\0';

   return Index;
}

int main()
{
   char Text[32];
   Convert(3735928559, 16, Text);
   printf("0x%08X\n", Text);

   getchar();

   return 0;
}

Any base until 36 and it can be used for even more bases. simply add some checks for extra saftey because I've coded this real quick...
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites