display byte array as string

A

alessio211734

Using c language what's the best way to display a byte array as char *

An example byte array 10 255 10 I want display as char* "0A FF 0A" in
hex.

Thanks in advance.
 
P

P. Lepin

alessio211734 said:
Using c language what's the best way to display a byte array as char *

An example byte array 10 255 10 I want display as char* "0A FF 0A" in
hex.

Since I'm reading this in clc++, I'll assume you meant C++, not C. Well,
here's one way, ought to be good enough.

#include <algorithm>
#include <iostream>
#include <ostream>
typedef unsigned char byte;template<typename t>struct d:std
::unary_function<t, void>{void operator() (t const&x){std::
cout<<x;}};template<typename t>struct h:std::unary_function
<t,byte>{byte operator() (t const&x){int f=55+(x&15);return
f<65?f-7:f;}};template<class t,typename f,typename g>struct
hd :std::unary_function<t, void>{void operator()(t const&x)
{f h;g d;h(d(x>>4));h(d(x));h(' ');}};

int main(){
byte a[]={1,8,10,15,16,29,31,32,127,128,200,255,10};
std::for_each(a,a+sizeof(a),hd<byte,d<byte>,h<byte> >());
std::cout<<std::endl;return 0;}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,740
Latest member
AdolphBig6

Latest Threads

Top