0

How do I convert the hex value of the below enum to a string and store it in a variable.

enum {
    a      = 0x54,
    b,
    c
};

For example

auto value = a;
std::string value_as_hex = to_hex(a);

How do I write to_hex

bradgonesurfing
  • 30,949
  • 17
  • 114
  • 217
  • 6
    https://en.cppreference.com/w/cpp/io/manip/hex#Example – BoBTFish Dec 19 '18 at 14:39
  • 1
    There is no way to figure out if a enum constant was initialized with hex value or in some other way. Thus printing a enum value in hex is no different from printing an arbitrary variable in hex. – HolyBlackCat Dec 19 '18 at 14:40
  • sorry I corrected it ...56 as per the sequence – Anshika Verma Dec 19 '18 at 14:42
  • 4
    Whether you assign a value in decimal (84), hex (0x54) or octal (0124), the value will be stored the same internally. You can only choose to display this value in different ways. – flau Dec 19 '18 at 14:47
  • 1
    Possible duplicate of [c++ cout hex values?](https://stackoverflow.com/questions/479373/c-cout-hex-values) – flau Dec 19 '18 at 15:06
  • 1
    Hexadecimal 56 and decimal 86 (and octal 126, and binary 01010110, and Roman LXXXVI...) denote the same value, the only difference is in their textual form. – molbdnilo Dec 19 '18 at 15:11
  • I can print the value in hex or octal with cout. how do I store (hex value) it in a variable to use further? – Anshika Verma Dec 19 '18 at 15:44
  • Possible duplicate of [Integer to hex string in C++](https://stackoverflow.com/questions/5100718/integer-to-hex-string-in-c) – bradgonesurfing Dec 19 '18 at 15:52

3 Answers3

1

If you want to print the hex value of an enum you can use printf with the %x placeholder. For example

#include <cstdio>

enum Foo {
    a      = 0x54,
    b      = 0xA6,
    c      = 0xFF
};

int main() {
    Foo e;

    e = a;
    printf("%x\n",e);

    e = b;
    printf("%x\n",e);

    e = c;
    printf("%x\n",e);

}

the output of the program is

54
a6
ff
bradgonesurfing
  • 30,949
  • 17
  • 114
  • 217
  • how do I store (hex value) it in a variable to use further? – Anshika Verma Dec 19 '18 at 15:46
  • e is a variable. When I write ``e = a`` the value of the enum `a` is stored in variable `e`. You can then use e sometime later. Please understand that you don't store a 'hex' value. When you write ``a = 0x54`` it is the same as writing ``a =84``. The computer has only one way of storing it. What do you want to do with this number later on? – bradgonesurfing Dec 19 '18 at 15:49
0

How about the following solution?

std::ostringstream str;
str << std::hex << a;
auto x = str.str();
andreee
  • 4,459
  • 22
  • 42
0

You could write your to_hex function to store the hex representation of the value in a string using stringstreams and IO manipulation :

#include <iostream>
#include <sstream>

std::string to_hex(const unsigned a) {
    std::stringstream ss;
    ss << "0x" << std::hex << a;
    return ss.str();
}

int main() {
    unsigned value = 0x1234;
    std::string value_as_hex = to_hex(value);

    std::cout << value_as_hex << "\n";
}

Output:

0x1234
flau
  • 1,328
  • 1
  • 20
  • 36