sandip132
asked on
String to Hex Unicode (JIS)
I am looking for a solution to convert a double byte string into its equivalent hex unicode.
Input : "D O U B L E" //(Japanese or English String)
Output : 0x65ed, 0x8466, 0x82a6, 0x9bf5, 0x6893 //(like this)
Thanks in advance.
Input : "D O U B L E" //(Japanese or English String)
Output : 0x65ed, 0x8466, 0x82a6, 0x9bf5, 0x6893 //(like this)
Thanks in advance.
oops, throw a i++ at the end
That would output the decimal integer values. You'd rather make that
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main () {
// for testing purposes
wstring str = L"D O U B L E";
// usially, you'd use
// getline(wcin,str);
for (string::iterator i = str.begin(); i != str.end(); ++i) {
wcout << setbase(ios_base::hex) << L"0x" << (int) *i << L" ";
}
wcout << endl;
return 0;
}
this code converts ascii string to equivalent hex
change it to unicode yourself :D
regards
change it to unicode yourself :D
regards
CString hex = "";
int length = m_hex.GetLength();
for(int i=0;i<length;i++)
hex += "\\x" + ToHex((BYTE)m_hex[i] + m_add);
ASKER
@csaint00:
No luck!
@jkr
it gives error :
Error 40 error C2440: 'initializing' : cannot convert from 'std::_String_iterator<_El em,_Traits ,_Alloc>' to 'std::_String_iterator<_El em,_Traits ,_Alloc>'
Error 41 error C2678: binary '!=' : no operator found which takes a left-hand operand of type 'std::_String_iterator<_El em,_Traits ,_Alloc>' (or there is no acceptable conversion)
@CSecurity:
CString is not supported!
As I am very new to c++, I guess something missing in code!
No luck!
@jkr
it gives error :
Error 40 error C2440: 'initializing' : cannot convert from 'std::_String_iterator<_El
Error 41 error C2678: binary '!=' : no operator found which takes a left-hand operand of type 'std::_String_iterator<_El
@CSecurity:
CString is not supported!
As I am very new to c++, I guess something missing in code!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Perfect!!!
Thanks jkr.
I made some changes in your code to get a actual Hex string using the decimal values.
One small query: How can I copy my std::string myString into wstring str? (I will open a new question if the solution is more complex.)
I tried :
wchar_t str[1024];
lstrcpyW(str,myString); // This won't work.
lstrcpyW(str,(wchar_t*)myS tring); // Same here.
Thanks again for your help.
@All
Below code works fine to convert chars String to Hex Unicode (JIS).
Thanks jkr.
I made some changes in your code to get a actual Hex string using the decimal values.
One small query: How can I copy my std::string myString into wstring str? (I will open a new question if the solution is more complex.)
I tried :
wchar_t str[1024];
lstrcpyW(str,myString); // This won't work.
lstrcpyW(str,(wchar_t*)myS
Thanks again for your help.
@All
Below code works fine to convert chars String to Hex Unicode (JIS).
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main () {
// for testing purposes
wstring str = L"D O U B L E"; //Use Japanese chars
// usially, you'd use
// getline(wcin,str);
char outStr[100];
for (wstring::iterator i = str.begin(); i != str.end(); ++i) { // 'wstring' instead of 'string' in this line
//wcout << setbase(ios_base::hex) << L"0x" << (int) *i << L" ";
sprintf(outStr,"0x%x",(int) *i);
wcout << setbase(ios_base::hex) << outStr << L" ";
}
wcout << endl;
return 0;
}
ASKER
Thanks jkr. :)
while(input[i] != EOS){
cout << hex << (int)input[i] << " ";
}