PHP Funktionen in C++ mit der STL?
-
Hi,
ich habe 4 Funktionen die ich in C++ haben möchte:
1/ http://de3.php.net/manual/de/function.bin2hex.php
2/ http://de3.php.net/manual/de/function.decoct.php
3/ http://de3.php.net/manual/de/function.ord.php
4/ http://de3.php.net/manual/de/function.chunk-split.phpNur da ich vorher nur im PHP Bereich war und erst jetzt mit C++ richtig angefangen habe, weiß ich noch nicht sehr viel darüber. Aber Funktion 2 habe ich mit stringstream und oct hinbekommen (kennt jemand ne bessere lösung? her damit :D) und Funktion 4 habe ich mit einer For-Schleife hinbekommen (Bessere Lösung, bitte sagen :))
Aber bei 1 und 3 hol ich mir einen Wolf.
Wie sähen die in C++ aus?
-
zu 1.) bin2hex mit STL
#include <iostream> #include <bitset> #include <sstream> #include <stdexcept> std::string bin2hex( const std::string& in ) { using namespace std; bitset< 32 > bs; { stringstream s( in ); if( !(s >> bs) ) throw exception( "Fehler beim Lesen der Binärdaten" ); } stringstream s; if( !(s << hex << bs.to_ulong()) ) throw exception( "Fehler beim Schreiben im Hex-Format" ); return s.str(); } int main() { using namespace std; cout << bin2hex("101101011100") << std::endl; cout << bin2hex("1011100011111100") << std::endl; cout << bin2hex("1000101111010010") << std::endl; return 0; }Das ist zwar nicht exakt die beschriebenen PHP-Funktion, aber so erscheint's mir sinnvoll.
und zu 3.) ord
std::string str = "Hello World"; int asciiWert = int( str[0] ); // wie ord( str )Gruß
Werner