473,806 Members | 2,248 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Hexadecimal Conversion in Python

Hello, I am reading in a socket message from a server and am only
receiving this '....'. Now obviously it is in the wrong format. How
would I convert these bys in Python, I have looked everywhere but I do
not see much documentation on converting ptyhon types to other data
types.
Any Help would be appreciated.

Nov 2 '05 #1
10 2720
DaBeef wrote:
Hello, I am reading in a socket message from a server and am only
receiving this '....'. Now obviously it is in the wrong format. How
would I convert these bys in Python, I have looked everywhere but I do
not see much documentation on converting ptyhon types to other data
types.


What is the server supposed to send (you need to know that
if you want to decode it).
Then, lookup struct.unpack .

HTH

Stefan
Nov 2 '05 #2
it is returning data such as 0x04. I am new to python so this is a
pain for me, learning to do this in a language whose llibrary is
somewhat limited. But instead I receieve .... So I wnat to convert to
the original data. Also can you define a constant in Python such as
#define value 0x04
Thank-you for your help

Nov 2 '05 #3
DaBeef wrote:
it is returning data such as 0x04.
But you need to know what kind of data the other side is sending,
i.e. what kind of protocol it speaks.
I am new to python
New and starting with socket/network programming ?
Brave!
so this is a
pain for me, learning to do this in a language whose llibrary is
somewhat limited.


You mean Pythons' library is limited?
You know nothing...
But instead I receieve .... So I wnat to convert to

the original data. Also can you define a constant in Python such as
#define value 0x04


You should look here:

http://docs.python.org/

And probably here, too:

http://diveintopython.org/toc/index.html
What does your 'socket receiver' look like ?
Stefan
--
Stefan Näwe
stefan_AT_naewe _DOT_de
Nov 2 '05 #4
"DaBeef" wrote:/
it is returning data such as 0x04. I am new to python so this is a
pain for me, learning to do this in a language whose llibrary is
somewhat limited.


you make no sense at all.

what are you receiving data from? how are you receiving it? what
library are you using? what's 0x04? an integer? a string? a byte?
what's .... ? a string? a typo? an odd character that's munged by
your mail program?

</F>

Nov 2 '05 #5
I have been coding for 5 years. This is a proprietary protocol, so it
is difficult converting. I did this in java but was just able to
convert a stream. I looked through the Python library, I am more or
less getting backa string represented as a "...." So now I want to
convert it to all the hexa, bin until I see a match and can then work
teh rest of my program

Nov 2 '05 #6
On 2 Nov 2005 12:28:26 -0800, "DaBeef" <De***********@ gmail.com> wrote:
Hello, I am reading in a socket message from a server and am only
receiving this '....'. Now obviously it is in the wrong format. How
would I convert these bys in Python, I have looked everywhere but I do
not see much documentation on converting ptyhon types to other data
types.
Any Help would be appreciated.

print repr(msg)
where msg is what you _actually_ read (and tell us how you got the message in, BTW)
and show us a copy/pasted copy from your screen.
Unless you are very very good at descriptions, it's hard to beat presentation of
machine representations of what you are talking about ;-)

Regards,
Bengt Richter
Nov 2 '05 #7
On 2005-11-02, DaBeef <De***********@ gmail.com> wrote:
I have been coding for 5 years. This is a proprietary protocol, so it
is difficult converting.
Eh? What's so difficult about it?
I did this in java but was just able to convert a stream.
Yet you seem unable to describe what it is you're trying to do.
I looked through the Python library, I am more or less getting
backa string represented as a "...."
And what is it you _want_? If the other end sent you four
ASCII "." bytes, shouldn't that be what you see?
So now I want to convert it to all the hexa, bin
Sorry, I've no clue what "hexa, bin" is or how to convert to
it.
until I see a match and can then work teh rest of my program


I have absolutely no idea what you're trying to do, but maybe
this will help: In Python a "string" is an array of 8-bit
bytes.

If you want the integer equivalent of the 3rd byte in a string s,
do this:

b = ord(s[2])

For example:
s = "ABC"
ord(s[0]) 65 ord(s[1]) 66 ord(s[2]) 67
If you want a list of the integer equivalents of the bytes in a
string, do this:

bl = [ord(c) for c in s]
[ord(c) for c in s]

[65, 66, 67]

--
Grant Edwards grante Yow! Make me look like
at LINDA RONSTADT again!!
visi.com
Nov 2 '05 #8
On 2 Nov 2005 12:53:45 -0800, "DaBeef" <De***********@ gmail.com> wrote:
I have been coding for 5 years. This is a proprietary protocol, so it
is difficult converting. I did this in java but was just able to
convert a stream. I looked through the Python library, I am more or
less getting backa string represented as a "...." So now I want to
convert it to all the hexa, bin until I see a match and can then work
teh rest of my program

Maybe the binascii module's hexlify will get you into territory more
familiar to you? Python generally stores byte data as type str "strings."
If you want to see the bytes as hex (a string of hex characters ;-) you can e.g.,
import binascii
binascii.hexlif y('ABC123...\x0 1\x02\x03') '4142433132332e 2e2e010203'

To convert individual character, you can use a format string on the ordinal value
for c in 'ABC123...\x01\ x02\x03': print '%02X'%ord(c), ...
41 42 43 31 32 33 2E 2E 2E 01 02 03

Or perhaps you really want the integer ordinal value itself?
for c in 'ABC123...\x01\ x02\x03': print ord(c), ...
65 66 67 49 50 51 46 46 46 1 2 3

(print obviously does a conversion to decimal string representation for output)

If you are interested in the bits, you can check them with bit operations, e.g.,
for c in 'ABC123...\x01\ x02\x03':

... print ''.join(chr(48+ ((ord(c)>>b)&1) ) for b in xrange(7,-1,- 1)),
...
01000001 01000010 01000011 00110001 00110010 00110011 00101110 00101110 00101110 00000001 00000010 00000011

(cf. 41 42 42 etc above)

Regards,
Bengt Richter
Nov 2 '05 #9
On Wednesday 02 November 2005 12:53, DaBeef wrote:
I have been coding for 5 years. This is a proprietary protocol, so it
is difficult converting. I did this in java but was just able to
convert a stream. I looked through the Python library, I am more or
less getting backa string represented as a "...." So now I want to
convert it to all the hexa, bin until I see a match and can then work
teh rest of my program


Its great that you are learning python, but it seems you are confused about
how it works, so you are not making sense to a lot of people. Hex conversion
can be done in a lot of ways. You should look into "struct" as others have
suggested, which might be a more resource effecient (less processor cycles)
choice and also give you your data as numerical types. If you have managed to
get to the point of having a string that looks like this:

'\xcb\xdb\xbe\x ef'

Then the simplest thing for you to do (least amount of learning) at this point
might be to look at "decode" and "encode"

for example
hex_rep = '\xcb\xdb\xbe\x ef'.encode('hex ')
print hex_rep

'cbdbbeef'

Also, you might annoy (read: 'you have annoyed') a lot of people by saying
that python has a "llimited" library. Not only are you incorrect, but you
made a typo. You will do well to learn as much of the python library as you
can, but it will take some time. You will also do well to avoid typos and
grammatical errors in your communications.

Also, you need to answer Fredrik's question. Let me restate it. What do you
mean by '....'? This encodes in hex to '2e2e2e2e'. Is this the cipher text
you were expecting? If so, your company may want to re-think its encryption
algorithm.

James

--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/
Nov 2 '05 #10

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

Similar topics

10
22825
by: pavithra.eswaran | last post by:
Hi, I would like to convert a single precision hexadecimal number to floating point. The following program seems to work fine.. But I do not want to use scanf. I already have a 32 bit hexadecimal number and would like to convert it into float. Can anyone tell me how to do it? int main() { float theFloat;
5
7915
by: Damon | last post by:
I'm getting '', hexadecimal value 0x02, is an invalid character when I'm deseralizing XML from a 3rd party XML gateway. How do I get rid of these hexadecimal values before I deserialize? Cheers Damon
2
9757
by: akash deep batra | last post by:
hi i want to convert a 96 bit binary number into a hexadecimal number. e.g binary number= 001100010001010000100101011110111111010101110100010110000101011000101010000000000000000000000000 how can i do that in C#. also i want to convert a hexadecimal number (24 digits) into a binary number
8
18668
by: Vijay | last post by:
Hi , I am doing a small project in c. I have a Hexadecimal file and want to convert into ascii value. (i.e., Hexadecimal to Ascii conversion from a file). Could anyone help me? Thanks in adv.
7
19240
by: elliotng.ee | last post by:
I have a text file that contains a header 32-bit binary. For example, the text file could be: %%This is the input text %%test.txt Date: Tue Dec 26 14:03:35 2006 00000000000000001111111111111111 11111111111111111111111111111111 00000000000000000000000000000000 11111111111111110000000000000000
6
14755
by: Andrea | last post by:
Hi, suppose that I have a string that is an hexadecimal number, in order to print this string I have to do: void print_hex(unsigned char *bs, unsigned int n){ int i; for (i=0;i<n;i++){ printf("%02x",bs); } }
6
16921
by: sweeet_addiction16 | last post by:
hello Im writin a code in c... can sum1 pls help me out in writing a c code to convert decimalnumber to hexadecimal number.The hexadecimal number generated has to be an unsigned long.
14
3199
by: Ellipsis | last post by:
Ok so I am converting to hexadecimal from decimal and I can only cout the reverse order of the hexadecimal?! How could I reverse this so its the right order? Heres my code: #include <iostream> using namespace std; void binary(int number) { int remainder; if(number <= 1)
5
3215
by: Praveena P | last post by:
Hi folks, I am new to Python... so am not too sure about how the type conversion works. I have to read a file that contains hexadecimal data and use the data further to do some arithmetic calculations. A sample of the input is : 00000000000020E0000032F800000000400022005E The problem I am facing is this: I am using f.read(2) to read a byte at a time, but the data that is
0
9596
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
10364
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
10370
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,...
0
10109
tracyyun
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...
0
6876
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
5678
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4328
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3849
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3008
bsmnconsultancy
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.