I'm trying to write a small program converting a hex string to ascii, I would like to know if there is a function like
cout << hex << 192;
// this prints out c0
that prints out the ascii chars of strings like:
char *hexToAscii = "48 45 4c 4c 4f 61 57 4f 52 4c 44";
I'm new at c++ programming, still in my learning process so please bear with me..
Any help would be appriciated, including links to resources..
regards
Xenzeo
> Hey all...
>
> I'm trying to write a small program converting a hex string to ascii, I
> would like to know if there is a function like
>
> cout << hex << 192;
> // this prints out c0
>
> that prints out the ascii chars of strings like:
> char *hexToAscii = "48 45 4c 4c 4f 61 57 4f 52 4c 44";
>
> I'm new at c++ programming, still in my learning process so please bear
> with me..
If no C++ answer materialises, never forget that you could always do this:
char hexToAscii[] = "48 45 4c 4c 4f 61 57 4f 52 4c 44";
char *p = strtok(hexToAscii, " ");
while(p != NULL)
{
unsigned long n = strtoul(p, NULL, 16);
printf("%c", (int)n);
p = strtok(NULL, " ");
}
--
Richard Heathfield : bin...@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Thanks... ;-)
#include <algorithm>
#include <ios>
#include <iostream>
#include <string>
std::ostream& func(std::ostream& os, const std::string& s)
{
os.clear();
std::ios::fmtflags f(os.flags());
os << std::hex;
std::copy(s.begin(), s.end(), std::ostream_iterator<int>(os, " "));
os.flags(f);
return os;
}
int main()
{
func(std::cout, "Hello world");
std::cout.put('\n');
return 0;
}
-Mike
I'v got this compile error on this code:
translate2.c++: In function `std::ostream& func(std::ostream&, const
std::string&)':
translate2.c++:11: error: `ostream_iterator' undeclared in namespace `std'
translate2.c++:11: error: parse error before `>' token
I'm using g++ (GCC) 3.3.2 20031218 (Gentoo Linux 3.3.2-r5, propolice-3.3-7)
Another thing, doesnt this convert Ascii to Hex?? I need the oppesed.
-Xenzeo
I forgot to #include <iterator> Sorry.
>
> I'm using g++ (GCC) 3.3.2 20031218 (Gentoo Linux 3.3.2-r5,
propolice-3.3-7)
>
>
> Another thing, doesnt this convert Ascii to Hex?? I need the oppesed.
Sorry again. :-) I must have misunderstood your question.
#include <algorithm>
#include <ios>
#include <iostream>
#include <iterator>
#include <sstream>
#include <string>
std::ostream& func(std::ostream& os, const std::string& s)
{
std::istringstream iss(s);
iss >> std::hex;
std::copy(std::istream_iterator<int>(iss),
std::istream_iterator<int>(),
std::ostream_iterator<char>(os, ""));
return os;
}
int main()
{
func(std::cout, "48 45 4c 4c 4f 61 57 4f 52 4c 44");
std::cout.put('\n');
return 0;
}
-Mike
>
> I forgot to #include <iterator> Sorry.
>
np, i should have cought this my self..
>
> Sorry again. :-) I must have misunderstood your question.
>
> #include <algorithm>
> #include <ios>
> #include <iostream>
> #include <iterator>
> #include <sstream>
> #include <string>
>
> std::ostream& func(std::ostream& os, const std::string& s)
> {
> std::istringstream iss(s);
> iss >> std::hex;
>
> std::copy(std::istream_iterator<int>(iss),
> std::istream_iterator<int>(),
> std::ostream_iterator<char>(os, ""));
>
> return os;
> }
>
> int main()
> {
> func(std::cout, "48 45 4c 4c 4f 61 57 4f 52 4c 44");
> std::cout.put('\n');
> return 0;
> }
>
Thanks, mike..
54 68 61 6e 6b 73 20 61 67 61 69 6e 2c 20 6e 6f 77 20 69 27 76 20 67 6f 74 20 62 6f 74 68 20 70 72 6f 67 73 20 77 6f 72 6b 69 6e 67 2c 20 6d 75 63 68 20 61 70 70 72 69 63 69 61 74 65 64 ;-)