Make a desktop binary conversion (CONS) gadget python learning diary 2.28~3.6

Posted by bapi on Sun, 06 Mar 2022 18:51:17 +0100

2022 3.6

0 Beginning Trash

Finally, the vegetable chicken's diary has been updated. This time it is a bit long. Teachers who want to see something useful can skip the catalog directly.
I haven't written a diary for a long time. Did the chicken give up? Not really. Only chicken and vegetable have a new understanding of their dishes. jpg, decided to start from the basics and follow the road of ctf step by step. The most recent learning is python, which will also be the learning content for a considerable period of time (of course, the sooner the better). This project is your first Python program. It has no special technical connotation, but the actual experience and light reading of doing the project are completely different, although simple, But there are also many benefits.

1 Origin and C Language Ancestor

In addition to the reason that you are learning python, and more importantly, the reason why you need a truce to preserve your passion for study or work, that is, laziness, is why this project (so to speak, literally cannot be done at your own expense) began. The vegetable chicken used to suffer from CONS (conversation of number system). There is nothing to say in the middle school's Austrian math class. After all, this is the exam, but when I get to the university, it's really a big loss to rely on draft paper (well, I admit, it's mostly a mistake), and when I start to learn C, I do some questions about binary conversion, which gives me the idea:
1. Make a program to help people do the heavy work of binary conversion
2, it's best to put it on your desktop, just like a calculator (you won't get this article if it has this capability)

This should also be a requirement for most programming and usage, so after a considerable amount of bitterness (believe me, even though that's C as a regular course and the chicken is passionate enough to write a program that's incomplete in functionality and complex in operation takes longer than the python version), there's a first version:



True, this cbp without exe is the first generation of cons. In order to meet the second standard, which is easy to open (and to be visually appealing), chickens even change the icon to make that shortcut look different. But even with the use of shortcuts (when it comes to shortcuts, I can honestly say that the speed at which codeblocks open actually makes this shortcut meaningless), the open program is not directly usable:

Yes, it's really hard to build and run, quit after it's finished with a couple of x's, and go through the intimate inquiry of artificial intelligence disability "save or not". And the function implementation is also a problem:

Yes, only binary and decimal are converted to each other, and (quite shamefully) because of technical problems, binary conversion decimal also calculates the length of numbers.
---
*Note: The interface here is a powershell because it is easy to check for errors and replace it when writing the python version. While the powershell is friendly in providing errors, some overconfident actions can make it unnecessary when using it. We'll then switch back to the console to make it easier for the program to use (it was an unforgettable experience at first to check for errors in the extreme screenshots of the console, and the power shell's error is still very positive).

As for the code, there are four file s in one main, with more than two hundred lines of code combined to convert binary to decimal, and the user experience is very general (although the font and color focus is as beautiful as possible, this "shortcut" directly returns it to its origin)

---
*dtb and btd each spent more than 80 lines of code, and now it really seems like a half-done implementation.

To sum up, this program was not used on a large scale after it was completed, and it didn't take place until the end of last month to tamp up python's content (the so-called day of winter vacation when you finished python, you just finished looking at grammar and forgot a lot of details), you saw bin(), oct(), hex() functions, and this idea came back. Do an exercise to use these easily forgotten functions (and of course, later find that these functions are not worth mentioning).

2 Build True Shortcuts

Another source of confidence in restarting this project is an in-depth understanding of non-graphical interface systems, as there are other ways to achieve the effect of a gadget shortcut on the desktop:

Yes, it's for laziness. Because I really miss the feeling of clicking the shutdown button directly (win11 is really inhuman in some places, but there's room for chickens to use it), I don't know where to find a way to use txt generation to execute it directly. The bat command is a simple way to create a new ordinary txt file and write the corresponding console commands in it (since you're on the desktop, pay attention to the path):

---
*This is the script to open a new version of python, compiled with Python 3, a little more troublesome is that the path was written or could not be opened at that time. Finally, it was found that it was because the input method in Chinese was used and then shift. This requirement is more demanding, whether it is letters or characters, it must be a half-corner English input method, or else it will look the same or something will go wrong. And it drives people crazy.

After writing the command in txt, select the All Files format as Save As, and then manually add the'.bat'format to the file name:

Save to your desktop and you'll have a real shortcut.
---
*In theory, there should also be a compiler for c in windows that can run the previous cbp program directly, but the author is obviously nagging here because he is lazy in Baidu (joking, next update must be added)

---
*Place it under goobers for easy identification. Why not use tools instead of goobers and watch Spider-Man: Parallel Universe by yourself\Smile and Cry. jpg

3 python - program body

For your reference, here we put the code directly, the details are recorded after, mainly for the chicken itself, uninterested masters can skip directly
---
* Since a vegetable chicken article that was too watery to read was read by thousands - no one appreciates it - the vegetable chicken realized that no matter how nutritious its diary was, or whether it had a hopeful baby coming in to watch it, just say one more sentence, In fact, it is still valuable for a better master to spray a few sentences under the vegetable chicken's diary (meaning: scold now! Big man is coming up soon). Regardless of tmd quality, vegetable chickens are willing to be drowned out if they can say some valuable comments. After all, vegetable chickens are completely tired of learning without guidance (better call groping). It's an inefficient and painful process. Every breakthrough is exciting, but it's just like the old man who closed his door to derive the triangle area formula. The last thing I found out after exhausting my retirement time is that the corresponding theorem is mentioned in my junior high school textbook (it's still a bit sad to say here, and I'm sorry for the readers who have read the vegetable chicken diary, because so many so-called articles really don't add up to a lot of ups).

'''
Binary conversion applet, we want to compare C Language writing is easier and more practical
'''

#Setup of initial picture
_break = "\t------"

print()
print()
print()

print("\t0100100      101000     10    01")
print("\t11    01    10    11    10    11")
print("\t10    11    110   10    11    01")
print("\t0001100     01 0  00    10001101")
print("\t1111011     10  1 00    00101001")
print("\t01    11    11   011    01    00")
print("\t10    01    00    00    01    10")
print("\t1000001      001101     00    10" "\t@Ferrero")

print("")
print(_break * 4)

print("Program for conversation of number system. Boh is ready.")
print(_break * 4)
print("TIPS")
print(">>>V to view history data.")
print("")

'''
Macro-like settings area
'''

#file
fstream1 = open("d:\python_projects\CONS\data.txt", mode = "w")


#variables
judge_op = 'y'
o_ption1 = None
o_ption2 = None
num = None


'''
main()
'''
print(_break * 4)
print("INPUT MODE(decimalism default):  B/b:Bin  O/o:Oct  H/h:Hex")
o_ption1 = input(">>>Choose the mode:")
print()
print("OUTPUT MODE(decimalism default):  B/b:Bin  O/o:Oct  H/h:Hex")
o_ption2 = input(">>>Choose the mode:")
print()


#store the data
print("option1:", file = fstream1)
print(o_ption1, file = fstream1)
print("option2:", file = fstream1)
print(o_ption1, file = fstream1)


#judge the mode
while judge_op == 'y':
	if len(o_ption1) == 0:
		print("INPUT")
		num = int(input(">>>Input the number:"))

		if len(o_ption2) == 0:
			num = num
			print(num)

		if o_ption2 == 'b' or o_ption2 == 'B':
			num = bin(num)
			print("__1...5...9...13..17..21..25..29..33..")
			print(num)

		if o_ption2 == 'o' or o_ption2 == 'O':
			num = oct(num)
			print(num)

		if o_ption2 == 'h' or o_ption2 == 'O':
			num = hex(num)
			print(num)

	if o_ption1 == 'b' or o_ption1 == 'B':
		print("INPUT")
		print("                    1...5...9...13..17..21..25..29..33..")
		num = input(">>>Input the number:")

		#transfer into bin
		numsum = 0
		numsum = int(numsum)

		numing = list(num)
		#print(numing)
		numlen = len(num)
		#print(numlen)
		_index = 0
		_index = int(_index)

		while int(_index) < numlen:
			numsum = int(numsum) + int(num[int(numlen)- 1 - _index]) * (2 ** _index)
			_index = int(_index) + 1

		#print(numsum)
		num = numsum

		if o_ption2 == 'b' or o_ption2 == 'B':
			num = bin(num)
			print("__1...5...9...13..17..21..25..29..33..")
			print(num)

		if o_ption2 == 'o' or o_ption2 == 'O':
			num = oct(num)
			print(num)

		if len(o_ption2) == 0:
			num = int(num)
			print(num)

		if o_ption2 == 'h' or o_ption2 == 'O':
			num = hex(num)
			print(num)

	if o_ption1 == 'o' or o_ption1 == 'O':
		print("INPUT")
		num = input(">>>Input the number:")

		#transfer into oct
		numsum = 0
		numsum = int(numsum)

		numing = list(num)
		#print(numing)
		numlen = len(num)
		#print(numlen)
		_index = 0
		_index = int(_index)

		while int(_index) < numlen:
			numsum = int(numsum) + int(num[numlen - 1 - _index]) * (8 ** _index)
			_index = int(_index) + 1

		#print(numsum)
		num = numsum

		if o_ption2 == 'b' or o_ption2 == 'B':
			num = bin(num)
			print("__1...5...9...13..17..21..25..29..33..")
			print(num)

		if o_ption2 == 'o' or o_ption2 == 'O':
			num = oct(num)
			print(num)

		if len(o_ption2) == 0:
			num = int(num)
			print(num)

		if o_ption2 == 'h' or o_ption2 == 'O':
			num = hex(num)
			print(num)

	if o_ption1 == 'h' or o_ption1 == 'H':
		print("INPUT")
		num = input(">>>Input the number:")

		#transfer into hex
		numsum = 0
		numsum = int(numsum)

		numing = list(num)
		#print(numing)
		numlen = len(num)
		#print(numlen)
		_index = 0
		_index = int(_index)

		while int(_index) < numlen:
			if num[numlen - 1 - _index] == 'a' or num[numlen - 1 - _index] == 'A':
				numsum = int(numsum) + 10 * (16 ** _index)
			if num[numlen - 1 - _index] == 'b' or num[numlen - 1 - _index] == 'B':
				numsum = int(numsum) + 11 * (16 ** _index)
			if num[numlen - 1 - _index] == 'c' or num[numlen - 1 - _index] == 'C':
				numsum = int(numsum) + 12 * (16 ** _index)
			if num[numlen - 1 - _index] == 'd' or num[numlen - 1 - _index] == 'D':
				numsum = int(numsum) + 13 * (16 ** _index)
			if num[numlen - 1 - _index] == 'e' or num[numlen - 1 - _index] == 'E':
				numsum = int(numsum) + 14 * (16 ** _index)
			if num[numlen - 1 - _index] == 'f' or num[numlen - 1 - _index] == 'F':
				numsum = int(numsum) + 15 * (16 ** _index)
			if num[numlen - 1 - _index] == '0':
				numsum = int(numsum) + int(num[numlen - 1 - _index]) * (16 ** _index)
			if num[numlen - 1 - _index] == '1':
				numsum = int(numsum) + int(num[numlen - 1 - _index]) * (16 ** _index)
			if num[numlen - 1 - _index] == '2':
				numsum = int(numsum) + int(num[numlen - 1 - _index]) * (16 ** _index)
			if num[numlen - 1 - _index] == '3':
				numsum = int(numsum) + int(num[numlen - 1 - _index]) * (16 ** _index)
			if num[numlen - 1 - _index] == '4':
				numsum = int(numsum) + int(num[numlen - 1 - _index]) * (16 ** _index)
			if num[numlen - 1 - _index] == '5':
				numsum = int(numsum) + int(num[numlen - 1 - _index]) * (16 ** _index)
			if num[numlen - 1 - _index] == '6':
				numsum = int(numsum) + int(num[numlen - 1 - _index]) * (16 ** _index)
			if num[numlen - 1 - _index] == '7':
				numsum = int(numsum) + int(num[numlen - 1 - _index]) * (16 ** _index)
			if num[numlen - 1 - _index] == '8':
				numsum = int(numsum) + int(num[numlen - 1 - _index]) * (16 ** _index)
			if num[numlen - 1 - _index] == '9':
				numsum = int(numsum) + int(num[numlen - 1 - _index]) * (16 ** _index)

			_index = int(_index) + 1

		#print(numsum)
		num = numsum

		if o_ption2 == 'b' or o_ption2 == 'B':
			num = bin(num)
			print("__1...5...9...13..17..21..25..29..33..")
			print(num)

		if o_ption2 == 'o' or o_ption2 == 'O':
			num = oct(num)
			print(num)

		if len(o_ption2) == 0:
			num = int(num)
			print(num)

		if o_ption2 == 'h' or o_ption2 == 'O':
			num = hex(num)
			print(num)

The interface is like this when you use it:

More than two hundred pieces of code, which seems to be a little different from the previous version of c, but does not implement the same functions. From the above interface, you can see that this version has a name and the initial interface logo, which is "boh". The source is also simple, bin, oct, hex, But one of the great things about it is that Italian boh is a very versatile word (which is exactly what it looks like in Heroes Expedition, where Mandarin chickens don't speak well enough to understand small languages), which fits this version of the design concept, after all, compared to the last two combinations. This time there are 4*4=16 choices (yes, add in all these seemingly useless functions). Moreover, in combination with practical application, the program design here is more practical, and there is no limit to the progress of the C language version. After all, a small tool is limited in scale and does not need to be frequently transformed in different application scenarios: choose the in and out process at the beginning, and then simply type in continuously.

Next, I'll take a look at the details and record the chicken's tears and blood which are mistaken again and again. (T o T)


This is the design part of the initial picture effect, there's nothing to say. There has been no getshell's vegetable chicken self-esteem at all, which makes such a luxurious gimmick, but there are two things worth noting:
1, python's print is much more flexible and functional than C's printf, and the corresponding function format needs to be refreshed frequently.
2. The notes here are all in Chinese, but it's not a good habit. sumlime text is fairly inclusive, but many programs (especially paths) can't recognize Chinese characters (which should also be the result of unicode being less popular than ascii codes). And relying too much on Chinese characters is actually not good for learning (pwbtools is a good example, let alone a pigeon there).


open? Yes, there's one other thing the c version doesn't have at all: a record of historical data:

Just inside a txt. The idea of this function is to just see the open function and practice it. It is also of practical significance that sometimes the program closes directly after it has been used up, but after a while you find that you still need the results, you will have to restart the program and do it again (especially in the previous version of c, don't be too nervous). Even a simple row of data in the txt can save you a lot of trouble.
At present, this function only makes the degree of record conversion relationship, and later updates will complete it, and it can be seen that this function is not intended to be a degree that can be traced back 100 years, because of the simplicity of the new version, only the last record will be kept.


Here's a headache in python's beginnings: the data types in python. Most of the errors in this project are related to this. At first, Python seemed flexible and nice in defining variables, but once a type error appeared, it started to be less fragrant. Summary:
Use int more, initialize several key variables more, the caution is to learn C language; Also, bin/oct/hex does not have this capability, and don't dream of directly recognizing binary inputs (at least I don't see results after I ask my bride and city brain (c.s.d.n), so I have to follow up with some pretty silly algorithms, especially the hexadecimal part).


Then there are some little lessons about this cycle and judgment. To learn while well, there is still a big difference between for in python and for in C. The importance of while increases (perhaps because it is closer to chicken). Then there's the beauty of this len. It's not a vegetable chicken boasting about itself. This method was invented by a kind brother. Combining with the front console interface, you can see that vegetable chicken wants to achieve the default decimal function, but when a variable turns into something after input returns directly, it tries None anyway. Spaces and so on after no results (line breaks are more self-contradictory), and finally found that at least you can determine his length is 0, which is a big problem to solve, really let chickens know, to be decent, really not so simple. (T o T)


---
*Human-friendly design can not be less, and the number of digits in binary digits is also one of the things vegetable chickens don't want to recall.


This is the other part of the binary conversion. What a wonderful thing to do if you could binary input directly (drool), but unfortunately, you have to do it yourself, and because it's not as rigorous as C, the picture in this part is not so ugly, as you can see, Here vegetable chickens are ready to use int s with grass and trees.
There is also the name of the variable, _ index and previous o_ It's not surprising that ptions appear. After a chicken looks at python's keyword list, it decides that adding an underscore to a named variable is really a good deal.


Octal places are roughly the same as binary, but by hexadecimal the style starts to get weird because of the abcdef. Again, because there is no direct hex input, the chicken has to do it by itself. At first, we just wanted ABCDEF to check one by one in the past, and 0~9 would use range judgment directly, but there was a problem: once ABCDEF was entered, it would make num str-type, and even int would be useless, error display could not be converted, even the chance of ascii code operation would not be given, and there would be no way to compare without int. So that's the only clumsy way to do it, The only benefit is that it looks spectacular (In fact, there is a practical advantage, that is, if you type letters and numbers with a wrong hand, it is just a space and will not make a mistake: the console disappears, the powershell is more confused, makes a mistake, but does not disappear and gives you the opportunity to continue, and then you restart a few more times, which is to choose the console for actual use One reason).

4 To be improved

Yes, this applet is almost complete, but even when compared with the older version, there are many drawbacks, especially the emphasis on color. Without this function, you will find the previous numbers annoying, and there are records of historical data, more indentations, and so on. There are also many functions that can be envisioned and added (and even some common tables can be included, such as hexadecimal and binary conversions), and the reason why the chicken is inefficient in English leads to the inefficient use of many resources (several grammatical errors T o T were found just after checking the program).

The first time I wrote something that was so long (and so lost in innovation), from No. 6 to No. 7 (not sleepy at all), it was also the first article that I wanted others to see and believed could be of some value. But whether it's python or pwn or ctf, vegetable chicken or vegetable chicken \laughing and crying. jpg, whether it is this project or later crawlers, excel, compression pack cracking, or even machine vision, but also learn to improve, learn to innovate.

Stand by with boh (I hope you don't dream about those incomplete jobs, send them. The picture is from Baidu. There's really no way to remove the watermarks. I wanted to put a panda head on it, but I still want to think about so much to do, just a little bit of it)

Topics: Python pwn