Convert hex string to int
It's simply too big for an int (which is 4 bytes and signed).
Use
Long.parseLong("AA0F245C", 16);
Convert a hexadecimal string to an integer efficiently in C?
You want strtol
or strtoul
. See also the Unix man page
Convert a hex string to a hex int
You'd need supply integer base argument to String#to_i
method:
irb> color = "0xFF00FF"
irb> color.to_i(16)
=> 16711935
irb> color.to_i(16).to_s(16)
=> "ff00ff"
irb> '%#X' % color.to_i(16)
=> "0XFF00FF"
How do you convert a hexadecimal of type string to number in JS?
You can use the Number
function, which parses a string into a number according to a certain format.
console.log(Number("0xdc"));
C++ convert hex string to signed integer
use std::stringstream
unsigned int x;
std::stringstream ss;
ss << std::hex << "fffefffe";
ss >> x;
the following example produces -65538
as its result:
#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;
}
In the new C++11 standard, there are a few new utility functions which you can make use of! specifically, there is a family of "string to number" functions (http://en.cppreference.com/w/cpp/string/basic_string/stol and http://en.cppreference.com/w/cpp/string/basic_string/stoul). These are essentially thin wrappers around C's string to number conversion functions, but know how to deal with a std::string
So, the simplest answer for newer code would probably look like this:
std::string s = "0xfffefffe";
unsigned int x = std::stoul(s, nullptr, 16);
NOTE: Below is my original answer, which as the edit says is not a complete answer. For a functional solution, stick the code above the line :-).
It appears that since lexical_cast<>
is defined to have stream conversion semantics. Sadly, streams don't understand the "0x" notation. So both the boost::lexical_cast
and my hand rolled one don't deal well with hex strings. The above solution which manually sets the input stream to hex will handle it just fine.
Boost has some stuff to do this as well, which has some nice error checking capabilities as well. You can use it like this:
try {
unsigned int x = lexical_cast<int>("0x0badc0de");
} catch(bad_lexical_cast &) {
// whatever you want to do...
}
If you don't feel like using boost, here's a light version of lexical cast which does no error checking:
template<typename T2, typename T1>
inline T2 lexical_cast(const T1 &in) {
T2 out;
std::stringstream ss;
ss << in;
ss >> out;
return out;
}
which you can use like this:
// though this needs the 0x prefix so it knows it is hex
unsigned int x = lexical_cast<unsigned int>("0xdeadbeef");
How to convert a hex string to hex number
Try this:
hex_str = "0xAD4"
hex_int = int(hex_str, 16)
new_int = hex_int + 0x200
print hex(new_int)
If you don't like the 0x
in the beginning, replace the last line with
print hex(new_int)[2:]
Convert hex string to integer in Python
Without the 0x
prefix, you need to specify the base explicitly, otherwise there's no way to tell:
x = int("deadbeef", 16)
With the 0x
prefix, Python can distinguish hex and decimal automatically:
>>> print(int("0xdeadbeef", 0))
3735928559
>>> print(int("10", 0))
10
(You must specify 0
as the base in order to invoke this prefix-guessing behavior; if you omit the second parameter, int()
will assume base-10.)
Presto - hex string to int
You can use from_base(string, radix)
to parse number written with hex digits into a bigint
. You just need to strip leading '0x'
first:
select from_base(substr('0x100', 3), 16);
_col0
-------
256
or with regexp_replace()
:
presto:tiny> select from_base(regexp_replace('0x100', '^0x'), 16);
_col0
-------
256
Related Topics
How to Determine If a Date Is Between Two Dates in Java
Combine Multiple Collections into a Single Logical Collection
Reference Is Ambiguous with Generics
Compiler Error When Declaring a Variable Inside If Condition and No Curly Braces
How to Compare Character Ignoring Case in Primitive Types
Using Enum Values as String Literals
Why Does Stream<T> Not Implement Iterable<T>
How to Serialize Joda Datetime with Jackson JSON Processor
Java; String Replace (Using Regular Expressions)
Converting Characters to Integers in Java
Getting a Illegalblocksizeexception: Data Must Not Be Longer Than 256 Bytes When Using Rsa
Why F Is Placed After Float Values
Default Constructors and Inheritance in Java
Intellij Show Javadocs Tooltip on Mouse Over