Python Forum
Convert hex value into string
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Convert hex value into string
#1
Hello,

>>> hex_value = 0xFF
>>> string_value = 'hex value is: ' + str(hex_value)
>>> string_value
'hex value is: 255'
How to get hex value is: FF ?
Thanks
Sancho
Reply
#2
This includes the 0x
hex_value = 255
hex_value_str = hex(hex_value)
print(f'hex value is: {hex_value_str}')
Output:
hex value is: 0xff
Reply
#3
Some options:
>>> format(255, "x")
'ff'
>>> format(255, "X")
'FF'
>>> f"{255:x}"
'ff'
>>> f"{255:X}"
'FF'
>>> f"{hex(255)}"
'0xff'
Sancho_Pansa likes this post
Reply
#4
What do you mean by "get hex value is: FF"? Python lets you use hex notation when writing a string literal.
x = 0xff
And you can print an integer using hex notation a number of ways.
print(hex(x))
print(f'{x:x}')
print(f'{x:X}')
Or do you want to "get" a hex value, as in using input() to enter a hex value? Unfortunately I get an error when I try to do this:
x = int(input('Enter a number in hex format '))
Then error is informative "ValueError: invalid literal for int() with base 10: '0xFF'. So I tried this:
x = int(input('Enter a number in hex format '), 16)
This works if I enter 'ff', 'FF', '0xff' or '0xFF'
Reply
#5
Well ... probably I have to clarify the problem
  1. The hexadecimal value is fixed in the form 0xSS (e.g. 0xFF, 0x42, 0xBA, etc.) It cannot be entered in decimal form, it's part of design feautres
  2. This haxadecimal value have to be added to the string (actually a command that is sent over terminal). And in this command the hexadecimal value should be presented in its "hexadecimal form", but without preceeding 0x.

Sincerely,

Sancho
Reply
#6
Why not just strip off the 0x? If you receive it in that form and send it in that form, you don't actually need to interpret it as a number at all.

h = input("Enter the hex value: ")
h = h.replace("0x", "")
new_string = f"The hex value is {h}"
print(new_string)
Output:
Enter the hex value: 0xFF The hex value is FF
Reply
#7
(Dec-22-2020, 05:31 PM)bowlofred Wrote: Some options:
>>> format(255, "x")
'ff'
>>> format(255, "X")
'FF'
>>> f"{255:x}"
'ff'
>>> f"{255:X}"
'FF'
>>> f"{hex(255)}"
'0xff'

Resolved:
>>> hex_value = 0xFF
>>> string_value = 'hex value is: ' + format(hex_value, "X")
>>> string_value
'hex value is: FF'
>>> 
Thanks.
Reply
#8
I rushed a little bit saying it's Ok.
Actually, the problem is not completely resolved: format suppress leading zeros
For example 0x00FF is foramtted as FF, but it should be 00FF.
Any suggestions ?
Reply
#9
Resolved:
'hex value is: ' + '{:04X}'.format(hex_value)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  convert string to float in list jacklee26 6 1,813 Feb-13-2023, 01:14 AM
Last Post: jacklee26
  how to convert tuple value into string mg24 2 2,233 Oct-06-2022, 08:13 AM
Last Post: DeaD_EyE
  Convert string to float problem vasik006 8 3,269 Jun-03-2022, 06:41 PM
Last Post: deanhystad
  Convert a string to a function mikepy 8 2,421 May-13-2022, 07:28 PM
Last Post: mikepy
Question How to convert string to variable? chatguy 5 2,225 Apr-12-2022, 08:31 PM
Last Post: buran
  Convert string to int Frankduc 8 2,394 Feb-13-2022, 04:50 PM
Last Post: menator01
  Convert string to path using Python 2.7 tester_V 10 6,272 Nov-20-2021, 02:20 PM
Last Post: snippsat
  Convert each element of a list to a string for processing tester_V 6 5,166 Jun-16-2021, 02:11 AM
Last Post: tester_V
Question convert unlabeled list of tuples to json (string) masterAndreas 4 7,352 Apr-27-2021, 10:35 AM
Last Post: masterAndreas
  Convert String of an int array to a Numpy array of ints mdsousa 5 5,575 Apr-08-2021, 08:00 PM
Last Post: mdsousa

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020