L
L
lovesuper2015-05-29 23:35:07
Programming
lovesuper, 2015-05-29 23:35:07

How to convert a string containing hex to native hex in C++?

In an Arduino project, I needed to assemble a hexadecimal command for an external device using string concatenation. How to convert a string like "0x0104" to like -- 0x0104 (number)? I have no experience in low-level programming, so I'm wandering around.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vladimir Martyanov, 2015-05-29
@vilgeforce

Drop 0x, then loop through the line from left to right. Each character is translated into the corresponding number, let it be h. Then result = result | h; result = result << 8; and move to the next character.
Translation from character to number - 2-3 IF.

X
xibir, 2015-05-29
@xibir

std::string s = "0xaabbff";
unsigned long long a = std::stoull(s, 0, 16);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question