python3.9 'byte' data handling [closed] - python-3.x

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I'm working on a 6809 python processor emulator for educational purposes.
I have a short binary file on the hard drive. The data is raw assembler output for a Motorola MC6809 that has the following contents:
0000300000338cfd4f5f10eec9100012126ec90010adc900186e4c12124cb10255270339121232624f5cf1025527046e4c12126ec4ff00000000
Actual code data:
338cfd4f5f10eec9100012126ec90010adc900186e4c12124cb10255270339121232624f5cf1025527046e4c12126ec4
Using python3.9 how do I get this into a list mem[] as either:
mem[00,00,30,00,00,33,8c,fd,4f,5f,...]
I suspect since ascii characters are involved here quotes would be needed
or
mem[0x00,0x00,0x30,0x00,0x00,0x33,0x8c,0xfd,0x4f,0x5f,...]
all I can seem to do is get the data back as
mem[b'00',b'00',b'30',b'00',b'00',b'33'.b'8c',b'fe',b'4f',b'5f',...]
But I can't seem to typecast these byte values into anything usable.
I've tried a half a dozen methods, with some questionable/unusable results.

Your question implies that you have a file full of ASCII characters, but your self-answer contradicts that by converting single bytes into integers.
It turns out that bytes are already integers. All you need to do is convert the bytes that you read into a list. You could probably work with the bytes object directly, but who am I to judge.
with open("/home/me/Documents/Sources/procproject.BIN", "rb") as memin:
mem = list(memin.read())

thank you for your time and replies
struct is not really a straight up simple method, but I found a rather interesting and simple way to get a byte array into a hex integer array
==========
mem = []
memin = open("/home/me/Documents/Sources/procproject.BIN", "rb")
while True:
mp = bytes(memin.read(1))
if mp == b'':
break
mem.append(hex(int.from_bytes(mp, 'big'))) #this is the magic#
memin.close()
mem=mem[5:]
mem = mem[:-5]
==========
works like a charm

Related

MATLAB: Function not recognized by MATLAB [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 months ago.
Improve this question
I need to extract substring. I have found that unlike Python, it is not possible to use indices to extract a substring from a string variable. I have also found that MATLAB does not allow use of negative indices to access elements at end of a vector like Python does.
Looking into MATLAB documentation I have found this function:
https://uk.mathworks.com/help/stateflow/ref/substr.html
However, when I enter this into the MATLAB, I get this:
>> substr
Unrecognized function or variable 'substr'.
I am using MATLAB R2022a. However, this function is not recognized. What could be the reason for this?
Also, are strings in MATLAB closer to the tedious strings of C language and not like the convinient entity they are in Python?
The documentation of substr says:
The operator substr is supported only in Stateflow® charts that use C
as the action language.
so this is not applicable in your case.
It is possible to get substrings of a string using indices. For example, you could use:
test_str = 'thisismyteststring'; % character array
test_str(9:12) % yields 'test'
test_str = "thisismyteststring"; % string array
extractBetween(test_str, 9, 12) % yields "test"
For more information on string handling, check out the documentation here.

Haskell: extract integers from a string [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
Hello I have this String
"(33,18,109)"
How do I extract the integers from this string to apply functions on them ?
Thank you.
The most straight forward way is to use read "(33,18,109)" :: (Int,Int,Int), but this is not considered a good way since it does not handle failure (for example read "(21,3,not valid)" and (21,43) would result in error.
A better way is to use the readMaybe "(1,2,3) :: Maybe (Int,Int,Int)". The result uses a Maybe to represent possible failure:
import Text.Read
ans :: String -> Maybe (Int,Int,Int)
ans = readMaybe
ans "(12,53,29)" == Just (12,53,29)
ans "not a valid string" == Nothing
However, you might want to see how you ended up with a string in the first place. Is it because of performing IO operations (e.g. reading from a file)? If not, usually you don't have to directly deal with a string representation of something then operate on it (since this violates the layers of abstraction).
Also, there is a topic called Parser Combinator that deals with parsing in Haskell. But this is hugely irrelevant if your task is to only parse 3 numbers instead of more complicated things like parsing arithmetic expressions (e.g. 1+2*3).

How to use a float in Java Card? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I need your help: does anyone know how I could present a float in Java Card?
The floating point number I need is 0.9. I've heard that I need to use a float point or something like this but I am not really sure.
Just like most Java Card implementations do not feature a 32-bit integer, they do not contain floating point arithmetic either. This however even goes much deeper: the compiled byte code for floating point is not even supported. So in the end you will have to do this yourself or look for vendor support. Note that most smart card CPU cores won't do floating point either, so it would have to be emulated using integer arithmetic.
If you require arithmetic on real numbers for money calculations or similar then you'd best look into fixed-point arithmetic. One trick is to simply perform calculations where each value is multiplied with 100, i.e. do calculations using cents. So then 0.90 times 10 would become 90 times 10. Then - at the terminal - you can simply re-insert the comma.
If you want to do integer calculations (optional but usually not supported), check my X-mas special answer here ... probably a contender for most complex answer on SO when it comes to code. This way you can do 32 bit calculations which you may need to handle any kind of precision (of ~9 decimal digits instead of ~4 that you get with shorts).
If you only need to store a float then simply encode the floating point to bytes, e.g. using DataOutputStream and store the resulting bytes. Or encode a packed BCD and use a byte to represent where the comma needs to be.

want someone to do my homework for me [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
# Change this program to output each letter of the text entered on a separate line
# with space in front of each letter, increasing to the right for each letter.
# For example, 'Python' input should output:
# P
# y
# t
# h
# o
# n
text = input('Enter text: ')
for letter in text:
print(letter)
I already tried to look online for the solution, there are none.
This code is for homework but i cant figure it out help wold be appreciated
As you haven't told us what you have tried to do so far, or what you have tried to learn to figure it out, I'm not going to post the Python code. Instead, lets think about what the program should be doing. Your assignment statement gives a broad overview, but as a programmer you need to take this overview and turn it into a set of smaller instructions. These smaller steps do not have to be in code. They can be in whatever form you like, even plain english.
For functional analysis (which is what you are doing for this problem) start with the inputs and outputs then fill in the stuff in-between.
1) Input: a string
X) Output: multiple lines with a single character and whitespace
Now how do you want to get from 1 to X. You already have the code to loop through each letter and print it, but you are missing two things to get to your required output.
A) way to place a string on a new line
B) way to add whitespace to a string
I'll give you a couple of hints. A) is something that is extremely common in almost any programming language. In fact, it is done the exact same way is any language that you are likely to use. Another hint. Your final output will be a single string that spans multiple lines. Ask yourself how a word processor or text editor gives works with blank lines.
B is a little more tricky, as there are a couple of nifty Python tricks that makes it simpler to do than in other languages. I'm guessing that you already know what happens when you add two numbers together such as 3 + 5. But what happens when you add two strings together such as "foo" + "bar". Notice how the behavior for adding numbers with the + operator is completely different than the behavior is for adding strings together with the same operator? That difference in behavior applies to other common operators as well. Play around with the other three common mathematical operators on both string and numbers to see what happens

Generate set of characters from list of strings efficiently [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
How would I generate a set of characters from a list of strings efficiently?
Suppose I've got a list of strings, e.g.:
windows
linux-2.6
Then the resulting string should be:
-.26dilnosuwx
The character order should correspond to the character type (ascii/utf8).
Programming language does not matter. I'd prefer a scripting solution (i.e. bash, python etc.) though.
The easiest to understand way (for me) would be:
create an array of 256 Boolean values
for each character in the string
convert the character to its numerical representation (i.e. 'A' is 65, etc.)
set the corresponding value in the array to true
end for
// done scanning strings. Now output:
for i = 0 to 255
if array[i] is set
output character value i
If you're working with Unicode characters, then that array will have to be 65,536 Boolean values.
There are other ways to do this. For example you could use a bit array rather than Boolean values to save space. Or you could create a hash table or set in some languages, for example. But the above works, is easy to understand, and can be translated to pretty much any programming language.
My solution with python would be with a random sample
>>> import random
>>>
>>> my_string = "linux-2.6"
>>>
>>> my_set = random.sample(my_string,len(my_string))
>>>
>>> for i in my_set: print i,
2 i . u x l - 6 n

Resources