473,809 Members | 2,718 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

binary string question

Hello,

I'm trying to figure out how to get bit operators to work on a binary
string. This is what I'm trying to do:

list(frame) #where frame is a binary string
y = frame[x] << 8

It gives me a TypeError. Whats the best way to get around that? I think
the struct module may be what I'm looking for, if so could someone give
me an example? Also, is there a better way to be able to access
individual bytes than converting the string to a list?

Thanks,

Dan Jones
Jul 18 '05 #1
7 3595
Dan Jones wrote:
Hello,

I'm trying to figure out how to get bit operators to work on a binary
string. This is what I'm trying to do:

list(frame) #where frame is a binary string
y = frame[x] << 8

It gives me a TypeError. Whats the best way to get around that? I think
the struct module may be what I'm looking for, if so could someone give
me an example? Also, is there a better way to be able to access
individual bytes than converting the string to a list?


You can access an individual byte simply by indexing the string -- for
any string x, x[0] is the first byte, x[1] the second one, and so on. As
to what you mean by "shifting by 8 bits", I dunno -- just slice with
x[1:] is what you perhaps may mean...? For N bits where N is not a
multiple of 8 you have a serious problem, probably best met with
extension module gmpy if performance is important.
Alex

Jul 18 '05 #2
Dan Jones wrote:

I'm trying to figure out how to get bit operators to work on a binary
string. This is what I'm trying to do:

list(frame) #where frame is a binary string
y = frame[x] << 8
You don't describe what you want this to do. How about an example, showing
input and desired output?
It gives me a TypeError. Whats the best way to get around that? I think
the struct module may be what I'm looking for, if so could someone give
me an example? Also, is there a better way to be able to access
individual bytes than converting the string to a list?


Yes, use an array (see the module by that name).

On the other hand, depending on what you really want, the answer might
be as simple as taking your string and skipping the first element.
After all, shifting each byte in a long sequence to the left by 8 bits
is pretty much synonymous with that approach:

out = in[1:] + '\x00'

But I suspect that's not really what you meant. Please explain.

-Peter
Jul 18 '05 #3
On Fri, 07 Nov 2003 23:20:05 GMT, Alex Martelli wrote:
Dan Jones wrote:
I'm trying to figure out how to get bit operators to work on a binary
string.


You can access an individual byte simply by indexing the string


It's possible the original poster is asking (ambiguously) about treating
a string consisting of '0' and '1' digits, as a binary number.

--
\ "There's a fine line between fishing and standing on the shore |
`\ looking like an idiot." -- Steven Wright |
_o__) |
Ben Finney <http://bignose.squidly .org/>
Jul 18 '05 #4
Dan Jones <dj****@shcorp. com> writes:
Hello,

I'm trying to figure out how to get bit operators to work on a binary
string.
More below, but are you sure you really need to do this?
This is what I'm trying to do:

list(frame) #where frame is a binary string
This has no effect (apart from wasting ressources). You create a new list from
the string frame, but don't use it any further. You presumably intended

frame = list(frame)
y = frame[x] << 8

It gives me a TypeError. Whats the best way to get around that?
Well see below (not that I can see how rightshifting by 8 is going to make
much sense if you want to treat a string as collection of bytes).
I think the struct module may be what I'm looking for,
You want to treat each item in the string the same, right? In that case use
array.
if so could someone give me an example?
Here you go:
import array
frame = "abcde"
frameAsArray = array.array('b' , frame)
frameAsArray[0] >>= 1
frameAsArray.to string()

'0bcde'
Also, is there a better way to be able to access individual bytes than
converting the string to a list?


see above

'as
Jul 18 '05 #5
Ben Finney wrote:
On Fri, 07 Nov 2003 23:20:05 GMT, Alex Martelli wrote:
Dan Jones wrote:
I'm trying to figure out how to get bit operators to work on a binary
string.


You can access an individual byte simply by indexing the string


It's possible the original poster is asking (ambiguously) about treating
a string consisting of '0' and '1' digits, as a binary number.


oh, then:

x = long(thestringo f0sand1s, 2)

might work, but to access individual bits, bytes, etc, you need masks and
shifts.
Alex

Jul 18 '05 #6
On Fri, 2003-11-07 at 18:22, Peter Hansen wrote:
Dan Jones wrote:

I'm trying to figure out how to get bit operators to work on a binary
string. This is what I'm trying to do:

list(frame) #where frame is a binary string
y = frame[x] << 8


You don't describe what you want this to do. How about an example, showing
input and desired output?
It gives me a TypeError. Whats the best way to get around that? I think
the struct module may be what I'm looking for, if so could someone give
me an example? Also, is there a better way to be able to access
individual bytes than converting the string to a list?


Yes, use an array (see the module by that name).

On the other hand, depending on what you really want, the answer might
be as simple as taking your string and skipping the first element.
After all, shifting each byte in a long sequence to the left by 8 bits
is pretty much synonymous with that approach:

out = in[1:] + '\x00'

But I suspect that's not really what you meant. Please explain.

-Peter


Sorry, I should have included more information. I'm trying to write a
webcam app to grab and display images off a Philips (pwc) camera in
Linux. What I have so far is based on the mini-tv.py demo in the pyv4l
module. It will work with RGB output such as from a tv tuner card but
the Philips cameras use a different color palette. I found a C function
that converts the YUV420P color palette to RGB, and I'm trying to
convert it to python. The binary string in question is the image data
for one frame of video. To display it, I'm using PIL's
Image.fromstrin g() and tkinter.

I don't know much C at all and am fairly new to python as well, but I'll
post what I have so far.
Here's the C version:

static void ccvt_420p(int width, int height, const unsigned char *src,
unsigned char *dst, int push)
{
int line, col, linewidth;
int y, u, v, yy, vr, ug, vg, ub;
int r, g, b;
const unsigned char *py, *pu, *pv;

linewidth = width >> 1;
py = src;
pu = py + (width * height);
pv = pu + (width * height) / 4;

y = *py++;
yy = y << 8;
u = *pu - 128;
ug = 88 * u;
ub = 454 * u;
v = *pv - 128;
vg = 183 * v;
vr = 359 * v;

for (line = 0; line < height; line++) {
for (col = 0; col < width; col++) {
r = (yy + vr) >> 8;
g = (yy - ug - vg) >> 8;
b = (yy + ub ) >> 8;

if (r < 0) r = 0;
if (r > 255) r = 255;
if (g < 0) g = 0;
if (g > 255) g = 255;
if (b < 0) b = 0;
if (b > 255) b = 255;

switch(push) {
case PUSH_RGB24:
*dst++ = r;
*dst++ = g;
*dst++ = b;
break;

case PUSH_BGR24:
*dst++ = b;
*dst++ = g;
*dst++ = r;
break;

case PUSH_RGB32:
*dst++ = r;
*dst++ = g;
*dst++ = b;
*dst++ = 0;
break;

case PUSH_BGR32:
*dst++ = b;
*dst++ = g;
*dst++ = r;
*dst++ = 0;
break;
}

y = *py++;
yy = y << 8;
if (col & 1) {
pu++;
pv++;

u = *pu - 128;
ug = 88 * u;
ub = 454 * u;
v = *pv - 128;
vg = 183 * v;
vr = 359 * v;
}
} /* ..for col */
if ((line & 1) == 0) { // even line: rewind
pu -= linewidth;
pv -= linewidth;
}
} /* ..for line */
}

void ccvt_420p_rgb24 (int width, int height, const void *src, void *dst)
{
ccvt_420p(width , height, (const unsigned char *)src, (unsigned
char *)dst, PUSH_RGB24);
}

############### ##########

And here's what I have so far:
def yuv420p_to_rgb2 4(WIDTH, HEIGHT, source):
line, col, linewidth = 0,0,0
y,u,v,yy,vr,ug, vg,ub = 0,0,0,0,0,0,0,0
r,g,b = 0,0,0
linewidth = WIDTH >> 1
sourceframe = list(source)
py = 0
pu = py + (WIDTH * HEIGHT)
pv = pu + (WIDTH * HEIGHT) / 4
destination = []
destindex = 0

y = sourceframe[py]
py += 1
int(y)
yy = y << 8 # This is where it blows up
u = sourceframe[pu] - 128
ug = 88 * u
ub = 454 * u
v = sourceframe[pv] - 128
vg = 183 * v
vr = 359 * v

line = 0
while (line < HEIGHT):
col = 0
while (col < WIDTH):
r = (yy + vr) >> 8
g = (yy - ug - vg) >> 8
b = (yy + ub ) >> 8

if (r < 0):
r = 0
if (r > 255):
r = 255
if (g < 0):
g = 0
if (g > 255):
g = 255
if (b < 0):
b = 0
if (b > 255):
b = 255

destination[destindex] = r
destindex += 1
destination[destindex] = g
destindex += 1
destination[destindex] = b
destindex += 1

y = sourceframe[py]
py += 1
yy = y << 8
if (col & 1):
pu += 1
pv += 1
u = sourceframe[pu] - 128
ug = 88 * u
ub = 454 * u
v = sourceframe[pv] - 128
vg = 183 * v
vr = 359 * v

col += 1
#end while col

if ((line % 1) == 0): # even line: rewind
pu -= linewidth
pv -= linewidth

line += 1
#end while line
return destination



Jul 18 '05 #7
Dan Jones <dj****@shcorp. com> wrote in message news:<ma******* *************** **************@ python.org>...
....
....
....
static void ccvt_420p(int width, int height, const unsigned char *src,
unsigned char *dst, int push)
{
int line, col, linewidth;
int y, u, v, yy, vr, ug, vg, ub;
int r, g, b;
const unsigned char *py, *pu, *pv;

linewidth = width >> 1;
py = src;
pu = py + (width * height);
pv = pu + (width * height) / 4;

y = *py++;
I guess here you're using char as int.
yy = y << 8;
u = *pu - 128; ....
....
.... for (line = 0; line < height; line++) {
for (col = 0; col < width; col++) {
r = (yy + vr) >> 8;
g = (yy - ug - vg) >> 8;
b = (yy + ub ) >> 8; ....
....
....
And here's what I have so far:
def yuv420p_to_rgb2 4(WIDTH, HEIGHT, source):
line, col, linewidth = 0,0,0
y,u,v,yy,vr,ug, vg,ub = 0,0,0,0,0,0,0,0
r,g,b = 0,0,0
linewidth = WIDTH >> 1
To do the same in pyhton ... sourceframe = list(source) ... use sourceframe = map(ord,source) instead !!
py = 0
pu = py + (WIDTH * HEIGHT)
pv = pu + (WIDTH * HEIGHT) / 4
destination = []
destindex = 0
!!!! *y* becomes an int because *sourceframe* is a list of ints !!!! y = sourceframe[py]
py += 1
*********The following line has no effect********* ************* int(y) *************** *************** *************** *************** ***

!!!! Now *y* is an int and can be shifted !!!! yy = y << 8 # This is where it blows up


....
....
....

Regards
Peter
Jul 18 '05 #8

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

Similar topics

6
57573
by: Andrew | last post by:
Hi I have a question is there a function in C++ to convert an integer into a Binary number Thanks in Advance Cheers
1
8775
by: Niko Korhonen | last post by:
I'm currently in the process of programming a multimedia tagging library in standard C++. However, I've stumbled across one or two unclear issues while working with the library. First of all, is it safe to store binary data in std::string? This question rose from my implementation with APEv2 tags. An APEv2 tag's field value can contain either UTF encoded text or binary data. I've decided to use std::string to represent the field value....
5
2931
by: sathyashrayan | last post by:
Group, I have some doubts in the following program. ------------------program--------------------- /* ** Make an ascii binary string into an integer. */ #include <string.h> unsigned int bstr_i(char *cptr)
10
2388
by: jt | last post by:
I'm needing to take a binary string start at a certain position and return a pointer from that postion to the end of the binary stirng. something like this: char bstr; char *pos; pos=mid(bstr,35); / *return a pointer of the rest of the binary string starting at element 35 */
9
1955
by: ruffiano | last post by:
Hi, can someone tell me if a C++ string (std::string) represents a binary or an ASCII string? Thanks in advance.
4
2567
by: Freddy Coal | last post by:
Hi, I'm recovery data from an spectrum analizer, the equipment send me data in binary, each data in the trace have 4 bytes, I would like convert that to double, how make that?. I'm trying extract the data in substrings of 4 chars, and convert this with Cdbl(my substring), but I get an InvalidCastException in the conversion. Example of the spectrum data: ÿþÃÑÿþÉLÿþÃÿþË:ÿþ²øÿþÂ&ÿþÆAÿþ®ÿþÄ5ÿþÎ%ÿþ¹»ÿþÊ
11
3602
by: Freddy Coal | last post by:
Hi, I'm trying to read a binary file of 2411 Bytes, I would like load all the file in a String. I make this function for make that: '-------------------------- Public Shared Function Read_bin(ByVal ruta As String) Dim cadena As String = "" Dim dato As Array If File.Exists(ruta) = True Then
3
4825
by: Freddy Coal | last post by:
Hi, I would like append strings to a binary file, but I don´t understand how make that. I try with: FileOpen(1, Folder_Trabajo & "\Toma_Trazas.FC", OpenMode.Append, OpenAccess.Write, OpenShare.Default) FilePut(1, "My string") '<****** ERROR IN THIS LINE ****** FileClose(1)
11
4310
by: David Mathog | last post by:
In the beginning (Kernighan & Ritchie 1978) there was fprintf, and unix write, but no fwrite. That is, no portable C method for writing binary data, only system calls which were OS specific. At C89 fwrite/fread were added to the C standard to allow portable binary IO to files. I wonder though why the choice was made to extend the unix function write() into a standard C function rather than to extend the existing standard C function...
0
9602
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
10120
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
9200
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...
1
7661
isladogs
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...
0
6881
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
5550
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...
0
5688
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4332
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
3
3015
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.