Hex: Difference between revisions
Jump to navigation
Jump to search
(→C) |
|||
Line 2: | Line 2: | ||
Many examples come from [http://www.cplusplus.com/ cplusplus.com] and [http://stackoverflow.com stackoverflow.com]. |
Many examples come from [http://www.cplusplus.com/ cplusplus.com] and [http://stackoverflow.com stackoverflow.com]. |
||
= C = |
= C / C ++ = |
||
== Read an integer from an hex string == |
== Read an integer from an hex string == |
||
(See [http://stackoverflow.com/questions/1070497/c-convert-hex-string-to-signed-integer], [http://stackoverflow.com/questions/10324/how-can-i-convert-a-hexadecimal-number-to-base-10-efficiently-in-c]). |
|||
=== Using atol === |
=== Using atol === |
||
<source lang="c"> |
<source lang="c"> |
||
Line 22: | Line 23: | ||
</source> |
</source> |
||
=== Using strtol === |
=== Using strtol, strtoll === |
||
<code>strtol</code> (resp. <code>strtoll</code>) returns </tt>LONG_MIN<tt>, </tt>LONG_MAX</tt> (resp. <tt>LLONG_MIN</tt>, <tt>LLONGMAX</tt>) in case of under/overflow. |
|||
<div style="column-width:36em;"> |
|||
<source lang="c"> |
<source lang="c"> |
||
/* strtol example */ |
/* strtol example */ |
||
Line 38: | Line 41: | ||
li3 = strtol (pEnd,&pEnd,2); |
li3 = strtol (pEnd,&pEnd,2); |
||
li4 = strtol (pEnd,NULL,0); |
li4 = strtol (pEnd,NULL,0); |
||
printf ("The decimal equivalents are: %ld, %ld, %ld and %ld.\n", |
printf ("The decimal equivalents are: %ld, %ld, %ld and %ld.\n", |
||
li1, li2, li3, li4); |
|||
return 0; |
return 0; |
||
} |
} |
||
</source> |
</source> |
||
<source lang="cpp"> |
|||
⚫ | |||
#include <cstdlib> |
|||
Note there is also a <code>strtoull</code> |
|||
#include <iostream> |
|||
using namespace std; |
|||
int main() { |
|||
string s = "abcd"; |
|||
char * p; |
|||
long n = strtol( s.c_str(), & p, 16 ); |
|||
if ( * p != 0 ) { |
|||
cout << "not a number" << endl; |
|||
} |
|||
else { |
|||
cout << n << endl; |
|||
} |
|||
} |
|||
</source> |
|||
</div> |
|||
⚫ | |||
Use <code>strtoul</code> (resp. <code>strtoull</code>) when value to read is greater than <code>numeric_limits<long>::max()</code> (resp. <code>numeric_limits<long long>::max()</code>). |
|||
<div style="column-width:36em;"> |
|||
<source lang="c"> |
<source lang="c"> |
||
/* strtoul example */ |
/* strtoul example */ |
||
Line 62: | Line 86: | ||
} |
} |
||
</source> |
</source> |
||
<source lang="cpp"> |
|||
#include <cstdlib> |
|||
#include <iostream> |
|||
using namespace std; |
|||
int main() { |
|||
string s = "fffefffe"; |
|||
char * p; |
|||
long n = strtoul( s.c_str(), & p, 16 ); |
|||
if ( * p != 0 ) { |
|||
cout << "not a number" << endl; |
|||
} else { |
|||
cout << n << endl; |
|||
} |
|||
} |
|||
</source> |
|||
</div> |
|||
=== Using sscanf === |
=== Using sscanf === |
||
Line 72: | Line 114: | ||
sscanf(s, "%x", &x); |
sscanf(s, "%x", &x); |
||
printf("%u\n", x); |
printf("%u\n", x); |
||
} |
|||
</source> |
|||
=== Using stringstream === |
|||
<source lang="cpp"> |
|||
#include <sstream> |
|||
#include <iostream> |
|||
int main() { |
|||
unsigned int x; |
|||
std::stringstream ss; |
|||
ss << std::hex << "fffefffe"; |
|||
ss >> x; |
|||
// output it as a signed type |
|||
std::cout << static_cast<int>(x) << std::endl; |
|||
} |
} |
||
</source> |
</source> |
Revision as of 07:53, 4 October 2011
References
Many examples come from cplusplus.com and stackoverflow.com.
C / C ++
Read an integer from an hex string
Using atol
/* atol example */
#include <stdio.h>
#include <stdlib.h>
int main ()
{
long int li;
char szInput [256];
printf ("Enter a long number: ");
gets ( szInput );
li = atol (szInput);
printf ("The value entered is %d. The double is %d.\n",li,li*2);
return 0;
}
Using strtol, strtoll
strtol
(resp. strtoll
) returns LONG_MIN, LONG_MAX (resp. LLONG_MIN, LLONGMAX) in case of under/overflow.
/* strtol example */
#include <stdio.h>
#include <stdlib.h>
int main ()
{
char szNumbers[] = "2001 60c0c0 -1101110100110100100000 0x6fffff";
char * pEnd;
long int li1, li2, li3, li4;
li1 = strtol (szNumbers,&pEnd,10);
li2 = strtol (pEnd,&pEnd,16);
li3 = strtol (pEnd,&pEnd,2);
li4 = strtol (pEnd,NULL,0);
printf ("The decimal equivalents are: %ld, %ld, %ld and %ld.\n",
li1, li2, li3, li4);
return 0;
}
#include <cstdlib>
#include <iostream>
using namespace std;
int main() {
string s = "abcd";
char * p;
long n = strtol( s.c_str(), & p, 16 );
if ( * p != 0 ) {
cout << "not a number" << endl;
}
else {
cout << n << endl;
}
}
Using strtoul, strtoull
Use strtoul
(resp. strtoull
) when value to read is greater than numeric_limits<long>::max()
(resp. numeric_limits<long long>::max()
).
/* strtoul example */
#include <stdio.h>
#include <stdlib.h>
int main ()
{
char szInput [256];
unsigned long ul;
printf ("Enter an unsigned number: ");
fgets (szInput,256,stdin);
ul = strtoul (szInput,NULL,0);
printf ("Value entered: %lu. Its double: %lu\n",ul,ul*2);
return 0;
}
#include <cstdlib>
#include <iostream>
using namespace std;
int main() {
string s = "fffefffe";
char * p;
long n = strtoul( s.c_str(), & p, 16 );
if ( * p != 0 ) {
cout << "not a number" << endl;
} else {
cout << n << endl;
}
}
Using sscanf
#include <stdio.h>
main()
{
char s[] = "fffffffe";
int x;
sscanf(s, "%x", &x);
printf("%u\n", x);
}
Using stringstream
#include <sstream>
#include <iostream>
int main() {
unsigned int x;
std::stringstream ss;
ss << std::hex << "fffefffe";
ss >> x;
// output it as a signed type
std::cout << static_cast<int>(x) << std::endl;
}