473,769 Members | 2,106 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 6592
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.hexlif y()
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.co m (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':'0 0000000',
'\x01':'0000000 1',
..........
'\xff':'1111111 1'}

#---collect answer in buffer---
s=StringIO.Stri ngIO()
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.co m (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\x ff') 16777215 hex(s2i('\xff\x ff\xff')) '0xffffff' hex(s2i('\x01\x 02\x03')) '0x10203' hex(s2i('\x01\x 02\x03\x04')) '0x1020304' hex(s2i('\x01\x 02\x03\x04\x05' )) '0x102030405L' hex(s2i('\x01\x 02')) '0x102' s2i('\x01\x02') 258

Note that this is unsigned --
hex(s2i('\xff\x ff\xff\xff')) '0xFFFFFFFFL' hex(s2i('\x80\x 00\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
8944
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) outputBank(0) = True outputBank(1) = False outputBank(2) = False
4
4293
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 javascript:(0xFE).toString(2 No problem. So, how do I do the same in C#? What simple thing am I missing
6
43663
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
5596
by: muss | last post by:
how to convert decimal number in a hexadecimal number using C? Please help me. Thanks.
8
18655
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
19229
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
14691
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
16919
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
4969
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
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10216
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
10049
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
9997
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
9865
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
8873
agi2029
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...
0
5310
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...
1
3965
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
3565
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.