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 PostThe strtoul wants three arguments ;)
/// Returns true if it was a good number bool cvtHex(int& n,const char* p) { if (!p) return 0.0; return sscanf(p,"%x",&n) == 1; } /// For PHP expert$ only inline int hexdec(const char* p) { return p?strtoul(p,0,16):0; }
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.