Hello JLBorges, seeplus, lastchance, johnnin:
Wow.
Thank you all very much.
Thank you all soooo much.
I can never thank you enough.
You are all the best, brilliant and brightest experts of Modern C++.
Thank you, Thank you all for the most elegant C++ code gems you all have showed to me in this thread.
I am learning a lot from all of yours, gold of C++ code gems.
I need a some more help please.
I wrote program shown later below, based on couple of lines of excellent code written above by seeplus and below is a code fragment from it:
1 2 3 4 5 6
|
std::ostringstream oss;
oss << "0x" << std::hex << std::setw(4) << std::setfill('0') << std::uppercase << bitRange("F0FFC3", 7, 16);
cout << oss.str() << endl;
oss.str(""); oss.clear();
|
This code is using ostringstream object oss to format the hex string output values.
I need this output format of hex string values.
I am writing an embedded C++ program, that transmits text using serial communication protocol.
Cout, OSS etc are not allowed by my embedded software compiler.
May I seek your help in re-writing following two lines of code by using C++ string feature and without using streams, please?
1 2
|
oss << "0x" << std::hex << std::setw(4) << std::setfill('0') << std::uppercase << bitRange("F0FFC3", 7, 16);
cout << oss.str() << endl;
|
Above code gives following hex output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
|
#include <iostream>
#include <string>
#include <cstdlib>
#include <iomanip>
#include <sstream>
using namespace std;
unsigned long long bitRange(const string& hexstring, int lowbit, int numbits)
{
unsigned long long i = stoull(hexstring, 0, 16);
return (i >> lowbit) & ((1ull << numbits) - 1);
}
int main()
{
cout << hex << bitRange("F0FFC3", 7, 16) << '\n';
cout << hex << bitRange("8F0F0F0F0FC3", 7, 40) << '\n';
std::ostringstream oss;
oss << "0x" << std::hex << std::setw(4) << std::setfill('0') << std::uppercase << bitRange("F0FFC3", 7, 16);
cout << oss.str() << endl;
oss.str(""); oss.clear();
oss << "0x" << std::hex << std::setw(4) << std::setfill('0') << std::uppercase << bitRange("8F0F0F0F0FC3", 7, 40);
cout << oss.str() << endl;
oss.str(""); oss.clear();
}
|
Above program is giving following output:
e1ff
1e1e1e1e1f
0xE1FF
0x1E1E1E1E1F
|
Last two lines of