473,608 Members | 2,565 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to print out each single char from a string in HEX format?

guys,

I've researched python pretty much but still have no idea how to print
out each single character from a string in HEX format? Hope someone
can give me some hints. Thanks a lot.

e.g. ###here is a string

a='01234'

###how to print out it out in this way

0x31 0x31 0x32 0x33 0x34

Jun 4 '07 #1
4 25444
En Mon, 04 Jun 2007 20:03:39 -0300, mike <ne*********@gm ail.comescribió :
I've researched python pretty much but still have no idea how to print
out each single character from a string in HEX format? Hope someone
can give me some hints. Thanks a lot.

e.g. ###here is a string

a='01234'

###how to print out it out in this way

0x31 0x31 0x32 0x33 0x34
pya='01234'
pyfor c in a:
.... print "%#x" % ord(c),
....
0x30 0x31 0x32 0x33 0x34
py>

See <http://docs.python.org/lib/built-in-funcs.html#l2h-55for the ord()
function and <http://docs.python.org/lib/typesseq-strings.htmlfor the
"%#x" format

--
Gabriel Genellina

Jun 4 '07 #2
Great! It works.

Thanks a lot.

Jun 5 '07 #3

Great! It works.
There is a builtin function called hex() that does the same, but also shares
the same problem as the solution above:
>>hex(10)
'0xa'
>>>
This is probably not "nice" in your printouts, it doesn't allign.
with the printf inspired solution you can set the precision like this:
>>a = "12\n34"
for c in a:
.... print "%#04x" % ord(c),
....
0x31 0x32 0x0a 0x33 0x34
>>>

regards troels
Jun 5 '07 #4
On 2007-06-05, Troels Thomsen <nejwrote:
with the printf inspired solution you can set the precision like this:
>>>a = "12\n34"
for c in a:
... print "%#04x" % ord(c),
...
0x31 0x32 0x0a 0x33 0x34
And if you just want to do the conversion w/o printing:
>>a = "12\n34"
>>' '.join(('%#04x' % ord(c) for c in a))
'0x31 0x32 0x0a 0x33 0x34'
>>' '.join(('%02x' % ord(c) for c in a))
'31 32 0a 33 34'

--
Grant Edwards grante Yow! Vote for ME -- I'm
at well-tapered, half-cocked,
visi.com ill-conceived and
TAX-DEFERRED!
Jun 5 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
36825
by: Bob | last post by:
I'm having trouble the string.Format() throwing exceptions and I can't figure out what I am doing wrong. Given the following setup code: string str = { "one", "two", "three", "four" }; double val = { 1.0, 2.0, 3.0, 4.0 }; string fmt = "{0} {1} {2} {3}"; The following variations of string.Format() will work just fine with strings: string s1 = String.Format(fmt, str, str, str, str); // works
7
1626
by: Tommy Vercetti | last post by:
The first three of these statements work. The fourth doesn't. String::Format("{0}{0}{0}{0}", S""); String::Format("{0}{1}{0}{0}", S"", S""); String::Format("{0}{1}{2}{0}", S"", S"", S""); String::Format("{0}{1}{2}{3}", S"", S"", S"", S""); Question #1: Why? Question #2: What is the simplest way to get this to work. I would
33
3659
by: Jordan Tiona | last post by:
How can I make one of these? I'm trying to get my program to store a string into a variable, but it only stores one line. -- "No eye has seen, no ear has heard, no mind can conceive what God has prepared for those who love him" 1 Cor 2:9
8
4985
by: Lucky | last post by:
hi guys! back again with another query. the problem is like this. i want to print a line like this: "---------------------------------------------" the easiest way is to simply assign it to string and print it. but i want to use the String.Format() method if possible to do it.
4
2322
by: carry | last post by:
Hi everybody, I need simple string formatting like string.Format() does, but just with strings what seems not to work. Can anyone tell me please why this doesn't work and how can it be done? This looked like the simple and good solution but no. class Example { string format = "{1} {2} {3}" string anotherFormat = "{3}{1}{2}";
5
38519
by: Zytan | last post by:
I am surprised that a single character string is not auto-created from a single char. It is hard to find information on converting a char into a string, since most people ask how to convert char to string. I have a function that accepts a string, and I wish to access this function for each character in the string, individually. The only solution I have found is: char c = 'A'; string s = System.Text.Encoding.ASCII.GetString(c);
2
2148
by: Oki | last post by:
I appreciate any help; I'm currently getting a problem with trying to concatenate a single char from a structure to an existing string. Basically, the problem is here: strcat( res, morse_c.letter ) morse_c.letter should return an alphabet 'a' or 'b', etc... I'm trying to concatenate that to a char *res and it returns the error: 'strcat' : cannot convert parameter 2 from 'char' to 'const char *' I"ve tried casting it --> (const...
3
2251
by: Frank Rizzo | last post by:
Hello, I am trying to print out an hour in a format of 3pm or 5am. I've tried the following, but it throws an exception saying invalid input string: string times = string.Format("{0:h}{1:tt}", startTime, startTime); However, if I change h to hh, it works fine, but then it prints out 03am string times = string.Format("{0:hh}{1:tt}", startTime, startTime);
13
3730
by: Hongyu | last post by:
Hi, I have a datetime char string returned from ctime_r, and it is in the format like ""Wed Jun 30 21:49:08 1993\n\0", which has 26 chars including the last terminate char '\0', and i would like to remove the weekday information that is "Wed" here, and I also would like to replace the spaces char by "_" and also remove the "\n" char. I didn't know how to truncate the string from beginning or replace some chars in a string with another...
0
7998
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8491
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8470
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8142
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6010
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5475
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3959
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4022
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1580
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.