0

Sorry, I have gone through my code and find the place where my problem actually lies.

I hope to cast number in c++ approach. The original variable is unsigned char which I stored in a char variable a. Now I need to cast this a to double while keeping its original unsigned value. How could I do this ?

Edit:
I didn't expect my problem should be so confusing. Here is a brief example

unsigned char a;
double b;
a = 140;
char c = static_cast<char>(a);
b = static_cast<double>(c);
std::cout << b << std::endl;

This prints a result of -116, but what I actually hopes is a double 140. How could I do this then ?

Edit v2:

The scene is like this: I read 1024 bytes from a file with fstream. The 1024 bytes contains int numbers as well as unsigned char numbers. These unsigned char numbers I mean to cast to double types. I simplly read all of them to a std::vector<char> array, and then I need to unpack these numbers. Some part of them are to be cast to double and I met my problem here.

If my problem is brought by abuse of c++, what is the correct way to handle this task please ?

9
  • 1
    Since in char unsigned ch = 129; double d = static_cast<double>(ch); d IS 129. you are doing something different. Commented Oct 23, 2018 at 2:51
  • Casting the value you're describing to a double should not result in -127. Can you edit your question to include the portion of your code where you believe your issue is coming from? Commented Oct 23, 2018 at 2:56
  • 1
    cplusplus.com/doc/tutorial/typecasting Converting to int from some smaller integer type, or to double from float is known as promotion, and is guaranteed to produce the exact same value in the destination type. Commented Oct 23, 2018 at 2:56
  • §7.3.10/2: "The result is exact if possible. If the value being converted is in the range of values that can be represented but the value cannot be represented exactly, it is an implementation-defined choice of either the next lower or higher representable value." So no way positive becoming negative. Commented Oct 23, 2018 at 3:04
  • Please post a minimal reproducible example. Commented Oct 23, 2018 at 3:37

3 Answers 3

1

This works:

b = static_cast<double>(static_cast<unsigned char>(c));

or:

b = static_cast<unsigned char>(c);

or:

b = c < 0 ? c + 256 : c;
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a great deal
0

In c++, char max number is 127, in the assign: char a = 128; you actually get char Min value, which is -128, what means that 140 will give you a result of -116. To solve this use unsigned char, that have max value of 255. Look at the table of the types Max/Min value that in the link. https://msdn.microsoft.com/en-us/library/7fh3a000.aspx

unsigned char a = 140;
int b = a;
cout << b << endl; // 140

2 Comments

These things are implementation-defined. Some systems have different ranges for char and different behaviour for out-of-range assignment
@M.M it seems to be relevant to the OP case.
0

When you store the value of 140 in a char variable it will be out of the range of -128 to +127 for the character.

The value of 140 is converted to -119 The mechanism for this is dependent on the implementation.

The value of -119 is then converted to double and displayed as -119 in double.

What you can do is directly cast the unsigned char numbers to double. This will solve your range issues.

unsigned char a;
a = 140;
b = static_cast<double>(a);
std::cout << b << std::endl;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.