473,321 Members | 1,622 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,321 software developers and data experts.

Convert hexadecimal string to binary

Guys,
I have the following hexadecimal string - '\xff\xff\xff' which i need
to convert to binary.

How would i go about doing this?

Eric
Jul 18 '05 #1
3 6556
Eric wrote:
I have the following hexadecimal string - '\xff\xff\xff' which i need
to convert to binary.

How would i go about doing this?


You'll need to explain more clearly, and possibly change how you
view such things, before anyone can help you with certainty.

What you show above is a *binary* sequence of bytes containing
three bytes, each with the value 255. The representation of
it which you copied (?) from the console contains printable
characters and extra stuff to make it easier to read, but that's
what you get from repr() on a binary string.

Do you actually want a string containing the six printable
characters 'FFFFFF'? If that's the case, it's not what you
would usually call "binary". Anyway, use binascii.hexlify()
for that...

Can you give an example of the output you really want? How
many bytes does it contain? How does it differ from the string
above?

-Peter
Jul 18 '05 #2
ek****@yahoo.com (Eric) writes:
Guys,
I have the following hexadecimal string - '\xff\xff\xff' which i need
to convert to binary.

How would i go about doing this?

Eric


Assuming you mean the result is a string of "1" and "0" chars, here is
a mechanism. We use a lookup to avoid converting to and from internal
numbers.

#---x is the data---
x='\xff\xff\xff'
#---notice it is actually a sequence of 8-bit bytes---
#---generate a dictionary of all 256 values and their binary codings---
bits={'\x00':'00000000',
'\x01':'00000001',
..........
'\xff':'11111111'}

#---collect answer in buffer---
s=StringIO.StringIO()
for byte in x:
s.write(bits[byte])

#---report result---
print s.getvalue()

--
ha************@boeing.com
6-6M21 BCA CompArch Design Engineering
Phone: (425) 342-0007
Jul 18 '05 #3
On 22 Apr 2004 08:06:26 -0700, ek****@yahoo.com (Eric) wrote:
Guys,
I have the following hexadecimal string - '\xff\xff\xff' which i need
to convert to binary.

How would i go about doing this?

Assuming that string is the representation of a string of bytes whose binary
values are 255 (or hex 0xff), and you want to view it like a big-endian
number representation, and convert it to an integer (or long, if it gets big),
def s2i(s): return sum([ord(c)*256**i for i,c in enumerate(s[::-1])]) ... s2i('\xff\xff\xff') 16777215 hex(s2i('\xff\xff\xff')) '0xffffff' hex(s2i('\x01\x02\x03')) '0x10203' hex(s2i('\x01\x02\x03\x04')) '0x1020304' hex(s2i('\x01\x02\x03\x04\x05')) '0x102030405L' hex(s2i('\x01\x02')) '0x102' s2i('\x01\x02') 258

Note that this is unsigned --
hex(s2i('\xff\xff\xff\xff')) '0xFFFFFFFFL' hex(s2i('\x80\x00\x00\x00'))

'0x80000000L'

You could do it other and faster ways...

Regards,
Bengt Richter
Jul 18 '05 #4

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

Similar topics

2
by: Joel Moore | last post by:
Maybe I'm just easily baffled after an all-nighter but I can't seem to figure out how to represent a BitArray as a hexadecimal string. For example: Dim outputBank As New BitArray(8) ...
4
by: CSharpener | last post by:
This should be *so* easy! How do I convert a Byte or int to a binary string representation in C# In JavaScript, it goes like this for an int: javascript:(123).toString(2 or...
6
by: MrKrich | last post by:
I want to convert Hexadecimal or normal integer to Binary. Does VB.Net has function to do that? I only found Hex function that convert normal integer to Hexadecimal.
13
by: muss | last post by:
how to convert decimal number in a hexadecimal number using C? Please help me. Thanks.
8
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...
7
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...
6
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++){...
6
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.
10
by: cmdolcet69 | last post by:
Public ArrList As New ArrayList Public bitvalue As Byte() Public Sub addvalues() Dim index As Integer ArrList.Add(100) ArrList.Add(200) ArrList.Add(300) ArrList.Add(400) ArrList.Add(500)
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.