I am trying to communicate with my vehicles OBD port using LibSerial. When ever i send a command to the vehicle it echos back a hexadecimal value but LibSerial returns this hexadecimal value back to me as a string, i.e. "0x01A". How would i convert this hex string into a double so that I can process it using the appropriate formulas.
Talguy 0 Junior Poster in Training
Recommended Answers
Jump to Postthere is no such thing as a double in hex. Use
hexdec
to convert to an integer then explicitly cast the return to a double.$someHEx = "0x01A"; $someDouble = (double)hexdec($someHex);
Jump to PostI just realized I was in the C++ forum, haha, oh well. Here's the C++ equivalent
In C++ you can use the following
#include <cstdio> int main(int argc, char** argv) { char* somehex = "05a1"; unsigned long somelong = strtoul(somehex, 16); printf("%d", somelong); return 0; }
Jump to Post
strtoul(<string>, NULL, <base to convert, 16 for hex>);
Also, ArkM, strtoul returns an unsigned long, not an int. And in your first function why are you type coercing 0.0 (double) to bool?
All 9 Replies
ShawnCplus 456 Code Monkey Team Colleague
Talguy 0 Junior Poster in Training
ShawnCplus 456 Code Monkey Team Colleague
ArkM 1,090 Postaholic
ShawnCplus 456 Code Monkey Team Colleague
Talguy 0 Junior Poster in Training
ArkM 1,090 Postaholic
ShawnCplus 456 Code Monkey Team Colleague
ArkM 1,090 Postaholic
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.