| View previous topic :: View next topic |
| Author |
Message |
TraxMate Master Cheater
Reputation: 0
Joined: 01 Mar 2008 Posts: 363
|
Posted: Sun May 08, 2011 8:22 am Post subject: string to hex |
|
|
I have a string in which I want to convert to an array of bytes.
| Code: | int main()
{
std::string strToHex = "DE AD BE EF";
BYTE Bytes[4];
for(unsigned i = 0; i < strToHex.length(); i++)
{
if(strToHex[i] == ' ')
strToHex.erase(i, 1);
}
std::stringstream ss(strToHex);
ss >> std::hex >> Bytes;
for(int i = 0; i < 4; i++)
{
std::cout << Bytes[i];
}
std::cin.get();
return 0;
} |
I want Bytes to contain { 0xDE, 0xAD, 0xBE, 0xEF } but instead it gets like this { 0xD, 0xE, 0xA, 0xD }...
The reason I want to convert a string to hex is because I'm making a program where the user will input an array of byte into a edit control (like you do with CE when you want to search for an AoB).
|
|
| Back to top |
|
 |
Stylo Grandmaster Cheater Supreme
Reputation: 3
Joined: 16 May 2007 Posts: 1073 Location: Israel
|
Posted: Sun May 08, 2011 11:35 am Post subject: |
|
|
Instead of converting the bytes you can set them as
| Code: |
string strHex = "\xDE\xAD\xBE\xEF";
|
the '\x' is an escape character that will reference each byte as the value you see
_________________
Stylo |
|
| Back to top |
|
 |
TraxMate Master Cheater
Reputation: 0
Joined: 01 Mar 2008 Posts: 363
|
Posted: Sun May 08, 2011 12:19 pm Post subject: |
|
|
| I know I could do that but since I don't know the string I need to convert that method doesn't work. Because it is a input from the user, I need to dynamically create an aob from the string.
|
|
| Back to top |
|
 |
Jani Grandmaster Cheater
Reputation: 2
Joined: 29 Dec 2006 Posts: 804
|
Posted: Sun May 08, 2011 12:37 pm Post subject: |
|
|
The solution is to use a temporary integer variable, because if you use a character, the compiler will think you really want to extract a character from the stream: | Code: | #include <sstream>
#include <string>
#include <iostream>
#include <limits>
#include <algorithm>
int main(int argc, char *argv[])
{
std::string str("DE AD BE EF");
int len = ( str.length() - std::count( str.begin(), str.end(), ' ' ) + 1 ) / 2;
unsigned char *bytes = new unsigned char[ len ];
std::stringstream ss(str);
int index = 0;
do {
unsigned int val;
ss >> std::hex >> val;
if( val > std::numeric_limits<unsigned char>::max() )
; // Error.
else
bytes[index++] = val;
} while( ss.good() );
for(int i = 0; i < len; ++i)
std::cout << std::hex << static_cast<unsigned int>(bytes[i]) << " ";
delete [] bytes;
std::cin.sync();
std::cin.ignore();
return EXIT_SUCCESS;
} |
EDIT: Note that I'm using whitespace as a separator. So if you want to really parse DEADBEEF as a whole, you need to mess with num_get IIRC.
EDIT2: Be sure to do more input validation. Checking against the numeric limit isn't enough.
|
|
| Back to top |
|
 |
Stylo Grandmaster Cheater Supreme
Reputation: 3
Joined: 16 May 2007 Posts: 1073 Location: Israel
|
Posted: Sun May 08, 2011 1:43 pm Post subject: |
|
|
| TraxMate wrote: | | I know I could do that but since I don't know the string I need to convert that method doesn't work. Because it is a input from the user, I need to dynamically create an aob from the string. |
So right after the user's input,
insert after each byte ( in the case of string it's two characters ) the '\x' escape character.
_________________
Stylo |
|
| Back to top |
|
 |
Jani Grandmaster Cheater
Reputation: 2
Joined: 29 Dec 2006 Posts: 804
|
Posted: Sun May 08, 2011 1:46 pm Post subject: |
|
|
| Stylo wrote: | So right after the user's input,
insert after each byte ( in the case of string it's two characters ) the '\x' escape character. | You have no idea what you are doing, do you? I'd like to see your working code.
|
|
| Back to top |
|
 |
Stylo Grandmaster Cheater Supreme
Reputation: 3
Joined: 16 May 2007 Posts: 1073 Location: Israel
|
Posted: Sun May 08, 2011 1:49 pm Post subject: |
|
|
What's the big deal of converting "DEADBEEF"
into "\xDE\xAD\xBE\xEF" exactly?
_________________
Stylo |
|
| Back to top |
|
 |
Jani Grandmaster Cheater
Reputation: 2
Joined: 29 Dec 2006 Posts: 804
|
Posted: Sun May 08, 2011 1:58 pm Post subject: |
|
|
| Stylo wrote: | What's the big deal of converting "DEADBEEF"
into "\xDE\xAD\xBE\xEF" exactly? | Because "\xDE\xAD\xBE\xEF" isn't the wanted output. 0xDE, 0xAD, 0xBE, 0xEF is. Try it and you'll see. Also, read up on C++ character constants/escape sequences or whatever you call them.
|
|
| Back to top |
|
 |
Stylo Grandmaster Cheater Supreme
Reputation: 3
Joined: 16 May 2007 Posts: 1073 Location: Israel
|
Posted: Sun May 08, 2011 2:06 pm Post subject: |
|
|
You do realize that "\xDE\xAD\xBE\xEF" and 0xDE, 0xAD, 0xBE, 0xEF treated the same
it's just the string format instead the proper int format
_________________
Stylo |
|
| Back to top |
|
 |
Jani Grandmaster Cheater
Reputation: 2
Joined: 29 Dec 2006 Posts: 804
|
Posted: Sun May 08, 2011 2:35 pm Post subject: |
|
|
| Stylo wrote: | You do realize that "\xDE\xAD\xBE\xEF" and 0xDE, 0xAD, 0xBE, 0xEF treated the same
it's just the string format instead the proper int format | I was saying that if the the string contents are "\xDE\xAD\xBE\xEF" (which is "\\xDE\\xAD\\xBE\\xEF" if you want to input them from a .cpp to a compiler), they're not the same as 0xDE, 0xAD, 0xBE, 0xEF. So, post some proof-of-concept code and I'll admit my mistake. Until then your idea won't work under any circumstances.
Last edited by Jani on Sun May 08, 2011 2:46 pm; edited 1 time in total |
|
| Back to top |
|
 |
TraxMate Master Cheater
Reputation: 0
Joined: 01 Mar 2008 Posts: 363
|
Posted: Sun May 08, 2011 2:40 pm Post subject: |
|
|
| Thanks Jani it works perfectly!
|
|
| Back to top |
|
 |
Stylo Grandmaster Cheater Supreme
Reputation: 3
Joined: 16 May 2007 Posts: 1073 Location: Israel
|
Posted: Sun May 08, 2011 10:13 pm Post subject: |
|
|
| Jani wrote: | | Stylo wrote: | You do realize that "\xDE\xAD\xBE\xEF" and 0xDE, 0xAD, 0xBE, 0xEF treated the same
it's just the string format instead the proper int format | I was saying that if the the string contents are "\xDE\xAD\xBE\xEF" (which is "\\xDE\\xAD\\xBE\\xEF" if you want to input them from a .cpp to a compiler), they're not the same as 0xDE, 0xAD, 0xBE, 0xEF. So, post some proof-of-concept code and I'll admit my mistake. Until then your idea won't work under any circumstances. |
I never said \xDE\xAD\xBE\xEF equals to \\xDE\\xAD\\xBE\\xEF
do you even know what escape characters mean?
_________________
Stylo |
|
| Back to top |
|
 |
Jani Grandmaster Cheater
Reputation: 2
Joined: 29 Dec 2006 Posts: 804
|
Posted: Mon May 09, 2011 2:42 am Post subject: |
|
|
| Stylo wrote: | | I never said \xDE\xAD\xBE\xEF equals to \\xDE\\xAD\\xBE\\xEF | I understood you meant this with this quote: | Stylo wrote: | So right after the user's input,
insert after each byte ( in the case of string it's two characters ) the '\x' escape character. | This is the thing you don't understand.
| Stylo wrote: | | do you even know what escape characters mean? | You still haven't posted any code to prove your point. Until then, I know exactly what those escape characters do and you do not. I can't believe it has taken you a day to write such a simple thing, which is only a few lines of code. Also, you just simply CANNOT escape dynamic input the way you think you can.
So, I'm waiting for you to write a body for this function (no need to do any error checking, the length is just for future): | Code: |
/**
* Converts an std::string to a byte array.
*
* const std::string &s - The input string.
* unsigned char *dst - The output byte array.
* unsigned int len - The length of the output buffer.
*
* @return int - The number of bytes converted.
*/
int toByteArray(const std::string &s, unsigned char *dst, unsigned int len)
{
// Complete me.
} |
My version, without proper input validation or error checking, would be something like: | Code: | #include <sstream>
#include <string>
#include <limits>
int toByteArray(const std::string &s, unsigned char *dst, unsigned int len)
{
std::stringstream ss(s);
unsigned int index = 0;
while( ss.good() && index < len ) {
unsigned int val;
ss >> std::hex >> val;
if( val > std::numeric_limits<unsigned char>::max() )
return index; // Error.
else
dst[index++] = val;
}
return index;
} |
To test the function you can use following code: | Code: | #include <string>
#include <iostream>
#include <algorithm>
int toByteArray(const std::string &s, unsigned char *dst, unsigned int len);
int main(int argc, char *argv[])
{
// This str variable contains the dynamic input by user.
// Now it's static, but let's assume it would be dynamic.
std::string str("DE AD BE EF CA FE BA BE");
int len = ( str.length() - std::count( str.begin(), str.end(), ' ' ) + 1 ) / 2;
unsigned char *bytes = new unsigned char[ len ];
len = toByteArray(str, bytes, len);
std::cout << "The length of the byte array is: " << len << std::endl;
for(int i = 0; i < len; ++i)
std::cout << std::hex << static_cast<unsigned int>(bytes[i]) << " ";
delete [] bytes;
std::cin.sync();
std::cin.ignore();
return EXIT_SUCCESS;
} |
I expect this thread to die when Stylo tries to implement this according to the instructions he has been giving. In other words Stylo won't reply anymore, because he noticed he had no idea about the topic.
|
|
| Back to top |
|
 |
Stylo Grandmaster Cheater Supreme
Reputation: 3
Joined: 16 May 2007 Posts: 1073 Location: Israel
|
Posted: Mon May 09, 2011 5:38 am Post subject: |
|
|
Why are you getting crazy like a damn little girl that never gets what she want?
I'm not even going to post any code since my time is too precious right now and i'm writing this through my iphone in a middle of a class.
You gave him a fine working example and he gets it, end of story
hope you won't explode or something :S
_________________
Stylo |
|
| Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8587 Location: 127.0.0.1
|
Posted: Mon May 09, 2011 5:54 am Post subject: |
|
|
Original question got answered. Take your pissing matches to PMs please.
Locked.
_________________
- Retired. |
|
| Back to top |
|
 |
|