Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I know how to print hex to screen, but I dunno how to store it to variable.
C++
buff = "Hello";
for(i=0; i<strlen(buff); i++){
   kar[i] = buff[i];
   cout << "0x" << hex << (int)kar[i] << endl;
}

So, how do I store each hex number to a variable -- as long as I know, the type is unsigned char.
Thanks
Posted
Updated 18-Jun-15 21:56pm
v4
Comments
Richard MacCutchan 19-Jun-15 4:28am    
Why would you want to? The only time you need to convert it to hex is when you want to display it somewhare. So just store it in its native form, char, int, float etc.

One of the way is: use sprintf:
http://www.cplusplus.com/reference/cstdio/sprintf[^].

For the hexadecimal string representation of a 1-byte character, you need two hexadecimal digit, so the format will be "%02x", such as in this example: http://stackoverflow.com/questions/11070183/sprintf-how-to-print-hexadecimal-number-with-leading-0-to-have-width-2[^].

—SA
 
Share this answer
 
Comments
rudy-peto 18-Jun-15 23:34pm    
Thanks! A bit helpful. But it don't work when like this:
kar[i] = sprintf("%02x", buff[i]);
cout << kar[i];
rudy-peto 18-Jun-15 23:41pm    
Upss.. sorry, My mistake.. I should not print kar[i], It should be looks like this.

sprintf(buffer, "%02x", buff[i]);
cout << buffer << endl;

It works! Thanks..!
rudy-peto 19-Jun-15 0:09am    
But anyway, I store each hex into buffer.. But how do I store it to array of pointer, so it looks like:

buffer[0] = 0x55
buffer[1] = 0x4E
buffer[2] = 0x50
Sergey Alexandrovich Kryukov 19-Jun-15 1:44am    
Read sprintf specification and you will see. The buffer has nothing to do with the hex. You need another buffer to hex string on output. Say, it can be array of pointers each pointing to the buffer for 2-character string (or 3-character, with 'x'). Or it can be one buffer with N stings located one after another. You pointer "buffer" is the input one, and you also need output buffers. Allocate them. You simply need to provide a 2-3 character buffer per each sprintf call, no matter how the buffers are arranged. This is not a matter of formatting data, this is a matter of basics of using pointers/array.

Well, you can read this, if you have any problems with these topics: http://www.cplusplus.com/doc/tutorial/arrays.

—SA
rudy-peto 19-Jun-15 1:48am    
Thanks for the references. Yep, I got a bit confuse about array / pointer, how to pass a value or an address between them..huff
Rudi-peto asked:
But anyway, I store each hex into buffer... But how do I store it to array of pointer, so it looks like: …
Please see my comments to Solution 1.

You really need to get comfortable with pointers and arrays, that's it. Let me show one very simple and memory-efficient approach. Let's say, you input buffer of bytes you called buff in the question has the length count:
C++
int count = strlen(buff);

Before you proceed, you have to allocate the buffer for output string, call it output. Instead of allocating each string separately and using some pointers to an array of pointers ("jagged array"), let's simply put then all in one buffer. How much memory you need? Each hexadecimal string will take 4 bytes, if we count 'x': also two half-byte hexadecimal digits and null, because we are messing up with those stupid null-terminated strings. So, for whole output buffer, you will need just count*4 bytes:
C++
char * output = new char[count * 4];

// do something...

delete[] output;

In the loop, you can call sprintf (Solution 1) count times. Let's do simple "manual" pointer arithmetic. First string passed as the first sprintf call will be at the very end of the buffer, so the pointer would be the same as output. To get the pointer to next string, increment this pointer adding exactly 4. As the compile-time type of the element of the array output is 1-byte char, the compiler will interpret this 4 as 4 bytes. So, in each loop iteration, the string pointer for each string will be equal to output + 4 * index, where index is the loop control variable. This way, you will populate the array with count strings of the same length.

Essentially, I wrote the complete pseudo-code for you. You populate all the output hex string in the loop. Later on, you can use this output array to access any of these string by index, using exactly the same pointer arithmetic.

Alternatively, you can also use "2D" arrays, as explained here, understanding that in C++ "2D" is just a metaphor, the way to interpret the same pointer: http://www.cplusplus.com/doc/tutorial/arrays[^].

Also note that the term "2D", "3D" and so one is nothing but some jargon. Mathematically correct work is "rank". In the strict mathematical terminology, all those finite sets are always zero-dimensional objects. Only continuums can have such property as "dimension". From elementary linear algebra and analytical geometry, you can learn that continuous linear spaces can have some integer dimensions. And the geometrical object can have dimensions of 1, 2, 3 and more if they are the manifolds in some continuous spaces; for other geometrical objects, the dimension can be fractional or at all undefined. But this is a different story…

—SA
 
Share this answer
 
v9
There is a C++ way for that.
The ostringstream class[^] provides, for strings, the same interface provided by cout for standard output.

Try, for instance:
C++
 #include <iostream>
 #include <string>
 #include <sstream>
 #include <vector>
 using namespace std;

int main()
{
  string buff = "Hello";

  vector < string > v;

  for (size_t n=0; n < buff.size(); ++n)
  {
    ostringstream oss;
    oss << "0x" << hex << (int) buff[n];
    v.push_back( oss.str() );
  }

  for (size_t n=0; n<v.size(); ++n)
  {
    cout << v[n] << endl;
  }
}


Or, with an updated compiler
C++
 #include <iostream>
 #include <string>
 #include <sstream>
 #include <vector>
 using namespace std;

int main()
{
  string buff = "Hello";

  vector < string > v;

  for (auto c: buff)
  {
    ostringstream oss;
    oss << "0x" << hex << static_cast<int>(c);
    v.push_back( oss.str() );
  }

  for (auto s: v)
    cout << s << endl;
}
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900