How to get std::numeric_limits<char>::min() value? - c++

How to get (correctly and/or readable value of) std::numeric_limits<char>::min()?
cout << std::numeric_limits<char>::min() << endl;
cout << std::numeric_limits<char>::max() << endl;
return
�
// some character that can't be copied here, it looks like a rectangle containing four numbers in it

You just need to convert it to something that when streamed to cout will interpret it as an integer. E.g.
#include <limits>
#include <iostream>
#include <ostream>
int main()
{
int minc = std::numeric_limits<char>::min();
unsigned maxc = std::numeric_limits<char>::max();
std::cout << minc << std::endl;
std::cout << maxc << std::endl;
}
I deliberately use unsigned for std::numeric_limits<char>::max() just in case sizeof(int) == 1 and char is unsigned.

The problem is that the standard streams will output chars as characters and not as integral values. You can force them to do that by casting to an integral type that isn't a character type:
cout << (int)std::numeric_limits<char>::min() << endl;
cout << (int)std::numeric_limits<char>::max() << endl;

Just cast it to an int.
cout << (int)std::numeric_limits<char>::min() << endl;
cout << (int)std::numeric_limits<char>::max() << endl;

Related

Why can't wcout use the hex keyword to output hexadecimal format?

I have entered a wchar type variable and want to see the hexadecimal of the variable. However,when I use the wcout keyword, I can't always output hexadecimal. Is there a grammatical error?
#include <iostream>
void test_wide_character_input() {
using namespace std;
wchar_t ch = L'?';
wcout << ch << endl;
wcout << hex;
wcout << ch << endl;
cout << "---" << endl;
wchar_t w_ch = L'1';
wcout << w_ch << endl;
cout << w_ch << endl;
cout << hex;
cout << w_ch << endl;
}
int main() {
test_wide_character_input();
return 0;
}
out:
PS C:\Users\j30022312\Downloads\Relearn_C_Plus_Plus-main\Relearn_C_Plus_Plus-main\PART_2_The_C++_Library\Chapter_8_The_IO_Library\examples> .\a.exe
?
?
---
1
49
31
Change the character to unsigned int type before converting it to hexadecimal like this:
wcout << hex << (unsigned)ch;

C++ manipulation using iomanip library

I am new to C++ STL libraries and need help.
I want to add two numbers suppose A = 4555 and B = 50, and output them as:
4555
+50
4605
Another Examples:
500000 + 12
500000
+12
500012
If i am storing both A and B in integer data type while the sign '+' in character data type. How can i manipulate them to get the preferred output.
I just cant figure out how to manipulate two variables together.
You might utilize the manipulators std::showpos, std::noshowpos and std::setw:
#include <iostream>
#include <iomanip>
int main() {
int a = 4555;
int b = 50;
std::cout
<< std::noshowpos << std::setw(10) << a << '\n'
<< std::showpos << std::setw(10) << b << '\n'
<< std::noshowpos << std::setw(10) << (a+b) << '\n';
}
If you want a width depending on the values you may use three std::ostringstream(s) and create intermediate strings (without setw). After that you print the strings using the maximal length of each for setw:
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <sstream>
int main() {
int a = 4555;
int b = 50;
std::ostringstream as;
std::ostringstream bs;
std::ostringstream rs;
as << std::noshowpos << a;
bs << std::showpos << b;
rs << std::noshowpos << (a+b);
unsigned width = std::max({ as.str().size(), bs.str().size(), rs.str().size() });
std::cout
<< std::setw(width) << as.str() << '\n'
<< std::setw(width) << bs.str() << '\n'
<< std::setw(width) << rs.str() << '\n';
}
See also:
http://www.cplusplus.com/reference/iomanip/
http://www.cplusplus.com/reference/ios/
Note: You may have a look at the manipulator std::internal.
If you could use constant width (or variable width equal to the maximum width of the numbers involved) with std::setw from <iomanip> as:
#include <iostream>
#include <iomanip>
#include <string>
void display_sum(int a, int b)
{
std::cout << std::setw(10) << a << "\n"
<< std::setw(10) << ("+" + std::to_string(b)) << "\n"
<< std::setw(10) << (a+b) <<"\n" << std::endl;
}
int main()
{
display_sum(4555, 50);
display_sum(500000, 12);
display_sum(503930, 3922);
}
Output:
4555
+50
4605
500000
+12
500012
503930
+3922
507852
Online demo
In your example the fields can fit a maximum number of 7 characters. Perhaps you want to resize the strings to 7 before writing. e.g. fname.resize(7).
To format it as you want you need to #include <iomanip> and use std::left and std::setw(7).
file1 << left << setw(7) << fname
<< tab << setw(7) << lname
<< tab << setw(7) << street
<< tab << setw(7) << city
<< tab << setw(7) << state
<< tab << setw(7) << zip << endl;

Converting fl in hexadecimal in c++

I am new to C++, and programming, and I want to write a C++ program to convert a float in hexadecimal with the help of pointers
I've looked on other threads and really tried to get a hold of this but can't seem to figure it out.
Here is what I have done so far:
int main()
{
float number = -12.0
unsigned char *ptr = ((unsigned char*) &number);
for (int i = 0; i < sizeof(float); i++)
{
cout << "Byte " [i] << "is : " << ptr[i] << endl;
}
So with this, i assume I am able to have access to the bytes that compose the float. However, can you suggest any ideas to convert this to hexadecimal. I guess, I have to be able to read the binary behind all this... but I am not really sure how.
Note: I understand how to convert from binary to hexadecimal
iostreams can print numbers in hexadecimal:
int main() {
float number=-1.0;
unsigned char *ptr = ((unsigned char*)&number);
for (int i = 0; i < sizeof(float); i++)
{
cout << "Byte ";
cout << setw(0) << dec << i << "is : ";
cout << setw(2) << setfill('0') << hex << ptr[i] << endl;
}
what you want might be sth like this:
float number=-1.0;
char cz[]="0123456789ABCDEF";
int si=sizeof(float);
char* ptr=(char*)(&number);
for(int i=0;si-i;++i){
std::cout<<cz[((*ptr)&0xf0)>>4]<<cz[(*ptr)&0x0f]<<' ';
++ptr;
}
You have minor bug here cout << "Byte " [i] which is the same as doing:
const char *text = "Byte ";
cout << text[i]
Another minor bug is that ptr[] is char thus cout treats it as a printable character (not a number), using (int)ptr[i] will do. Also you want print i as decimal and ptr[i] as hex, so you have to use std::dec and std::hex
cout << "Byte " << std::dec << i << " is : " << std::hex << (int)ptr[i] << endl;
You may polish it a bit by using std::setw() and std::setfill() from <iomanip>:
#include <iostream>
#include <iomanip>
using std::cout;
using std::endl;
int main()
{
float number = -1.0;
unsigned char *ptr = ((unsigned char*) &number);
cout << std::setfill('0');
for (int i = 0; i < sizeof(float); i++)
{
cout << "Byte "
<< std::dec << std::setw(0) << i << " is : "
<< std::hex << std::setw(2) << (int)ptr[i] << endl;
}
return 0;
}
This will print out:
Byte 0 is : 00
Byte 1 is : 00
Byte 2 is : 80
Byte 3 is : bf

The decimal, octal and hexadecimal representation of an integer in C++

How can I express the value of an integer using decimal, octal or hexadecimal representation?
(I would prefer only iostream usage)
Assuming you just want to see them, for your own reference. Though storing them in a variable is "just a shot away".
#include <iostream>
using namespace std;
int main () {
int n;
n=70;
cout << hex << n << endl;
cout << dec << n << endl;
cout << oct << n << endl;
return 0;
}
By "decimal integer" I hope you mean a string that uses decimal to represent an integer. Integer types, like int, do not have a base. Or if you insist that they must have a base because of their internal representation then the base is always 2. String representations of integers, now those have a base.
std::istringstream iss(std::string("123"));
int i;
if (iss >> i) {
std::cout << "read a decimal integer!\n";
std::cout << "here it is in decimal: " << i << "\n";
std::cout << "here it is in hex: " << std::hex << i << "\n";
std::cout << "here it is in octal: " << std::oct << i << "\n";
}

How do you find the range of values that integer types can represent in C++?

The size and range of the integer value types in C++ are platform specific. Values found on most 32-bit systems can be found at Variables. Data Types. - C++ Documentation. How do you determine what the actual size and range are for your specific system?
C Style
limits.h contains the min and max values for ints as well as other data types which should be exactly what you need:
#include <limits.h> // C header
#include <climits> // C++ header
// Constant containing the minimum value of a signed integer (–2,147,483,648)
INT_MIN;
// Constant containing the maximum value of a signed integer (+2,147,483,647)
INT_MAX;
For a complete list of constants and their common values check out: Wikipedia - limits.h
C++ Style
There is a template based C++ method as other commenters have mentioned using:
#include <limits>
std::numeric_limits
which looks like:
std::numeric_limits<int>::max();
and it can even do craftier things like determine the number of digits possible or whether the data type is signed or not:
// Number of digits for decimal (base 10)
std::numeric_limits<char>::digits10;
// Number of digits for binary
std::numeric_limits<char>::digits;
std::numeric_limits<unsigned int>::is_signed;
Take a look at std::numeric_limits
Why not just be sure and use boost's numeric types?
ie:
boost::uint32_t
boost::int32_t
etc
You can use the types defined in stdint.h (or cstdint, if you are using C++), which are part of the C99 standard. It defines types with such names as int32_t, uint8_t, int64_t, an so on, which are guaranteed to be portable and platform independent.
For more information: stdint.h
Use the sizeof() operator in C++ to determine the size (in bytes) of a value type. The standard library header file limits.h contains the range limits for integer value types. You can run the following program to learn the size and range limits for integer types on your system.
#include <stdlib.h>
#include <iostream>
#include <limits>
using namespace std;
int main(int argc, char** argv) {
cout << "\nCharacter Types" << endl;
cout << "Size of character type is " << sizeof(char) << " byte." << endl;
cout << "Signed char min: " << SCHAR_MIN << endl;
cout << "Signed char max: " << SCHAR_MAX << endl;
cout << "Unsigned char min: 0" << endl;
cout << "Unsigned char max: " << UCHAR_MAX << endl;
cout << "\nShort Int Types" << endl;
cout << "Size of short int type is " << sizeof(short) << " bytes." << endl;
cout << "Signed short min: " << SHRT_MIN << endl;
cout << "Signed short max: " << SHRT_MAX << endl;
cout << "Unsigned short min: 0" << endl;
cout << "Unsigned short max: " << USHRT_MAX << endl;
cout << "\nInt Types" << endl;
cout << "Size of int type is " << sizeof(int) << " bytes." << endl;
cout << "Signed int min: " << INT_MIN << endl;
cout << "Signed int max: " << INT_MAX << endl;
cout << "Unsigned int min: 0" << endl;
cout << "Unsigned int max: " << UINT_MAX << endl;
cout << "\nLong Int Types" << endl;
cout << "Size of long int type is " << sizeof(long) << " bytes." << endl;
cout << "Signed long min: " << LONG_MIN << endl;
cout << "Signed long max: " << LONG_MAX << endl;
cout << "Unsigned long min: 0" << endl;
cout << "Unsigned long max: " << ULONG_MAX << endl;
return (EXIT_SUCCESS);
}
You can get the range of any data type by applying the following formulla:
[-2 power (N-1)] to { [+2 power (N-1)] - 1 }
Where "N" is the width of data type, for example in JAVA the width of int is 32,hence N = 32.
Try this out you will get it.
#include<stdio.h>
#include<limits.h>
void main()
{
printf(" signed data types " );
printf(" int min : %d ", INT_MIN); // INT_MIN, INT_MAX, SCHAR_MIN, SCHAR_MAX ....etc
printf(" int max : %d ",INT_MAX);// pre defined constants to get the values of datatypes
printf(" signed char min : %d ", SCHAR_MIN);
printf(" signed char max : %d ", SCHAR_MAX);
// similarly for un_signed
// use %u for control charter, and UINT_MAX, UCHAR_MAX, USHRT_MAX, ULONG_MAX.
}
Bitwise operations can be used to find the number of bits and range of int in a platform.
Here is a sample I wrote to test the range of int on my machine.
#include <iostream>
using namespace std;
void print_int_range() {
int i=1;
int nOfBits=0;
while (i != 0) {
i = i << 1;
nOfBits++;
}
cout << "int has " << nOfBits << " bits" << endl;
cout << "mininum int: " << (1 << (nOfBits - 1)) << ", maximum int: " << ~(1 << (nOfBits - 1)) << endl;
}
void print_unsigned_int_range() {
unsigned int i=1;
int nOfBits=0;
while (i != 0) {
i = i << 1;
nOfBits++;
}
cout << "unsigned int has " << nOfBits << " bits" << endl;
cout << "mininum int: " << (0) << ", maximum int: " << (unsigned int) (~0) << endl;
}
int main() {
print_int_range();
print_unsigned_int_range();
}
And here is my output:
int has 32 bits
mininum int: -2147483648, maximum int: 2147483647
unsigned int has 32 bits
mininum int: 0, maximum int: 4294967295
sizeof(int)