| View previous topic :: View next topic |
| Author |
Message |
Miaurice Newbie cheater
Reputation: 0
Joined: 03 Jul 2009 Posts: 21 Location: Germany
|
Posted: Sun Jan 22, 2012 10:41 am Post subject: [c++] Converting String to Char[] |
|
|
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 |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8587 Location: 127.0.0.1
|
Posted: Sun Jan 22, 2012 12:19 pm Post subject: |
|
|
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 |
|
 |
Miaurice Newbie cheater
Reputation: 0
Joined: 03 Jul 2009 Posts: 21 Location: Germany
|
Posted: Sun Jan 22, 2012 12:56 pm Post subject: |
|
|
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 |
|
 |
mgr.inz.Player I post too much
Reputation: 222
Joined: 07 Nov 2008 Posts: 4438 Location: W kraju nad Wisla. UTC+01:00
|
Posted: Sun Jan 22, 2012 2:52 pm Post subject: |
|
|
| 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 |
|
 |
manc Grandmaster Cheater
Reputation: 1
Joined: 16 Jun 2006 Posts: 551
|
Posted: Sun Jan 22, 2012 5:03 pm Post subject: |
|
|
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 |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8587 Location: 127.0.0.1
|
Posted: Sun Jan 22, 2012 7:52 pm Post subject: |
|
|
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 |
|
 |
|