Jump to content
ciccio.greco88

Conversion dec to hex

Recommended Posts

How do you see, whether a value has a decimal or hexadecimal representation? Most probably, it is stored in some form of binary in the memory. ;-)

But you can directly assign both hex literals (C++ feature) and hex strings (SystemC datatypes feature) to sc_uint variables:

#include <systemc.h>
int sc_main(int,char*[])
{
 sc_uint<32> pippo;
 // ...
 pippo =  0x2A;   // assign from hex literal
 pippo = "0x0A2"; // assign from hex string

 std::cout << pippo // decimal value by default
    << "="
    << pippo.to_string(SC_HEX) // SystemC hex representation
    << " (0x" << std::hex << pippo <<")" // C++ hex representation (with manual '0x' prefix)
    << std::endl;
 return 0;
}

Gotcha: In SystemC, hex strings are signed literals by default. When dealing with unsigned variables (sc_uint, et.al), you should make sure that the hex string contains a leading 0 to avoid unexpected sign extension, when converting from the hex string.

Greetings from Oldenburg,

Philipp

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×