Getting epoc time in c++(hexadecimal value printed implicitly) - c++

My program want to print epoc time in C++
If I print with fprintf/printf command, i get the correct output
fprintf(stdout, "epoch time=%u\t", (unsigned)time(NULL));
printf("var epoch=%u\n",(unsigned)time(NULL));
Output--->1528573149
But When i print the epoc time using C++, the output it is printed in "hex" format
cout<<"epoch time ="<< (unsigned)time(0);
Output---->5b1c2cdd
Unable to figure, how decimal value is implicitly converted to hexadecimal value.

Shows up as decimal for me, but try this:
std::cout << std::dec << (unsigned)time(0);
Or you can set desired format flag:
std::cout.setf(std::ios_base::dec, std::ios_base::basefield);

The only way this:
cout<<"epoch time ="<< (unsigned)time(0);
could print in hexadecimal is if you've previously set std::cout to hexadecimal mode.
For example:
#include <ctime>
#include <iostream>
#include <iomanip>
int main() {
std::cout << " decimal epoch time = " << time(0) << "\n";
std::cout << std::hex;
std::cout << "hexadecimal epoch time = " << time(0) << "\n";
}
The output on my system is:
decimal epoch time = 1528663129
hexadecimal epoch time = 5b1d8c59
This is in my opinion the most annoying feature of C++ I/O: To print a single item with a non-default format, you have to change the state of the output stream, which affects all later output on that stream.
If you want to print a single data item in hexadecimal (which you've probably done earlier in your program), you can do something like:
std::cout << "x = " << std::hex << x << std::dec << "\n";
This sets the output format back to decimal -- regardless of what it was previously. There's probably a complicated way to restore it to whatever it was.

Related

Conversion of string to double without scientific notation in C++

I have a string variable and want to convert it into double without any scientific notation. I tried using std::stod but that doesn't work.
std::stringstream timestamp;
timestamp << t_val;
string test = timestamp.str();
cout << test << endl; // this gives 1506836639.96
double d = std::stod(test);
cout << d << endl; // This gives 1.50684e+09 instead of 1506836639.96
I tried using setprecision and fixed but I couldn't store the result into a variable. Is there a way I can store the value of test (1506836639.96) as a double?
The scientific notation has to do with std::cout, not the way the value is stored, so you must use std::fixed before you print the value:
std::cout << std::fixed << std::setprecision(2) << d << std::endl;
As you can see in the demo, this works fine, it should work for you as well.
As #goodvibration commented std::to_string also works but it's not possible to redefine, in a simple manner, the default number or decimal places in this case.
std::cout << std::to_string(d) << std::endl;
Live demo

C++ -- Hex to String Conversion [duplicate]

has it occurred to anyone that a simple std::cout might print a value in hex format when it is supposed to format just a decimal(like an integer)?
for example, I have a line as :
std::cout << "_Agent [" << target << "] is still
among " << ((target->currWorker)->getEntities().size()) << " entities
of worker[" << target->currWorker << "]" << std::endl;
which would print :
_Agent [0x2c6d530] is still among 0x1 entities of worker[0x2c520f0]
Note:
1-the said out put is sometime decimal and some times hex
2- the behaviour is smae even if I change ((target->currWorker)->getEntities().size()) to (int)((target->currWorker)->getEntities().size())
any hints?
thanks
You probably have set std::cout to print hex in prior in the context of your code but forget to reset. For example:
std::cout<<std::hex<<12;
/*blah blah blah*/
std::cout<<12; //this will print in hex form still
so you have to do like the following
std::cout<<std::dec<<12;
to print in decimal form.
Try to find line like this std::cout << std::showbase << std::hex; some where in your code, which sets std::cout to print output in hexadecimal with 0x base indicator prefix.
To reset it to show decimal add this line std::cout<<std::dec before the current cout.
You can learn more about c++ io manipulators flags here

std::cout gives different output from qDebug

I am using Qt, and I have an unsigned char *bytePointer and want to print out a number-value of the current byte. Below is my code, which is meant to give the int-value and the hex-value of the continuous bytes that I receive from a machine attached to the computer:
int byteHex=0;
byteHex = (int)*bytePointer;
qDebug << "\n int: " //this is the main issue here.
<< *bytePointer;
std::cout << " (hex: "
<< std::hex
<< byteHex
<< ")\n";
}
This gives perfect results, and I get actual numbers, however this code is going into an API and I don't want to use Qt-only functions, such as qDebug. So when I try this:
int byteHex=0;
byteHex = (int)*bytePointer;
std::cout << "\n int: " //I changed qDebug to std::cout
<< *bytePointer;
std::cout << " (hex: "
<< std::hex
<< byteHex
<< ")\n";
}
The output does give the hex-values perfectly, however the int-values return symbols (like ☺, └, §, to list a few).
My question is: How do I get std::cout to give the same output as qDebug?
EDIT: for some reason the symbols only occur with a certain Qt setting. I have no idea why it happened but it's fixed now.
As others pointed out in comment, you change the outputting to hex, but you do not actually set it back here:
std::cout << " (hex: "
<< std::hex
<< byteHex
<< ")\n";
You will need to apply this afterwards:
std::cout << std::dec;
Standard output streams will output any character type as a character, not a numeric value. To output the numeric value, convert to a non-character integer type:
std::cout << int(*bytePointer);

Converting String to Double -- Loses Precision

I am having trouble converting a string to a double. I am given a string with lat/long coordinates in the format of 33.9425/N 118.4081/W
I first call my function trimLastChar(std::string& input) twice, which will remove the the North, South, East, West characters and then the forward slash. This function correctly returns 33.9425 and 118.4081 respectfully as a std::string.
I am using the following code to convert my std::string to a double...however the problem is, the conversion losses precision -- I suspect it gets rounded?.
// Location In String is what trimLastChar returns
std::stringstream stream(locationInString);
std::cout << "DEBUG: before: " << locationInString << " ";
// output is a double* output = new double passed by reference to my function
stream >> output;
std::cout << output << std::endl;
The output in this case would produce:
33.9425
118.408
As you notice, the correct value should be 118.4081 but the 1 is missing...
Any ideas how to fix? Or more important, why is this happening?
The precision wasn't lost on input. It's being lost on output.
#include <iostream>
using std::cout;
using std::endl;
int main()
{
double v = 118.4081;
cout << v << endl;
cout.precision(10);
cout << v << endl;
}
outputs:
$ g++ -Wall x.cpp && ./a.out
118.408
118.4081
$
You probably have more digits than the output shows. By default, only a small number of digits is shown, you need to use std::setprecision to see more digits. Try
std::cout << std::setprecision(10) << output << std::endl;

cout print hex instead of decimal

has it occurred to anyone that a simple std::cout might print a value in hex format when it is supposed to format just a decimal(like an integer)?
for example, I have a line as :
std::cout << "_Agent [" << target << "] is still
among " << ((target->currWorker)->getEntities().size()) << " entities
of worker[" << target->currWorker << "]" << std::endl;
which would print :
_Agent [0x2c6d530] is still among 0x1 entities of worker[0x2c520f0]
Note:
1-the said out put is sometime decimal and some times hex
2- the behaviour is smae even if I change ((target->currWorker)->getEntities().size()) to (int)((target->currWorker)->getEntities().size())
any hints?
thanks
You probably have set std::cout to print hex in prior in the context of your code but forget to reset. For example:
std::cout<<std::hex<<12;
/*blah blah blah*/
std::cout<<12; //this will print in hex form still
so you have to do like the following
std::cout<<std::dec<<12;
to print in decimal form.
Try to find line like this std::cout << std::showbase << std::hex; some where in your code, which sets std::cout to print output in hexadecimal with 0x base indicator prefix.
To reset it to show decimal add this line std::cout<<std::dec before the current cout.
You can learn more about c++ io manipulators flags here