Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert an integer to hexadecimal without the extra '0x' leading and 'L' trailing characters in Python?

Tags:

I am trying to convert big integer number to hexadecimal, but in result I get extra "0x" in the beginning and "L" at the and. Is there any way to remove them. Thanks. The number is:

44199528911754184119951207843369973680110397865530452125410391627149413347233422 34022212251821456884124472887618492329254364432818044014624401131830518339656484 40715571509533543461663355144401169142245599341189968078513301836094272490476436 03241723155291875985122856369808620004482511813588136695132933174030714932470268 09981252011612514384959816764532268676171324293234703159707742021429539550603471 00313840833815860718888322205486842202237569406420900108504810 

In hex I get:

0x2ef1c78d2b66b31edec83f695809d2f86e5d135fb08f91b865675684e27e16c2faba5fcea548f3 b1f3a4139942584d90f8b2a64f48e698c1321eee4b431d81ae049e11a5aa85ff85adc2c891db9126 1f7f2c1a4d12403688002266798ddd053c2e2670ef2e3a506e41acd8cd346a79c091183febdda3ca a852ce9ee2e126ca8ac66d3b196567ebd58d615955ed7c17fec5cca53ce1b1d84a323dc03e4fea63 461089e91b29e3834a60020437db8a76ea85ec75b4c07b3829597cfed185a70eeaL 
like image 273
user1047517 Avatar asked Apr 18 '12 21:04

user1047517


People also ask

How do you get a hex value without 0x in Python?

The Python string. zfill() method fills the string from the left with '0' characters. In combination with slicing from the third character, you can easily construct a hexadecimal string without leading '0x' characters and with leading '0' characters up to the length passed into the string. zfill(length) method.

How do you remove an ox from hexadecimal in Python?

Since Python returns a string hexadecimal value from hex() we can use string. replace to remove the 0x characters regardless of their position in the string (which is important since this differs for positive and negative numbers).

How do you convert int to hex in Python?

Python hex() function is used to convert an integer to a lowercase hexadecimal string prefixed with “0x”. We can also pass an object to hex() function, in that case the object must have __index__() function defined that returns integer. The input integer argument can be in any base such as binary, octal etc.

Why does hex have 0x in front?

The prefix 0x is used in code to indicate that the number is being written in hex.


1 Answers

The 0x is literal representation of hex numbers. And L at the end means it is a Long integer.

If you just want a hex representation of the number as a string without 0x and L, you can use string formatting with %x.

>>> a = 44199528911754184119951207843369973680110397 >>> hex(a) '0x1fb62bdc9e54b041e61857943271b44aafb3dL' >>> b = '%x' % a >>> b '1fb62bdc9e54b041e61857943271b44aafb3d' 
like image 83
Praveen Gollakota Avatar answered Sep 23 '22 23:09

Praveen Gollakota