Quoting the C++03 standard, §4.7/3 (Integral Conversions):

If the destination type is signed, the value is unchanged if it can be represented in the destination type (and bit-field width); otherwise, the value is implementation-defined.

Because the result is implementation-defined, by definition it is impossible for there to be a truly portable solution.

Answer from ildjarn on Stack Overflow
🌐
Learn C++
learncpp.com › cpp-tutorial › introduction-to-type-conversion-and-static_cast
4.12 — Introduction to type conversion and static_cast – Learn C++
October 21, 2021 - The variable itself is not affected ... the same value even after we’ve cast its value to an int. ... Signed integral values can be converted to unsigned integral values, and vice-versa, using a static cast....
Discussions

Unsigned int to int and vice-versa - C++ Forum
You should be able to implicitly cast from unsigned to signed and vice versa. P.S. when I say implicitly I mean like · Though you can also safely explicitly cast using the static_cast like you are. ... Thanks, that clears things up. @giblit It was more about using the > operator between an int and ... More on cplusplus.com
🌐 cplusplus.com
static_cast<unsigned> - Post.Byes - Bytes
Re: static_cast< unsigned> ... static_cast(c) is the same thing (with added semantics) as *static_cast(&c) so you can see you're basically intepreting memory a different way.... More on post.bytes.com
🌐 post.bytes.com
July 23, 2005
[C++] static_cast'ing a negative value to an unsigned int
Is the result of following line const unsigned int a = static_cast ( -1 ); well defined? I.e. does the standard make any guarantees as to what the outcome will be, or does this invoke undefined behavior? I've scanned the C++ standard for a clear statement on this matter bu More on gamedev.net
🌐 gamedev.net
3
October 28, 2009
Static cast from unsigned int64 to int could be problematic
valhalla/src/tyr/serializers.cc · Shouldn't it be "writer.Int64(static_cast(alertc_code));" More on github.com
🌐 github.com
1
October 26, 2023
🌐
Cppreference
en.cppreference.com › w › cpp › language › static_cast.html
static_cast conversion - cppreference.com
August 18, 2024 - union U { int a; double b; } u; void* x = &u; // x's value is “pointer to u” double* y = static_cast<double*>(x); // y's value is “pointer to u.b” char* z = static_cast<char*>(x); // z's value is “pointer to u”
🌐
Cplusplus
cplusplus.com › forum › beginner › 148544
Unsigned int to int and vice-versa - C++ Forum
You should be able to implicitly cast from unsigned to signed and vice versa. P.S. when I say implicitly I mean like · Though you can also safely explicitly cast using the static_cast like you are. ... Thanks, that clears things up. @giblit It was more about using the > operator between an int and unsigned int that was giving (permissible) warnings, I'm using the cast to just avoid that minor complaint from it.
🌐
GeeksforGeeks
geeksforgeeks.org › c++ › static_cast-in-cpp
static_cast in C++ - GeeksforGeeks
July 11, 2025 - This is the simplest type of cast that can be used. It is a compile-time cast. It does things like implicit conversions between types (such as int to float, or pointer to void*), and it can also call explicit conversion functions.
🌐
Post.Byes
post.bytes.com › home › forum › topic
static_cast<unsigned> - Post.Byes - Bytes
July 23, 2005 - > e.g. static_cast<uns igned>(static_c ... char to unsigned char (on this implementation) converts it to the range 0 to 255 and then the cast to unsigned int leaves it in that 0 to 255 range....
🌐
Microsoft Learn
learn.microsoft.com › en-us › cpp › cpp › type-conversions-and-type-safety-modern-cpp
Type conversions and type safety | Microsoft Learn
June 19, 2025 - static_cast returns an error if the compiler detects that you are trying to cast between types that are completely incompatible. You can also use it to cast between pointer-to-base and pointer-to-derived, but the compiler can't always tell whether ...
🌐
GameDev.net
gamedev.net › forums › topic › 551720-c-static_casting-a-negative-value-to-an-unsigned-int
[C++] static_cast'ing a negative value to an unsigned int - General and Gameplay Programming - GameDev.net
October 28, 2009 - I don't know whether it's well defined, but the only behavior I've ever seen from compilers is to simply reinterpret the bits in that case. Since -1 is 0xFFFFFFFF in two's complement, casting it to an unsigned int results in 4,294,967,295.
Find elsewhere
🌐
GitHub
github.com › valhalla › valhalla › issues › 4353
Static cast from unsigned int64 to int could be problematic · Issue #4353 · valhalla/valhalla
October 26, 2023 - valhalla/src/tyr/serializers.cc Line 428 in 5b3cd97 writer.Int(static_cast (alertc_code)); Shouldn't it be "writer.Int64(static_cast (alertc_code));"?
Author   cduvenhorst
🌐
Reddit
reddit.com › r/cpp_questions › "signed integers are two's complement" => is static_cast between uint64_t and int64_t safe?
r/cpp_questions on Reddit: "Signed Integers are Two's Complement" => is static_cast between uint64_t and int64_t safe?
December 2, 2022 -

Would these asserts now always be true?

#include <cstdint>
#include <cassert>
#include <limits>

int main()
{
  {
    int64_t i1 = std::numeric_limits<int64_t>::max();
    uint64_t u = static_cast<uint64_t>(i1);
    int64_t i2 = static_cast<int64_t>(u);
    assert(i1 == i2);
  }
  {
    uint64_t u1 = std::numeric_limits<uint64_t>::max();
    int64_t i = static_cast<int64_t>(u1);
    uint64_t u2 = static_cast<uint64_t>(i);
    assert(u1 == u2);
  }
  return 0;
}

Based on the implementation of "Signed Integers are Two's Complement" (P1236R1/P0907R4) by the compiler

🌐
Reddit
reddit.com › r/cplusplus › how do i turned unsigned char into int in c++?
r/Cplusplus on Reddit: How do I turned unsigned char into int in C++?
December 17, 2019 -

I am currently working on a project to manipulate ppm files which contain unsigned char in the fourth line if code that represent the rgb values and I don't know how to translate them into integers so I can match them to their rgb values.

🌐
Java2s
java2s.com › ref › cpp › cpp-static-cast-to-unsigned-int.html
C++ static_cast to unsigned int
#include <iostream> #include <iomanip> int main()/*w ww . j a va2s .c o m*/ { const unsigned int R {0x00FF0000}; const unsigned int G {0x0000FF00}; const unsigned int B {0x000000FF}; enum class Color :unsigned int{ Red = R, Green = G, Yellow = R | G, Purple = R | B, Blue = B, Black = 0, White = R | G | B }; Color color1 {Color::Yellow}; Color color2 {Color::Purple}; Color color3 {Color::Green}; unsigned int color {static_cast<unsigned int>(color1)}; // Get the enumerator value std::cout << std::setw(38) << "The components of color1 (yellow) are:" << " Red:" << std::setw(3) << ((color & R) >> 1
🌐
Microsoft Learn
learn.microsoft.com › en-us › cpp › cpp › static-cast-operator
static_cast Operator | Microsoft Learn
August 3, 2021 - However, the resulting char may not have enough bits to hold the entire int value. Again, it is left to the programmer to verify that the results of a static_cast conversion are safe. The static_cast operator can also be used to perform any implicit conversion, including standard conversions and user-defined conversions. For example: // static_cast_Operator_3.cpp // compile with: /LD /GR typedef unsigned char BYTE; void f() { char ch; int i = 65; float f = 2.5; double dbl; ch = static_cast<char>(i); // int to char dbl = static_cast<double>(f); // float to double i = static_cast<BYTE>(ch); }
🌐
Cplusplus
cplusplus.com › doc › tutorial › typecasting
Type-cast
The only guarantee is that a pointer cast to an integer type large enough to fully contain it (such as intptr_t), is guaranteed to be able to be cast back to a valid pointer. The conversions that can be performed by reinterpret_cast but not by static_cast are low-level operations based on reinterpreting the binary representations of the types, which on most cases results in code which is system-specific, and thus non-portable.
🌐
Linux Hint
linuxhint.com › static_cast-cpp
Static_cast C++
February 9, 2022 - Linux Hint LLC, [email protected] 1210 Kelly Park Circle, Morgan Hill, CA 95037 Privacy Policy and Terms of Use
🌐
Florida State University
cs.fsu.edu › ~lacher › courses › COP3330 › lectures › cppreview › slide15.html
Explicit Type Conversion: Casting
Signed Family = {char, short, int, long, float, double, long double} Unsigned Family = {unsigned char, unsigned short, unsigned int, unsigned long}
Top answer
1 of 2
8

You can static_cast from an int to an unsigned, which performs the appropriate conversion. But what you have is a pointer; this pointer points to a region of memory that is to be interpreted as an int. Dereferencing the pointer yields the int's value. static_cast will not convert between pointers to unrelated types.

int i = 0;
int* ip = &i;
int j = *ip; // treat the region pointed to as an int

To treat the memory as unsigned under dereference, you need to reinterpret the memory; that's what reinterpret_cast does.

int i = 0;
int* ip = &i;
unsigned j = *reinterpret_cast<unsigned*>(ip); // treat the region pointed to as unsigned

You shouldn't do this, however. reinterpret_cast does exactly what it says: reinterprets the memory as a different type. If the bit pattern doesn't match what would be expected for that type, the result will differ from the conversion performed by static_cast<unsigned>. This can be the case on a system where signed integers are not represented using 2's complement, for instance.

The correct approach is to dereference the int* then static_cast<unsigned> the result of that:

int i = 0;
int* ip = &i;
unsigned j = static_cast<unsigned>(*ip); // treat the region pointed to as int
                                         // then convert the int to unsigned

To summarise: static_cast will perform the integer conversion, but it won't re-interpret a memory region as a different type. That's what reinterpret_cast is for.

2 of 2
2

You would have to use:

unsigned *j = static_cast<unsigned*>(static_cast<void*>(&i));

this is what you can read here at point 5, or simply use: reinterpret_cast:

int i = 0;
unsigned *j = reinterpret_cast<unsigned*>(&i);

often instead of using cast-s programmers use unions as below:

union {
  int n;
  unsigned char bt[4];
} data;
data.n = i;
data.bt[3] = 0xff;

As it was said in comments, using union for casting is undefined behaviour - even if it is supported by most compilers : you can read more on this here: Is using an union in place of a cast well defined? and here Is using an union in place of a cast well defined?

🌐
Cplusoop
cplusoop.com › programming-cplus › module2 › cplus-improves-casts.php
C++ improves casts (Making them safer)
// Using Explicit Type #include <iostream > int main(){ const unsigned int feet_per_yard{3}; const unsigned inches_per_foot{12}; double length {}; // Length as decimal yards unsigned int yards{}; // Whole yards unsigned int feet {}; // Whole feet unsigned int inches {}; // Whole inches std::cout << "Enter a length in yards as a decimal: "; std::cin >> length; // Get the length as yards, feet, and inches yards = static_cast<unsigned int>(length); feet = static_cast<unsigned int>((length - yards)*feet_per_yard); inches = static_cast<unsigned int> (length*feet_per_yard *inches_per_foot) % inches_per_foot; std::cout << length << " yards converts to " << yards << " yards " << feet << " feet " << inches << " inches."
🌐
Xsprogram
xsprogram.com › content › how-does-one-safely-static-cast-between-unsigned-int-and-int.html
How does one safely static_cast between unsigned int and int?
... The static_cast operator doesn’t ... behavior will result. Therefore, the above cast from unsigned int to int will yield unpredictable results if the value of the unsigned int is greater than the maximum value a signed int can hold....