| View previous topic :: View next topic |
| Author |
Message |
iPromise Grandmaster Cheater
Reputation: -1
Joined: 27 Jun 2009 Posts: 529 Location: Canada
|
Posted: Wed Jun 23, 2010 9:53 pm Post subject: Converting string to hex and vice versa in C++ |
|
|
| How would I go about doing this? |
|
| Back to top |
|
 |
bhpianist Cheater
Reputation: 1
Joined: 17 Apr 2010 Posts: 38
|
Posted: Wed Jun 23, 2010 11:08 pm Post subject: |
|
|
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 |
|
 |
hcavolsdsadgadsg I'm a spammer
Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
Posted: Thu Jun 24, 2010 12:16 am Post subject: |
|
|
| std::stringstream + std::dec / std::hex |
|
| Back to top |
|
 |
NoMercy Master Cheater
Reputation: 1
Joined: 09 Feb 2009 Posts: 289
|
Posted: Thu Jun 24, 2010 1:17 am Post subject: |
|
|
| If u googled you will have find some nice code |
|
| Back to top |
|
 |
Slugsnack Grandmaster Cheater Supreme
Reputation: 71
Joined: 24 Jan 2007 Posts: 1857
|
Posted: Thu Jun 24, 2010 6:33 am Post subject: |
|
|
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 |
|
 |
iPromise Grandmaster Cheater
Reputation: -1
Joined: 27 Jun 2009 Posts: 529 Location: Canada
|
Posted: Thu Jun 24, 2010 8:22 am Post subject: |
|
|
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 |
|
 |
Jani Grandmaster Cheater
Reputation: 2
Joined: 29 Dec 2006 Posts: 804
|
Posted: Thu Jun 24, 2010 11:45 am Post subject: |
|
|
| 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 |
|
 |
Slugsnack Grandmaster Cheater Supreme
Reputation: 71
Joined: 24 Jan 2007 Posts: 1857
|
Posted: Thu Jun 24, 2010 11:47 am Post subject: |
|
|
| _ultot_s() as i suggested is the tchar mapped safe _s version of itoa |
|
| Back to top |
|
 |
Jani Grandmaster Cheater
Reputation: 2
Joined: 29 Dec 2006 Posts: 804
|
Posted: Thu Jun 24, 2010 11:49 am Post subject: |
|
|
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 |
|
 |
hcavolsdsadgadsg I'm a spammer
Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
Posted: Thu Jun 24, 2010 3:55 pm Post subject: |
|
|
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 |
|
 |
iPromise Grandmaster Cheater
Reputation: -1
Joined: 27 Jun 2009 Posts: 529 Location: Canada
|
Posted: Thu Jun 24, 2010 5:07 pm Post subject: |
|
|
@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 |
|
 |
hcavolsdsadgadsg I'm a spammer
Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
Posted: Thu Jun 24, 2010 5:33 pm Post subject: |
|
|
| just apply the std::hex modifier like i said earlier. it can't be any simpler. |
|
| Back to top |
|
 |
Deltron Z Expert Cheater
Reputation: 1
Joined: 14 Jun 2009 Posts: 164
|
Posted: Fri Jun 25, 2010 12:14 am Post subject: |
|
|
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 |
|
 |
|