##### Convert hex value into string
 Convert hex value into string Sancho_Pansa Silly Frenchman Posts: 35 Threads: 8 Joined: Oct 2020 Reputation: Dec-22-2020, 04:03 PM 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 jefsummers Da Bishop Posts: 1,040 Threads: 1 Joined: May 2019 Reputation: Dec-22-2020, 05:05 PM 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 bowlofred Da Bishop Posts: 1,194 Threads: 3 Joined: Mar 2020 Reputation: Dec-22-2020, 05:31 PM 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 deanhystad So-and-so of the Yard Posts: 2,156 Threads: 11 Joined: Feb 2020 Reputation: Dec-22-2020, 06:38 PM 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 Sancho_Pansa Silly Frenchman Posts: 35 Threads: 8 Joined: Oct 2020 Reputation: Dec-23-2020, 07:51 AM Well ... probably I have to clarify the problem 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 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 bowlofred Da Bishop Posts: 1,194 Threads: 3 Joined: Mar 2020 Reputation: Dec-23-2020, 08:02 AM 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 Sancho_Pansa Silly Frenchman Posts: 35 Threads: 8 Joined: Oct 2020 Reputation: Dec-23-2020, 08:10 AM (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 Sancho_Pansa Silly Frenchman Posts: 35 Threads: 8 Joined: Oct 2020 Reputation: Dec-23-2020, 08:32 AM 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 Sancho_Pansa Silly Frenchman Posts: 35 Threads: 8 Joined: Oct 2020 Reputation: Dec-23-2020, 08:54 AM Resolved: `'hex value is: ' + '{:04X}'.format(hex_value)` Reply

 Possibly Related Threads… Thread Author Replies Views Last Post Convert each element of a list to a string for processing tester_V 6 567 Jun-16-2021, 02:11 AM Last Post: tester_V convert unlabeled list of tuples to json (string) masterAndreas 4 1,218 Apr-27-2021, 10:35 AM Last Post: masterAndreas Convert String of an int array to a Numpy array of ints mdsousa 5 1,021 Apr-08-2021, 08:00 PM Last Post: mdsousa Convert string to JSON using a for loop PG_Breizh 3 685 Jan-08-2021, 06:10 PM Last Post: PG_Breizh convert string into multiple columns in python3 VAN 2 905 Sep-26-2020, 11:14 PM Last Post: scidam ValueError: could not convert string to float: RandomCoder 3 1,905 Jul-27-2020, 07:38 AM Last Post: ndc85430 could not convert string to float player1681 4 2,800 May-22-2020, 12:10 PM Last Post: buran How do I convert this string back to a list of integers? donmerch 6 1,404 Apr-05-2020, 06:43 PM Last Post: donmerch could not convert string to float: C JamesW 2 2,228 Jan-29-2020, 04:56 PM Last Post: JamesW Python convert multi line into single line formatted string karthidec 2 3,234 Dec-23-2019, 12:46 PM Last Post: karthidec

Forum Jump:

### User Panel Messages

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