HextoCString conversion
-
Hi All, Please let me know some hints to convert a hex value to a string.. Regards, Spk521
-
Hi All, Please let me know some hints to convert a hex value to a string.. Regards, Spk521
-
It depends what you mean by convert. Do you want the hex value to appear as hex in the string (e.g. 0x3739 --> "0x3739") or do you want to display it as characters (e.g. 0x3739 --> "79")?
I must get a clever new signature for 2011.
-
Hi, Please try this.. char str[] = "01-15-43-43-34-37-37-41"; char delims[]="-"; CString strAscii = _T(""); CString strModelN0 = _T(""); char *result= NULL; result = strtok(str,delims); while(result != NULL) { result = strtok(NULL, delims); if(NULL != result) { strAscii.Format(_T("%c"),hexToAscii(result)); strModelN0 += strAscii; } } char hexToAscii(char *Num) { char hex[10]= "0x"; strcat(hex,Num); return strtol(hex, NULL, 16); } Regards, Spidy
-
Hi, Please try this.. char str[] = "01-15-43-43-34-37-37-41"; char delims[]="-"; CString strAscii = _T(""); CString strModelN0 = _T(""); char *result= NULL; result = strtok(str,delims); while(result != NULL) { result = strtok(NULL, delims); if(NULL != result) { strAscii.Format(_T("%c"),hexToAscii(result)); strModelN0 += strAscii; } } char hexToAscii(char *Num) { char hex[10]= "0x"; strcat(hex,Num); return strtol(hex, NULL, 16); } Regards, Spidy
-
Hi All, Please let me know some hints to convert a hex value to a string.. Regards, Spk521
You can do this easily using std::ostringstream[^]. Here is a small example for you:
#include <iostream>
#include <string>
#include <iomanip>
#include <sstream>int main()
{
// Test numbers - num1 declared as HEX, num2 as DEC
const unsigned int num1 = 0x1234, num2 = 1234;// We will use std::ostringstream for the conversion std::ostringstream oss; // First num to Hex // "0x" is optional if you don't need it you can use "oss << std::hex << num1;" oss << "0x" << std::hex << num1; std::string strFirstNumHex = oss.str(); // Clear the content of the string buffer for later use oss.str(std::string()); // Second num to Hex // "0x" is optional if you don't need it you can use "oss << std::hex << num2;" oss << "0x" << std::hex << num2; std::string strSecondNumHex = oss.str(); // Clear the content of the string buffer for later use oss.str(std::string()); // First num to Dec oss << std::dec << num1; std::string strFirstNumDec = oss.str(); // Clear the content of the string buffer for later use oss.str(std::string()); // Second num to Dec oss << std::dec << num2; std::string strSecondNumDec = oss.str(); // This line prints: Numbers in HEX: 0x1234, 0x4d2 Numbers in DEC: 4660, 1234 std::cout << "Numbers in HEX: " << strFirstNumHex << ", " << strSecondNumHex << "\\t" << "Numbers in DEC: " << strFirstNumDec << ", " << strSecondNumDec; std::cin.get(); return 0;
}
As you can see from the above example
strFirstNumHex
andstrSecondNumHex
contain hex string representations of the test integers (strFirstNumHex = 0x1234
andstrSecondNumHex = 0x4d2
).strFirstNumDec
andstrSecondNumDec
contain decimal string representations of the test integers. If you don't need the"0x"
prefix for your Hex strings you can remove this prefix from specified lines in above example. I hope this helps. :) NOTE: For clearing the content of the stream buffer I usually use:oss.str("");
, but it messes the formatting of the code block and I changed it tooss.str(s
-
Hi, Please try this.. char str[] = "01-15-43-43-34-37-37-41"; char delims[]="-"; CString strAscii = _T(""); CString strModelN0 = _T(""); char *result= NULL; result = strtok(str,delims); while(result != NULL) { result = strtok(NULL, delims); if(NULL != result) { strAscii.Format(_T("%c"),hexToAscii(result)); strModelN0 += strAscii; } } char hexToAscii(char *Num) { char hex[10]= "0x"; strcat(hex,Num); return strtol(hex, NULL, 16); } Regards, Spidy
-
You can do this easily using std::ostringstream[^]. Here is a small example for you:
#include <iostream>
#include <string>
#include <iomanip>
#include <sstream>int main()
{
// Test numbers - num1 declared as HEX, num2 as DEC
const unsigned int num1 = 0x1234, num2 = 1234;// We will use std::ostringstream for the conversion std::ostringstream oss; // First num to Hex // "0x" is optional if you don't need it you can use "oss << std::hex << num1;" oss << "0x" << std::hex << num1; std::string strFirstNumHex = oss.str(); // Clear the content of the string buffer for later use oss.str(std::string()); // Second num to Hex // "0x" is optional if you don't need it you can use "oss << std::hex << num2;" oss << "0x" << std::hex << num2; std::string strSecondNumHex = oss.str(); // Clear the content of the string buffer for later use oss.str(std::string()); // First num to Dec oss << std::dec << num1; std::string strFirstNumDec = oss.str(); // Clear the content of the string buffer for later use oss.str(std::string()); // Second num to Dec oss << std::dec << num2; std::string strSecondNumDec = oss.str(); // This line prints: Numbers in HEX: 0x1234, 0x4d2 Numbers in DEC: 4660, 1234 std::cout << "Numbers in HEX: " << strFirstNumHex << ", " << strSecondNumHex << "\\t" << "Numbers in DEC: " << strFirstNumDec << ", " << strSecondNumDec; std::cin.get(); return 0;
}
As you can see from the above example
strFirstNumHex
andstrSecondNumHex
contain hex string representations of the test integers (strFirstNumHex = 0x1234
andstrSecondNumHex = 0x4d2
).strFirstNumDec
andstrSecondNumDec
contain decimal string representations of the test integers. If you don't need the"0x"
prefix for your Hex strings you can remove this prefix from specified lines in above example. I hope this helps. :) NOTE: For clearing the content of the stream buffer I usually use:oss.str("");
, but it messes the formatting of the code block and I changed it tooss.str(s
-
S p k 521 wrote:
...convert hex to its corresponding ascii value...
This makes no sense? Are you wanting to convert from base-16 to base-10?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius