How to convert a string of hex values to a string?
int len = hex.length();
std::string newString;
for(int i=0; i< len; i+=2)
{
std::string byte = hex.substr(i,2);
char chr = (char) (int)strtol(byte.c_str(), null, 16);
newString.push_back(chr);
}
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:]
Python convert a string containing hex to actual hex
In Python, there is no separate type as 'hex'. It represents the hexadecimal notation of the number as str
. You may check the type
yourself by calling it on hex()
as:
# v convert integer to hex
>>> type(hex(123))
<type 'str'>
But in order to represent the value as a hexadecimal, Python prepends the 0x
to the string which represents hexadecimal number. For example:
>>> hex(123)
'0x7b'
So, in your case in order to display your string as a hexadecimal equivalent, all you need is to prepend it with "0x" as:
>>> my_hex = "0x" + "3f4800003f480000"
This way if you probably want to later convert it into some other notation, let's say integer (which based on the nature of your problem statement, you'll definitely need), all you need to do is call int
with base 16 as:
>>> int("0x3f4800003f480000", base=16)
4559894623774310400
In fact Python's interpreter is smart enough. If you won't even prepend "0x", it will take care of it. For example:
>>> int("3f4800003f480000", base=16)
4559894623774310400
"0x" is all about representing the string is hexadecimal string in case someone is looking/debugging your code (in future), they'll get the idea. That's why it is preferred.
So my suggestion is to stick with Python's Hex styling, and don't convert it with escape characters as "\x3f\x48\x00\x00\x3f\x48\x00\x00"
From the Python's hex
document :
Convert an integer number to a lowercase hexadecimal string prefixed with “0x”. If x is not a Python int object, it has to define an index() method that returns an integer.
Converting a list of strings that represent hex values into actual hex values
You can convert the hexadecimal string representations like this:
a = ['02', 'ff', '98', '80', '39', '31', '03']
b = [int(x, 16) for x in a]
This will create a list with integer equivalents of the input strings
Convert a string representation of a hex dump to a byte array using Java?
Update (2021) - Java 17 now includes java.util.HexFormat
(only took 25 years):
HexFormat.of().parseHex(s)
For older versions of Java:
Here's a solution that I think is better than any posted so far:
/* s must be an even-length string. */
public static byte[] hexStringToByteArray(String s) {
int len = s.length();
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
+ Character.digit(s.charAt(i+1), 16));
}
return data;
}
Reasons why it is an improvement:
Safe with leading zeros (unlike BigInteger) and with negative byte values (unlike Byte.parseByte)
Doesn't convert the String into a
char[]
, or create StringBuilder and String objects for every single byte.No library dependencies that may not be available
Feel free to add argument checking via assert
or exceptions if the argument is not known to be safe.
Appending hex values to a list/converting string lists to hex
Easy enough! Use the base parameter on the int
function to convert from hex string to integer, and you can use hex to go back from integer to hex string.
# Convert hex string to int
str_vals = ['0x0', '0x0', '0x0', '0x0', '0x0', '0x0', '0x0', '0x1']
int_vals = [int(val, 16) for val in str_vals]
# Convert int to hex string
int_vals = [0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1]
str_vals = [hex(val) for val in int_vals]
Related Topics
Zero Initialization and Static Initialization of Local Scope Static Variable
C++ Performance of Accessing Member Variables Versus Local Variables
Google Protocol Buffers Compare
Proper Way of Casting Pointer Types
Is Shrink_To_Fit the Proper Way of Reducing the Capacity a 'Std::Vector' to Its Size
Creating a String List and an Enum List from a C++ MACro
Avoid Warning 'Unreferenced Formal Parameter'
How to Execute Two Threads Asynchronously Using Boost
Can #If Pre-Processor Directives Be Nested in C++
What Exactly Is Va_End For? Is It Always Necessary to Call It
How to Store a Lambda Expression as a Field of a Class in C++11
Is It Valid for a Lambda To, Essentially, Close Over Itself
Which MACro to Wrap MAC Os X Specific Code in C/C++
How to Pass an Std::String to Glshadersource