1

I am working in python based on a Java code.

I have this in Java:

public static byte[]  datosOEM = new byte[900000]; 
public static byte x1=0,x2=0,x3=0,x4=0,x5=0,x6=0;

I wrote this in Python following some documents that I found:

datosOEM=bytes([0x90, 0x00, 0x00])
x1=bytes([0x00])
x2=bytes([0x00])
x3=bytes([0x00])
x4=bytes([0x00])
x5=bytes([0x00])
x6=bytes([0x00])

When I run my program it shows me this:

Traceback (most recent call last):
  File "test.py", line 63, in <module>
    x1=datosOEM[k];
IndexError:string index out of range

How I can fix this problem? Any help is well received.

Part of my code is this:

...
response=port.read(8)
print(response)
k=0
C=0
conexion=True
if(conexion):
    while(response>200):
        while(C==0):
            x1=datosOEM[k];
            if(x1=1):
                x2=datosOEM[k+1];
... 

Also, what can I do to not repeat that mistake again?

Thank you in advance for your help

This post was editated by JasonMArcher, thank you much for this.

  • 2
    You defined a `bytes` object with all of 3 bytes. The Java code defines a bytes array of 900000 bytes. That's a big size difference right there. The hex bytes 0x90, 0x00 and 0x00 are *very different values*. – Martijn Pieters Apr 15 '14 at 16:54
  • 3
    The Java code is probably best **not** translated as literally as all that. I doubt that you actually need to do the same thing in Python. – Martijn Pieters Apr 15 '14 at 16:55
  • Craig recommended me that I have to changed this: datosOEM=bytes([0x90, 0x00, 0x00]). For this: datosOEM=bytearray[900000], but now I have this problem: TypeError: 'type' object has no attribute '_getitem_'. What I can do to solve this new problem? What do you think about his solution? – user3429256 Apr 16 '14 at 15:04

1 Answers1

0

I'm not sure what you're trying to achieve. Maybe something like that?

datosOEM = [0]*900000 # a list of length 900.000, initialized with zeros
x1 = 0
x2 = 0
x3 = 0
# ...

Maybe you even want this:

datosOEM = [0]*900000 # a list of length 900.000, initialized with zeros
x = [0]*6
# ...
# ...
    while (C==0):
        x[0] = datosOEM[k];
        if (x[0] = 1):
            x[1] = datosOEM[k+1];
        # ...
jammon
  • 3,404
  • 3
  • 20
  • 29