C++
--
Questions
--
Followers
Top Experts
Is there a way to convert a string "ASCII" to its Decimal & hexadecimal value. For example
"Hello this is a test"
Hex:
48 65 6c 6c 6f 20 74 68 69 73 20 69 73 20 61 20 74 65 73 74
Binary:
01001000 01100101 01101100 01101100 01101111 00100000 01110100 01101000 01101001 01110011 00100000 01101001 01110011 00100000 01100001 00100000 01110100 01100101 01110011 01110100 00100000 01100011 01101111 01101110 01110110 01100101 01110010 01110100 01110011 00100000 01110100 01101111 00001101 00001010 00001101 00001010
Thanks all
Zero AI Policy
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
ASKER
Is there a way to convert a string "ASCII" to its *Binary* & hexadecimal value.
print it, store it in a string ???
// Binary
for(int i = 0; i < strlen(str); ++i)
{
for(int k = 7; k >= 0; ++k)
cout << ((str[i] >> k) & 1);
cout << " ";
}
cout << endl;
// Hex
cout << setf(iob_base::hex) << str << endl;
EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
ASKER
ASKER
Get a FREE t-shirt when you ask your first question.
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
ASKER
Your code looks simple and short, what do I write that in MFC.
Thanks
But 1 problem is
for(int k = 7; k >= 0; ++k)
cout << ((str[i] >> k) & 1);
nees to be
for(int k = 7; k >= 0; --k)
cout << ((str[i] >> k) & 1);
or you have an infinite (nearly) loop.
also that might work incorrectly on some systems. You probalby better make sure the character is treated as unsigned, like
cout << ((unsigned char)(str[i] >> k) & 1);
>>cout << setf(iob_base::hex) << str << endl;
is hopeless. the hex I/O manipulator won't work on a stringj, only numbers. You could use it in a loop that converts each byte (character) of the string to a number (unsigned character) and then print that character in hex. Like
for (int i = 0; i < strlen(str); ++i)
cout << hex << (unsigned char) str[i];
note also that using strlen() in the loop condition is extremely inefficient. it makes an order N operation into an order n^2 operation.
Sorry... just early morning code :) And yes, the strlen does add unneeded complexity to a otherwise simple function. I thought that the
As for Bermuda, if you are using MFC you probably don't have a console window anyhow. In that case you would need code that generates a string (or something similar) and passes it back to a function to be output in a message box or to set some window text to. Nietod's code will work for this, and should be fairly easy to add to you app.
EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
they both return a newly allocated string and free the supplied one (thou thats easy to change). so you can do whatever needs doing with the returned string
char *StrToHexStr(char *str) {
char *newstr = new char[(strlen(str)*2)+1];
char *cpold = str;
char *cpnew = newstr;
while('0円' != *cpold) {
sprintf(cpnew, "%02X", (char)(*cpold++));
cpnew+=2;
}
*(cpnew) = '0円';
delete [] str;
return(newstr);
}
char *StrToBinStr(char *str) {
char *newstr = new[strlen(str)*8)+1];
char *cpold = str;
char *cpnew = newstr;
while('0円' != *cpold) {
char mask = (char)0x80;
int i;
for( i = 0; i < 8; i++) {
if((*cpold & mask) == 0) {
*(cpnew++) = '0';
} else {
*(cpnew++) = '1';
}
mask = mask >> 1;
//the right shift isn't always defined
//so we must make sure the new MSB is 0 ourselves
mask = mask & 0x7F;
}
cpold++;
}
*(cpnew) = '0円';
delete [] str;
return(newstr);
}
>> sprintf(cpnew, "%02X", (char)(*cpold++));
since cpold is defined to be a pointer to a character, there is no need to use that cast. However on some platforms that will produce incorrect results. You want to make sure that the character is treated as unsigned so you do need a cast, but to unsigned character.
sprintf(cpnew, "%02X", (unsigned char)(*cpold++));
>> //the right shift isn't always defined
>> //so we must make sure the new MSB is 0 ourselves
>> mask = mask & 0x7F;
The exact behavior of the right shift is explicitly defined for an unsigned chracter. (its implimentation defined for signed characters). You should note that is why I used unsigned characters in my code.
C++
--
Questions
--
Followers
Top Experts
C++ is an intermediate-level general-purpose programming language, not to be confused with C or C#. It was developed as a set of extensions to the C programming language to improve type-safety and add support for automatic resource management, object-orientation, generic programming, and exception handling, among other features.