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 9013023
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T03:09:01+00:00 2026-06-16T03:09:01+00:00

I’m building an assembler for a Java-like bytecode. Essentially, it is a Python function

  • 0

I’m building an assembler for a Java-like bytecode. Essentially, it is a Python function which takes in a string, which was read from a text file containing opcodes, and writes the compiled binary data to a file. Here is an example of an input string string (from Example.txt):

class Example

Method add

meta
lva 4

code
0: ipush 1 0
1: ipush 2 2
2: iload 1
3: iload 2
4: iadd
5: istore 3
6: iload 3
7: ireturn

My task is to convert the Example.txt text file into it’s binary representation (machine code for my virtual machine). Each opcode (e.g. iadd) is exactly one byte long (i.e. a number in the range of 0 to 255). Some opcodes take in operands, which are one byte long too.

Saving only the method’s opcodes in a binary file is pretty easy. I’d parse each opcode into a number in the range of 0 to 255, and use array.append(op), and then array.tofile(handle).

However, I need to save not only the opcodes, but data, such as the name of the class, name of the method, etc.

How can I save both one-byte values and more complex data to a binary file?

I have envisioned something like this…

The file is divided into sections. The first section, for example, would be class Example. The next section is the one started by Method add.

Essentially, there are only two types of sections, the name section, which only contains class name, and the method section, which is then subsectioned into the meta and code sections.

Name sections start with 00 (in hex), and then need to present the name of the class (this is my problem). That 00 is essentially the hex representation of class. Method sections start with FF, and then they too, need to present their name in the next few bytes. Then, I could have 0F to mean “start of meta section” and F0 “start of code section” inside the primary Method section.

However, a problem remains. How do I name these sections? I had an idea of converting each character in e.g. “Example” to its hex representation, a single byte, and then saving those in the file after 00 or FF. This probably means that I will need some “end of section” symbol.

Is there a better/simpler/already implemented way of doing this?

  • 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-06-16T03:09:03+00:00Added an answer on June 16, 2026 at 3:09 am

    You are asking a pretty sprawling question about design as well as basic Python usage. I’m not sure you’ll get a good answer until you limit the question a bit.

    But let’s focus on the question of how to convert a string, e.g. “Example”, into an array of integers, so that you can append it to your array. Be sure you have a byte string; if you have a string of Unicode characters, be sure to encode it into an array of bytes first. (If you are using Python 2.x, your string is already an array of bytes.)

    out = [0]  # your output array
    
    s = "Example" # a byte string, not a Unicode string
    l = list(s)   # convert to array of ints
    
    out = out + l
    print out
    

    gives:

    [0, 'E', 'x', 'a', 'm', 'p', 'l', 'e']
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a text area in my form which accepts all possible characters from
I've got a string that has curly quotes in it. I'd like to replace
I would like to run a str_replace or preg_replace which looks for certain words
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
Does anyone know how can I replace this 2 symbol below from the string
I have a bunch of posts stored in text files formatted in yaml/textile (from
I would like to count the length of a string with PHP. The string
link Im having trouble converting the html entites into html characters, (&# 8217;) i

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.