-1

I need to convert an integer int parameter to an hexadecimal unsigned char buffer[n]. If integer is for example 10 then the hexadecimal unsigned char array should be 0x0A

To do so I have the following code:

int parameter;
std::stringstream ss;
unsigned char buffer[];

ss << std::hex << std::showbase << parameter;
typedef unsigned char byte_t;

byte_t b = static_cast<byte_t>(ss); //ERROR: invalid static_cast from type ‘std::stringstream {aka std::basic_stringstream<char>}’ to type ‘byte_t {aka unsigned char}’

buffer[0]=b;

Does anyone know how to avoid this error? If there is a way of converting the integer parameter into an hexadecimal unsigned char than doing first: ss << std::hex << std::showbase << parameter; that would be even better.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Cristina
  • 21
  • 1
  • 5
  • FYI: an unsigned char is just a single character. Your wording is quite confusing. – Karoly Horvath Sep 06 '15 at 21:57
  • Numbers aren't hexadecimal. `10` and `0xa` represent the same number. If you want a character that is the hexadecimal representation of ten, that would be `'a'`. – molbdnilo Sep 06 '15 at 21:58
  • Yes, I want the same number represented in hexadecimal and assigned to a unsigned char buffer[n]. Appending '0x' is done to show the base, and it is a requirement. Any idea of how to do it? – Cristina Sep 06 '15 at 22:08
  • `byte_t b = static_cast(ss);` is completely nonsensical. Do you want to have something like a `union Int32Bytes { int ival; byte_t bytes[4]; };` actually? – πάντα ῥεῖ Sep 06 '15 at 22:28
  • I know it's C++ but I still feel the urge to get this primitive task done with a dumb `snprintf(buffer, sizeof(buffer), "0x%08x", parameter)`. Change the type of buffer from `unsigned char[]` to `char[]`. Make sure that buffer is large enough (at least 11 bytes). If you want uppercase hex digits then replace `%08x` to `%08X` in the format string. – pasztorpisti Sep 06 '15 at 23:02

2 Answers2

1

Consulting my psychic powers it reads you actually want to have a int value seen in it's representation of bytes (byte_t). Well, as from your comment

I want the same number represented in hexadecimal and assigned to a unsigned char buffer[n].

not so much psychic powers, but you should note hexadecimal representation is a matter of formatting, not internal integer number representation.

The easiest way is to use a union like

union Int32Bytes { 
    int ival; 
    byte_t bytes[sizeof(int)]; 
};

and use it like

Int32Bytes x;
x.ival = parameter;

for(size_t i = 0; i < sizeof(int); ++i) {
    std::cout << std::hex << std::showbase << (int)x.bytes[i] << ' ';
}

Be aware to see unexpected results due to endianess specialities of your current CPU architecture.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
0

Problem 1: buffer is of undetermined size in your snippet. I'll suppose that you have declared it with a sufficient size.

Problem 2: the result of your conversion will be several chars (at least 3 due to the 0x prefix). So you need to copy all of them. This won't work with an = unless you'd have strings.

Problem 3: your intermediary cast won't succeed: you can't hope to convert a complex stringstream object to a single unsigned char. Fortunately, you don't need this.

Here a possible solution using std::copy(), and adding a null terminator to the buffer:

const string& s = ss.str();
*copy(s.cbegin(), s.cend(), buffer)='\0'; 

Live demo

Christophe
  • 68,716
  • 7
  • 72
  • 138