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 


[c++] Converting String to Char[]

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
Miaurice
Newbie cheater
Reputation: 0

Joined: 03 Jul 2009
Posts: 21
Location: Germany

PostPosted: Sun Jan 22, 2012 10:41 am    Post subject: [c++] Converting String to Char[] Reply with quote

Good evening Cheat-Engine-Community!

How can I convert something like this:
(Text in a TextBox) to an array?
(Equal to this: )?

Thanks in advance! (And sorry, if this has been asked before..)
Back to top
View user's profile Send private message
atom0s
Moderator
Reputation: 205

Joined: 25 Jan 2006
Posts: 8587
Location: 127.0.0.1

PostPosted: Sun Jan 22, 2012 12:19 pm    Post subject: Reply with quote

You could do something like this:

Code:

#include <Windows.h>
#include <iostream>
#include <sstream>
#include <string>

std::string HexStrToDefine( const std::string& strInput )
{
   std::stringstream ss;
   ss << "unsigned char m_DataOutput[] = { ";

   for( int x = 0; x < strInput.length(); x += 3 )
   {
      ss << "0x" << strInput.substr( x , 2 );

      if( (x + 3) < strInput.length() )
         ss << ", ";
   }

   ss << " };";

   return ss.str();
}

int __cdecl main( int argc, TCHAR* argv[] )
{
   std::string strInputString   = "DE AD BE EF";
   std::string strOutputString = HexStrToDefine( strInputString );

   return 0;
}


This has no error checking and requires your bytes to be 2 chars each.

So you cannot do things like:
DE AD B E A F

_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
Miaurice
Newbie cheater
Reputation: 0

Joined: 03 Jul 2009
Posts: 21
Location: Germany

PostPosted: Sun Jan 22, 2012 12:56 pm    Post subject: Reply with quote

Thanks, but as I interpret this piece of code, it takes an std::string and returns an std::string
Kind of useless for me..
Back to top
View user's profile Send private message
mgr.inz.Player
I post too much
Reputation: 222

Joined: 07 Nov 2008
Posts: 4438
Location: W kraju nad Wisla. UTC+01:00

PostPosted: Sun Jan 22, 2012 2:52 pm    Post subject: Reply with quote

Miaurice wrote:
Thanks, but as I interpret this piece of code, it takes an std::string and returns an std::string
Kind of useless for me..


Try this:
c_str()

_________________
Back to top
View user's profile Send private message MSN Messenger
manc
Grandmaster Cheater
Reputation: 1

Joined: 16 Jun 2006
Posts: 551

PostPosted: Sun Jan 22, 2012 5:03 pm    Post subject: Reply with quote

Take in the string
Create a pointer to an array of char's,
Fill said array with "0x" + the parts from your string
return pointer

I could be totally fuckin wrong though, nomsayin'

Edit: I mean in that case you at least have a handle on it...
as far as a direct conversion..Im too high to help with that atm, will provide code later though if you ask for it

_________________
Back to top
View user's profile Send private message
atom0s
Moderator
Reputation: 205

Joined: 25 Jan 2006
Posts: 8587
Location: 127.0.0.1

PostPosted: Sun Jan 22, 2012 7:52 pm    Post subject: Reply with quote

The way you presented the thread made it seem you wanted the string version and not the ability to use the hex. Here is a way I tossed together, minimal error checking and it can throw exceptions. You are also responsible for freeing the pointer when you are done with it.

Code:


#include <Windows.h>
#include <algorithm>
#include <string>

unsigned char* HexToData( const std::string& strInput )
{
   int nSpaces = std::count_if( strInput.begin(), strInput.end(),
      [](const char c) { return ( c == ' ' ); }
   );

   char* pszInput   = (char*)strInput.c_str();
   char* pszNext   = NULL;
   char* pszToken   = strtok_s( pszInput, " ", &pszNext );
   
   if( pszToken == NULL )
      return NULL;

   unsigned char* pszData = new unsigned char[ nSpaces + 1 ];

   while( pszToken != NULL )
   {
      unsigned char chData = (unsigned char)strtol( pszToken, NULL, 16 );
      *pszData = chData;
      ++pszData;

      pszToken = strtok_s( NULL, " ", &pszNext );
   }
   pszData -= ( nSpaces + 1 );

   return pszData;
}

int __cdecl main( int argc, TCHAR* argv[] )
{
   std::string strInputString = "DE AD BE EF FF F";

   unsigned char* lpData = HexToData( strInputString );

   if( lpData != NULL )
      delete lpData;

   return 0;
}

_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
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