1

I have trouble to find a good solution for the following scenario:

A write function takes a const char, which is given as an user-input through cin.get(ch) by calling write(ch). ch is a char.

while (c.active()) 
{
 char ch;
 cin.get(ch); // blocking wait for standard input

 if (ch == 3) // ctrl-C to end program
   break;
 c.write(ch);
}

This works fine, but I would like to modify it, so that it is possible to make an input like: "0A00CD88" or "0A 00 CD 88" and use it as input for write, which is using it as input for boost::asio::buffer.

In order to do so, I can use C/C++ and boost.

Thanks to anyone who shares knowledge on this matter!

Jook
  • 4,564
  • 3
  • 26
  • 53
  • To be more specific: "0A 00 CD 88" is a command I need to send as such. So I have to be able to put in "0A00CD88" or with spaces - i don't care really - but it has to transform into a stream of chars of according value to 0A,00,CD,88. I am sorry, this was not that clear in my initial question. @denys: thanks - your solution helped and directed me to an improvement of my question – Jook Sep 03 '12 at 12:53
  • I found a similar example [here](http://stackoverflow.com/questions/3221170/how-to-turn-a-hex-string-into-an-unsigned-char-array), but I am a bit slow today - still trying to adapt this somehow, any ideas? – Jook Sep 03 '12 at 13:22

2 Answers2

3

That is your solution:

just replace

std::string hex_chars("E8 48 D8 FF FF 8B 0D");

with

std::string hex_chars;
std::getline(std::cin, hex_chars);

P.S. I hope I've understood your question correctly :)

Community
  • 1
  • 1
denys
  • 2,437
  • 6
  • 31
  • 55
  • I am still about to understand how it all works, but it works as I asked for it to work - only instead of bytes.push_back(c) I am using c.write(ch). Thank you denys, James McNellis and Gbps! – Jook Sep 03 '12 at 14:10
2

Next code will solve your 1st scenario (without spaces)

int x;
std::cin >> std::hex >> x;

Use hexadecimal base

denys
  • 2,437
  • 6
  • 31
  • 55