Problems with python and pyQT

S

silusilusilu

Hi,
i'm new with python: so excuse me for my questions....
i have this code:

def updateLog(self, text):
self.ui.logTextEdit.moveCursor(QTextCursor.End)
self.ui.logTextEdit.insertHtml("<font color=\"Black\">"+text)
self.ui.logTextEdit.moveCursor(QTextCursor.End)

logTextEdit is a QTextEdit object.With this code,i can display only ascii characters: how can i diplay text as hex and binary values?
Thanks
 
C

Chris “Kwpolska†Warrick

Hi,
i'm new with python: so excuse me for my questions....
i have this code:

def updateLog(self, text):
self.ui.logTextEdit.moveCursor(QTextCursor.End)
self.ui.logTextEdit.insertHtml("<font color=\"Black\">"+text)
self.ui.logTextEdit.moveCursor(QTextCursor.End)

logTextEdit is a QTextEdit object.With this code,i can display only ascii characters: how can i diplay text as hex and binary values?
Thanks

You would need to convert them to strings first. You may want bin()
and hex() for that. And if you want to convert 'q' to 0x71,
hex(ord("q")). And if you want to turn 'hello' into 0x68656c6c6f, you
would need to iterate over 'hello' and run the above function over
every letter.

Also, you are able to display Unicode characters, too.
 
S

silusilusilu

Thanks for your reply: very useful!!
I have another question: with hex command i display (for example)

0x1

is it possible to display 0x01?
Thanks

Il giorno lunedì 27 maggio 2013 15:10:24 UTC+2, Chris “Kwpolska” Warrick ha scritto:
 
D

Dave Angel

Thanks for your reply: very useful!!
I have another question: with hex command i display (for example)

0x1

is it possible to display 0x01?

hex() is a function, not a command. And it only takes the one
parameter, the int to be converted.

For more generality, try the str.format() method. The x type converts
to hex without any prefix, and then you can add the width and fill field.

http://docs.python.org/2/library/stdtypes.html#str.format
http://docs.python.org/2/library/string.html#formatstrings
http://docs.python.org/2/library/string.html#format-specification-mini-language

For starters, try:

print "0x{0:0>2x}".format(12)
or
print "0x{0:0>2X}".format(12)
if you like uppercase hex characters
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top