473,725 Members | 2,006 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?


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


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

0x31 0x31 0x32 0x33 0x34

Jun 4 '07 #1
4 25450
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


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

0x31 0x31 0x32 0x33 0x34
pyfor c in a:
.... print "%#x" % ord(c),
0x30 0x31 0x32 0x33 0x34

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:
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
Jun 5 '07 #5

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

Similar topics

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
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
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
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.
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}";
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);
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...
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);
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...
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,...
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...
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
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...
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();...
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...
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.