Jun 1, 2012 at 4:02pm Jun 1, 2012 at 4:02pm UTC
hi
did any one know how can i fill a char table with hexadecimal number to be read after byte by byte
i find the exemple in c language :
char y[256] = "\x11\x11\x20\x20";
when i make std::cout << y[0] ; i got 11
So waht is the solution for c++?
thanks
Jun 1, 2012 at 4:25pm Jun 1, 2012 at 4:25pm UTC
now, if i have :
a= 0x11;
b= 0x20;
how can i make them in the char table to be read after byte by byte ?
and thank you :)
Jun 1, 2012 at 4:32pm Jun 1, 2012 at 4:32pm UTC
I'm not sure what you mean by "after byte by byte". You can always insert them into the array after the ones you populated with
y[4]=a; y[5]=b;
If you mean adding them after the end of the defined array at indeces 256 and 257, you need to use std::vector.
http://cplusplus.com/reference/stl/vector/
If you don't mean either of these things, then I don't know what you are asking.
Edit: It would probably be good to learn to use std::vector either way.
Last edited on Jun 1, 2012 at 4:33pm Jun 1, 2012 at 4:33pm UTC
Jun 1, 2012 at 4:38pm Jun 1, 2012 at 4:38pm UTC
lets take an other example :
if a have :
x = 0xc123;
i want to make it in a char table y[256]
and when i make std::cout << y[0]; i got 193 ( 0xc1)
so how can i make it in the char table to be read like that ?
Jun 1, 2012 at 5:31pm Jun 1, 2012 at 5:31pm UTC
Are you looking to convert a sequence of bytes that correspond to the big-endian notation of some integer to that integer?
Write a conversion function, or use the appropriate function from the ntoh* family if your OS provides that.
Something like (assuming y is a vector of bytes of course)
1 2 3
uint16_t x;
for (size_t n=0; n < y.size(); ++n) // std::accumulate() also works
x = (x << CHAR_BIT) | y[n];
or, with ntohs (tested on Linux (little-endian), Solaris, and AIX (big-endian))
1 2 3
uint16_t x;
std::copy(y.begin(), y.end(), reinterpret_cast <char *>(&x));
x = ntohs(x);
Last edited on Jun 1, 2012 at 5:46pm Jun 1, 2012 at 5:46pm UTC