I am loosing it with c++. So I say sorry straight from the start. This might be a stupid question for c experts. I am trying to fix this since hours and also used a fair amount of time to find something suitable online. No success.
So maybe somebody have mercy and helps me out with this one.
I am trying to convert Strings to feed a function which is requiring a hex value.
I have:
String s1 = "01"
String s2 = "02"
what I need to feed the hex function is then
doSomethingWithHex(0x01,0x02)
Another example could be:
s1 = "1B"
s2 = "03"
and I would need to call doSomethingWithHex(0x1B,0x03)
I was fiddling around with toCharArray as I thought that could solve my problem,
but as mentioned before with no luck at all.
Any suggestion bringing me on track again is appreciated!
First, numbers are binary, bits representing powers of two. The concept of octal, decimal, and hex are just presentations for humans to read. The function you need is 'strtoul()'. Look at the arguments of this function at this link.
northberlin:
what I need to feed the hex function is then
doSomethingWithHex(0x01,0x02)
There ain't no such thing like a "hex number"!
Hex numbers - as you talk of them - are integer numbers, maybe signed or unsigned, most likely unsigned, and besides the difference whether the number is signed or unsigned there is a difference about the bit-width of a number. On 8-bit controllers, numbers are most likely 8, 16, 32 or 64 bits wide.
And your talking about HEX ist just the "human readable representation of a number".
If your function executes by calling code line
doSomethingWithHex(0x01,0x02);
there is no difference whether you call the function by code line
doSomethingWithHex(1,2);
Whether you write a hex representation in your code or a decimal representation (or maybe a binary representation) makes no differrence in code execution.
The only difference is the human readable representation, maybe 0x01 (hex), maybe 1 (decimal) or maybe 0b00000001 (binary) representation, but always the same number, even if the human representation is different in hex, decimal or binary.
PaulS:
Why are you using String to hold the data? Where does the data come from?
If you used a char array, with a NULL terminator, you could use strtoul() to convert the string representation of the value to an unsigned long.
Arctic_Eddie:
First, numbers are binary, bits representing powers of two. The concept of octal, decimal, and hex are just presentations for humans to read. The function you need is 'strtoul()'. Look at the arguments of this function at this link.
Thanks Paul and Eddie, strtoul was the direction I was looking for. Good to have something finished before the day is over
jurs:
There ain't no such thing like a "hex number"!
Hex numbers - as you talk of them - are integer numbers, maybe signed or unsigned, most likely unsigned, and besides the difference whether the number is signed or unsigned there is a difference about the bit-width of a number. On 8-bit controllers, numbers are most likely 8, 16, 32 or 64 bits wide.
And your talking about HEX ist just the "human readable representation of a number".
If your function executes by calling code line
Whether you write a hex representation in your code or a decimal representation (or maybe a binary representation) makes no differrence in code execution.
The only difference is the human readable representation, maybe 0x01 (hex), maybe 1 (decimal) or maybe 0b00000001 (binary) representation, but always the same number, even if the human representation is different in hex, decimal or binary.
Thanks for clarifying this. I really think I need to refresh some of the theory again