Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 515765
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:39:41+00:00 2026-05-13T07:39:41+00:00

I’m hacking a security system DVR so I can add motion capture and other

  • 0

I’m hacking a security system DVR so I can add motion capture and other fun features in python (version 2.6). One of the functions I was able to decompile from java and convert to python was the following:

def ToInt(abyte0, i):

  if(abyte0[i] >= 0):
      j = abyte0[i]
      print "A " + str(j)
  else:
      j = 256 + abyte0[i]

  if abyte0[i + 1] >= 0:
      j = j + (abyte0[i + 1] * 256)
      print "A " + str(j)
  else:
      j = j + (256 + abyte0[i + 1]) * 256

  if abyte0[i + 2] >= 0:
      j = j+ abyte0[i + 2] * 256 * 256
      print "A " + str(j)
  else:
      j = j + (256 + abyte0[i + 2]) * 256 * 256

  if abyte0[i + 3] >= 0:
      j = j + abyte0[i + 3] * 256 * 256 * 256
      print "A " + str(j)
  else:
      j = j + (256 + abyte0[i + 3]) * 256 * 256 * 256

  return j

I added print statements to it to see what was going on while I was figuring things out. Now that I have it working, I went back to remove the print statements and I get an error!

Traceback (most recent call last):
  File "C:\Users\...\videostuff.py", line 154, in <module>
  ReadData(i)
    File "C:\Users\..\videostuff.py", line 122, in ReadData
    data = s.recv(DATA_SIZE)
    OverflowError: long int too large to convert to int

There seems to be a problem with converting from a long int to an int when reading from the socket with the returned value. But why does this work with the print statements in place?!

Calling code:

i = ToInt(data_string.tolist(), 0)

# First byte tells how big the data wil be
if i > 4 and (i - 4) > DATA_SIZE:
    DATA_SIZE = i - 4

# line 122
data = s.recv(DATA_SIZE)

All the codez:

import socket
from array import array
import Image
import StringIO
import sys

def ToInt(abyte0, i):

    if(abyte0[i] >= 0):
        j = abyte0[i]
        #print "A " + str(j)
    else:
        j = 256 + abyte0[i]

    if abyte0[i + 1] >= 0:
        j = j + (abyte0[i + 1] * 256)
        #print "A " + str(j)
    else:
        j = j + (256 + abyte0[i + 1]) * 256

    if abyte0[i + 2] >= 0:
        j = j+ abyte0[i + 2] * 256 * 256
        #print "A " + str(j)
    else:
        j = j + (256 + abyte0[i + 2]) * 256 * 256

    if abyte0[i + 3] >= 0:
        j = j + abyte0[i + 3] * 256 * 256 * 256
        #print "A " + str(j)
    else:
        j = j + (256 + abyte0[i + 3]) * 256 * 256 * 256

    return j

def StrLen(abyte0):
    for i in len(abyte0):
        if abyte0[i] == 0:
            return i

def StrLen(abyte0, i):
    for j in len(abyte0):
        if abyte0[i] == 0:
            return i
        else:
            i = i + 1

def Connect(s):
    out_header = array('B', [32, 0, 0, 0, 205, 0, 0, 0])
    data = array('B', [0, 0, 0, 0, \
                       5, 0, 0, 0, \
                       0, 0, 0, 0, \
                       0, 0, 0, 0, \
                       0, 0, 0, 0, \
                       0, 0, 0, 0, \
                       0, 0, 0, 0  \
                       ])

    #print 'sending data: '
    #print out_header.tostring()
    #print out_header.buffer_info()
    #print struct.unpack('BBBBBBBBB', out_header)
    s.send(out_header)
    s.send(data)

def ReadHeader():
    global DATA_SIZE
    global DATA_TYPE

    DATA_SIZE = 32

    # Read the reply header
    data = s.recv(HEADER_SIZE)
    data_string = array('B', data)

    i = ToInt(data_string.tolist(), 0)

    # First byte tells how big the data wil be
    if i > 4 and (i - 4) > DATA_SIZE:
        DATA_SIZE = i - 4

    print "DATA_SIZE is " + str(DATA_SIZE)

    # Second byte tells what the data is
    DATA_TYPE = data_string[4]

    #if DATA_TYPE == 1:
    #    print "Dunno"
    #elif DATA_TYPE == 106:
    #    print "MESSAGE"
    #elif DATA_TYPE == 207:
    #    print "IMAGE"
    #elif DATA_TYPE == 0:
    #    print "FALSE"
    #else:
    #    print "ERROR"

def ReadData(i):
    global DATA_SIZE

    #if DATA_SIZE > sys.maxint:
    #    DATA_SIZE = sys.maxint

    data = s.recv(DATA_SIZE)

    data_string = array('B', data)
    #afile = open("Dataz.txt", 'w')
    #data_string.tofile(afile)
    #print data_string

    abyte0 = data_string.tolist()

    #draw image
    image_w = ToInt(abyte0, 0)
    image_h = ToInt(abyte0, 4)

    # should be around 9k
    image_data_length = ToInt(abyte0, 72)

    datas = data_string[76:]

    file1 = open("Dataz.jpg", 'wb')
    datas.tofile(file1)
    file1.close()
    #print "Length of image data is: " + str(len(datas))

    #file = StringIO.StringIO(datas)
    #file1 = open("Dataz1.jpg", 'rb')
    #image = Image.open(file1)
    #image.show()

#Global variables
TCP_IP = '192.168.1.106'
TCP_PORT = 17860    
HEADER_SIZE = 8
DATA_SIZE = 32

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((TCP_IP, TCP_PORT))

#Starts channel 1
Connect(s)

ReadHeader()

#first data contains the channel name and status
data = s.recv(DATA_SIZE)
data_string = array('B', data)

# Second data should be first image
ReadHeader()
ReadData(0)

for i in range(1, 20):
    ReadHeader()
    ReadData(i)

s.close()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T07:39:41+00:00Added an answer on May 13, 2026 at 7:39 am

    If you pass socket.recv() a value larger than 2147483647 (hex 0x7fffffff) Python will attempt to use a long integer instead of a regular integer. Clearly your value for DATA_SIZE is considered to be larger than that.

    The ToInt() code appears to be trying to build up an integer out of individual bytes, and likely you aren’t doing the conversion properly (specifically the part where you try to handle negative values). Generally when converting code from a C-like language, and I’ll include Java in that set here, you want to find a higher-level way to express the code than working with individual bytes and characters.

    Try using the standard library struct module to do the conversion instead of working with bytes. Making sure you specify the proper byte order (big/little endian) to get correct results. For example, assuming your data is a 4-character string where the first character represents the smallest value, this would probably do:

    >>> import struct
    >>> i = struct.unpack('<L', '\x23\x45\x00\x73')[0]
    >>> print i
    1929397539
    

    You can interpret and modify that by checking the documentation referenced above.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 344k
  • Answers 344k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I don't think it's anything to do with GLUT -… May 14, 2026 at 5:39 am
  • Editorial Team
    Editorial Team added an answer got it! The method should be as suggested by rake… May 14, 2026 at 5:39 am
  • Editorial Team
    Editorial Team added an answer Instead of creating the DataContext whenever the method is called,… May 14, 2026 at 5:39 am

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I want use html5's new tag to play a wav file (currently only supported
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I've got a string that has curly quotes in it. I'd like to replace
In order to apply a triggered animation to all ToolTip s in my app,

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.