binary numbers

B

Brad Tilley

How can I specify that a number be represented in binary format when
passing it to a function? I'm working with the Windows registry and
REG_BINARY values expect numbers in binary form...

This would be nice:

import binary
x = binary(5)

Any tips???

Thanks,
Brad
 
L

Larry Bates

Brad said:
How can I specify that a number be represented in binary format when
passing it to a function? I'm working with the Windows registry and
REG_BINARY values expect numbers in binary form...

This would be nice:

import binary
x = binary(5)

Any tips???

Thanks,
Brad

Integers are stored in memory as binary.

x=5

stores 5 in x in binary form.

Larry Bates
 
M

Max M

I am not quite shure that I understand you correctly. But I think so.

You are probaly loking for the struct module. It handles strings as
lumps of binary data. So if you want to store something as "binary" you
convert it to a string containing binary data, and pass that string to
the registry.

It's not that hard. A function doing what you describe above could be
written a bit like:

# converts int to "binary short"
from struct import pack
x = pack('h', 5)

So technically x is now a string, but it contains the binary
representation of bytes that is a "short" on your system. I am not shure
if the registry expects a short though.


more info in the docs at:

4.3 struct -- Interpret strings as packed binary data



--

hilsen/regards Max M, Denmark

http://www.mxm.dk/
IT's Mad Science
 
M

Max M

I am not quite shure that I understand you correctly. But I think so.

You are probaly loking for the struct module. It handles strings as
lumps of binary data. So if you want to store something as "binary" you
convert it to a string containing binary data, and pass that string to
the registry.

It's not that hard. A function doing what you describe above could be
written a bit like:

# converts int to "binary short"
from struct import pack
x = pack('h', 5)

So technically x is now a string, but it contains the binary
representation of bytes that is a "short" on your system. I am not shure
if the registry expects a short though.


more info in the docs at:

4.3 struct -- Interpret strings as packed binary data



--

hilsen/regards Max M, Denmark

http://www.mxm.dk/
IT's Mad Science
 
D

Darren Kirby

quoth the Larry Bates:
Integers are stored in memory as binary.

x=5

stores 5 in x in binary form.

I think our friend is looking for a module or function that turns 5 into 101
(or perhaps 00000101)

Brad, have a look here:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/219300

-d
--
Part of the problem since 1976
http://badcomputer.no-ip.com
Get my public key from
http://keyserver.linux.it/pks/lookup?op=index&search=bulliver
"...the number of UNIX installations has grown to 10, with more expected..."
- Dennis Ritchie and Ken Thompson, June 1972

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQBBYydSOrzWcOwL7mwRAgeZAKCYaphj8r6TmAF/WvqEujtCYXbJnwCfe+XI
OuTl78mhfJqBwL5MWDPdOBc=
=Ki6O
-----END PGP SIGNATURE-----
 
B

Brad Tilley

Max said:
I am not quite shure that I understand you correctly. But I think so.

You are probaly loking for the struct module. It handles strings as
lumps of binary data. So if you want to store something as "binary" you
convert it to a string containing binary data, and pass that string to
the registry.

It's not that hard. A function doing what you describe above could be
written a bit like:

# converts int to "binary short"
from struct import pack
x = pack('h', 5)

So technically x is now a string, but it contains the binary
representation of bytes that is a "short" on your system. I am not shure
if the registry expects a short though.


more info in the docs at:

4.3 struct -- Interpret strings as packed binary data

I Forgot that I'm asking a bunch of CS geeks a
question about "binary"... I mean "bianry" as in
the base 2 representation of numbers... One is one
is one, but one can be represented in different
bases and still be one... get it?

Dec Bin
1 1
2 10
3 11
4 100
5 101
6 110
....
....
....

How do I pass the binary representation of a
number to a function???
 
R

Roel Schroeven

Brad said:
Thanks, this is what I need.

Are you sure? I mean, are you really really sure? REG_BINARY indeed
expects data in binary form, but that does not mean that you should
convert it to a string of 0's and 1's. It means that it can take any
series of bytes, and doesn't care what is in those bytes.

The struct module approach as explained by Max M sounds like a much
better idea in your case. Even better to use REG_DWORD instead of
REG_BINARY if that's possible for you.
 
T

Tim Roberts

Brad Tilley said:
I Forgot that I'm asking a bunch of CS geeks a
question about "binary"... I mean "bianry" as in
the base 2 representation of numbers... One is one
is one, but one can be represented in different
bases and still be one... get it?

Not inside a computer, it can't.
Dec Bin
1 1
2 10
3 11
4 100
5 101
6 110
...
How do I pass the binary representation of a
number to a function???

I'm afraid that it is YOU that does not understand.

Binary is the ONLY representation used inside a computer. When you see the
number "5", it is because a utility function has neatly converted the
binary value in a variable or register to an ASCII format that your human
brain can comprehend. ALL of the following things are equivalent:

x = 15
x = 037
x = 0x0f
x = 6+9
x = len("abcdefghijklmno")

In every case, the variable "x" will contain the binary value 000..0000101,
again because a utility function has neatly converted the human-convenient
forms above into a binary value.

The registry type REG_BINARY does not really mean binary. It means an
arbitrary array of bytes. The only difference between REG_BINARY and
REG_SZ is that a REG_BINARY value can contain zeros, and is displayed
differently in a registry dump.
 
T

Tim Roberts

Brad Tilley said:
Thanks, this is what I need.

No, it isn't. Registry REG_BINARY values are NOT binary, in the sense of
"a string of ones and zeros". REG_BINARY means "an arbitrary list of
bytes". The string "ABCD" is a perfectly legitimate value for a REG_BINARY
registry value; it will display in a hex dump as "41 42 43 44 00".
 
D

Darren Kirby

quoth the Tim Roberts:
No, it isn't. Registry REG_BINARY values are NOT binary, in the sense of
"a string of ones and zeros". REG_BINARY means "an arbitrary list of
bytes". The string "ABCD" is a perfectly legitimate value for a REG_BINARY
registry value; it will display in a hex dump as "41 42 43 44 00".

Well, to defend my own ignorance here, I have no idea about editing a windows
registry, in fact I do not run windows. Brad's original post was:
This would be nice:
import binary
x = binary(5)

And the info I pointed him to was exactly what he asked for...
Seems a good thing that we have some "CS" types around here :)

-d
--
Part of the problem since 1976
http://badcomputer.no-ip.com
Get my public key from
http://keyserver.linux.it/pks/lookup?op=index&search=bulliver
"...the number of UNIX installations has grown to 10, with more expected..."
- Dennis Ritchie and Ken Thompson, June 1972

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQBBZRtnOrzWcOwL7mwRAlD9AJ0f54yP/CFGoLv0ArV4jpZLZ0OsSQCfQjLW
QOop1emz+3VyQQPyDyfa1m8=
=cAxR
-----END PGP SIGNATURE-----
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top