Solved

String to Hex Unicode (JIS)

Posted on 2009-04-10
8
867 Views
Last Modified: 2012-06-27
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.
0
Comment
Question by:sandip132
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 3

Expert Comment

by:csaint00
ID: 24115784
basically,

while(input[i] != EOS){

cout <<  hex << (int)input[i] << " ";

}
0
 
LVL 3

Expert Comment

by:csaint00
ID: 24115786
oops, throw a i++ at the end
0
 
LVL 86

Expert Comment

by:jkr
ID: 24116132
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;
}

Open in new window

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 17

Expert Comment

by:CSecurity
ID: 24122625
this code converts ascii string to equivalent hex
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);

Open in new window

0
 
LVL 12

Author Comment

by:sandip132
ID: 24127698
@csaint00:
No luck!

@jkr
it gives error :
Error      40      error C2440: 'initializing' : cannot convert from 'std::_String_iterator<_Elem,_Traits,_Alloc>' to 'std::_String_iterator<_Elem,_Traits,_Alloc>'      


Error      41      error C2678: binary '!=' : no operator found which takes a left-hand operand of type 'std::_String_iterator<_Elem,_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!
0
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 24131015
Sorry, my fault, that sampel should of course use wide string iterators. Try teh following:
#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 (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" ";
  }
 
  wcout << endl;
 
  return 0;
}

Open in new window

0
 
LVL 12

Author Comment

by:sandip132
ID: 24134618
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*)myString); // Same here.

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;
}

Open in new window

0
 
LVL 12

Author Closing Comment

by:sandip132
ID: 31568887
Thanks jkr. :)
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Getting IP address 8 101
How to programmatically differentiate between C and Java 10 213
How to copy an image file into clipboard C/C++? 1 218
Find Visual Studio Tools 2 121
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question