Its late, and maybe my output above was backwards for your wants. If so you can reverse it easily enough (generate it reversed, don't generate wrong and fix).
All you have to do to reverse it is iterate the string forward, but you have to start at some offset for non multiples of 4, so length %4 as the starting position I think (again, getting late & sleepies).
Either way, is the above, using only string, OK for you now? Or is your own working right now?
I am not sure that his stringstream is doing anything cout won't?
Correct. I was playing with things and could have removed stringstream and just used cout. I was first building the required extract before display. It could be:
1 2
//std::cout << "x = " << x << ", y = " << y << ", extract = ";
std::cout << "0x" << std::hex << std::setw(4) << std::setfill('0') << std::uppercase << res << '\n';
Hello jonnin, JLBorges and others,
Thank you very much for your great C++ code and ideas.
I am currently working with String version of jonnin and some functions & techniques of JLBorges and others and also using my C++ skills.
I am resolving issues with the C++ cross compiler, microcontroller etc.
I will let you all know as soon as I am successful in compiling and running this program.
Thanks again a bunch.
In JL Borges following code:
Arm Mbed C++ compiler is giving error by saying:
exceptions, throw are not allowed.
Case statement returning char array is crashing the program.
I think memory allocation is not happening for binary digits array?
hex_str_to_bin_str function is also not working. Crashing the microcontroller program.
Any help in rewrite of these please?
Looks like, writing a one function using C char arrays is only way,
by converting 12 hex chars (+ 1 null char) i.e. 48 binary chars array to
40 binary chars (+1 null char) array after ignoring left 1 bit and right 7 bits?
Many, many thanks for your great help.
I can't use malloc etc.
I can't return char * etc.
I only need 12 hex chars input array (that is 48 binary digits),
and 10 output hex chars (40 binary digits).
I have to declare in my calling function, 12 + 1 fixed array size hex array and pass to the function as input parameter.
And called function creates 40 + 1 digits/chars fixed size array. Then in this function I will send the 40 chars array to Serial/USB port.
All I need is compact C code lines to ignore 8 bits and regroup 40 digits to 10 hex chars array please.
It's not the compiler. malloc etc have got nothing to do with the compiler. They're all about the provided library functions. It is quite possible that an environment doesn't have any available RAM - hence no heap and hence no dynamic memory functions.
Writing for embedded systems is not the same as writing for say Windows/Linux et al.
Many thanks to lastchance for the following function and main program:
However, the stoi function call is causing Arm Mbed embedded microcontroller to give following abnormal termination error.
#include <iostream>
#include <string>
usingnamespace std;
void extract( const string &str, int &x, int &y )
{
int i = stoi( str.substr( 4, 2 ), 0, 16 );
x = ( i >> 6 ) & 1;
y = i & 63;
}
int main()
{
int x, y;
extract( "F0FFC3", x, y );
cout << x << '\n' << y << '\n';
}
1
3
Edit & Run
@AlexCantor,
Is there any web-based interface where we can simulate these ARM embedded systems? Failing that ... some documentation.
It is difficult for a PC-user to see how much C++ your "C++" compiler can actually cope with. It looks like most of the code that you have accepted is C, not C++. At best it is C++98. You seem to be able to use std::string (so, not C), but not many of its functions.