首页
实用工具
我的旅程
在线壁纸
更多
✒️ 问题反馈
📦 文章统计
🌍 国内镜像
🎬 次元视界
📒 流水账本
🎨 在线 PS
推荐
🕵️ 开源情报
🌆 图片压缩
🍭 资产清洗
💡 我的作品
👤 关于站长
⚔️ 次 元 剑
搜索
1
【工具分享】逆向工具箱 - 次元剑
89,538 阅读
2
【技术分享】PE文件结构分析 ( RVA转FOA )
7,350 阅读
3
【技术分享】NASM x86 Assembly Language
5,982 阅读
4
【技术分享】CK竞技之王游戏辅助制作
3,725 阅读
5
【每日随记】天涯明月刀无限飞修改思路
2,816 阅读
技术分享
CTF解题
英语笔记
数学笔记
网络通信
每日随记
攻防技术
工具分享
Search
标签搜索
Windows
Web安全
Python3
Linux
逆向工程
CTF
红队技术
人工智能
C/C++
黑客工具
Go
密码学
二进制安全
数学
漏洞挖掘
Android
eNSP
渗透测试
蓝队技术
黑客大会
发光的神
累计撰写
160
篇文章
累计收到
103
条评论
首页
栏目
技术分享
CTF解题
英语笔记
数学笔记
网络通信
每日随记
攻防技术
工具分享
页面
实用工具
我的旅程
在线壁纸
✒️ 问题反馈
📦 文章统计
🌍 国内镜像
🎬 次元视界
📒 流水账本
🎨 在线 PS
推荐
🕵️ 开源情报
🌆 图片压缩
🍭 资产清洗
💡 我的作品
👤 关于站长
⚔️ 次 元 剑
搜索到
29
篇与
的结果
2022-07-13
【技术分享】Python3 人脸检测 opencv + dlib实现 ( 第一课 )
简介dlib是一个很有名的库,有c++、Python的接口。使用dlib可以大大简化开发,比如人脸识别,特征点检测之类的工作都可以很轻松实现。同时也有很多基于dlib开发的应用和开源库,比如face_recogintion库(应用一个基于Python的开源人脸识别库,face_recognition)等等。参考代码import sys import dlib import cv2 detector = dlib.get_frontal_face_detector() #获取人脸分类器 # 传入的命令行参数 for f in sys.argv[1:]: # opencv 读取图片,并显示 img = cv2.imread(f, cv2.IMREAD_COLOR) # 摘自官方文档: # image is a numpy ndarray containing either an 8bit grayscale or RGB image. # opencv读入的图片默认是bgr格式,我们需要将其转换为rgb格式;都是numpy的ndarray类。 b, g, r = cv2.split(img) # 分离三个颜色通道 img2 = cv2.merge([r, g, b]) # 融合三个颜色通道生成新图片 dets = detector(img, 1) #使用detector进行人脸检测 dets为返回的结果 print("Number of faces detected: {}".format(len(dets))) # 打印识别到的人脸个数 # enumerate是一个Python的内置方法,用于遍历索引 # index是序号;face是dets中取出的dlib.rectangle类的对象,包含了人脸的区域等信息 # left()、top()、right()、bottom()都是dlib.rectangle类的方法,对应矩形四条边的位置 for index, face in enumerate(dets): print('face {}; left {}; top {}; right {}; bottom {}'.format(index, face.left(), face.top(), face.right(), face.bottom())) # 在图片中标注人脸,并显示 left = face.left() top = face.top() right = face.right() bottom = face.bottom() cv2.rectangle(img, (left, top), (right, bottom), (0, 255, 0), 3) cv2.namedWindow(f, cv2.WINDOW_AUTOSIZE) cv2.imshow(f, img) # 等待按键,随后退出,销毁窗口 k = cv2.waitKey(0) cv2.destroyAllWindows()官方示例 #!/usr/bin/python # The contents of this file are in the public domain. See LICENSE_FOR_EXAMPLE_PROGRAMS.txt # # This example program shows how to find frontal human faces in an image. In # particular, it shows how you can take a list of images from the command # line and display each on the screen with red boxes overlaid on each human # face. # # The examples/faces folder contains some jpg images of people. You can run # this program on them and see the detections by executing the # following command: # ./face_detector.py ../examples/faces/*.jpg # # This face detector is made using the now classic Histogram of Oriented # Gradients (HOG) feature combined with a linear classifier, an image # pyramid, and sliding window detection scheme. This type of object detector # is fairly general and capable of detecting many types of semi-rigid objects # in addition to human faces. Therefore, if you are interested in making # your own object detectors then read the train_object_detector.py example # program. # # # COMPILING/INSTALLING THE DLIB PYTHON INTERFACE # You can install dlib using the command: # pip install dlib # # Alternatively, if you want to compile dlib yourself then go into the dlib # root folder and run: # python setup.py install # or # python setup.py install --yes USE_AVX_INSTRUCTIONS # if you have a CPU that supports AVX instructions, since this makes some # things run faster. # # Compiling dlib should work on any operating system so long as you have # CMake and boost-python installed. On Ubuntu, this can be done easily by # running the command: # sudo apt-get install libboost-python-dev cmake # # Also note that this example requires scikit-image which can be installed # via the command: # pip install scikit-image # Or downloaded from http://scikit-image.org/download.html. import sys import dlib from skimage import io detector = dlib.get_frontal_face_detector() win = dlib.image_window() for f in sys.argv[1:]: print("Processing file: {}".format(f)) img = io.imread(f) # The 1 in the second argument indicates that we should upsample the image # 1 time. This will make everything bigger and allow us to detect more # faces. dets = detector(img, 1) print("Number of faces detected: {}".format(len(dets))) for i, d in enumerate(dets): print("Detection {}: Left: {} Top: {} Right: {} Bottom: {}".format( i, d.left(), d.top(), d.right(), d.bottom())) win.clear_overlay() win.set_image(img) win.add_overlay(dets) dlib.hit_enter_to_continue() # Finally, if you really want to you can ask the detector to tell you the score # for each detection. The score is bigger for more confident detections. # The third argument to run is an optional adjustment to the detection threshold, # where a negative value will return more detections and a positive value fewer. # Also, the idx tells you which of the face sub-detectors matched. This can be # used to broadly identify faces in different orientations. if (len(sys.argv[1:]) > 0): img = io.imread(sys.argv[1]) dets, scores, idx = detector.run(img, 1, -1) for i, d in enumerate(dets): print("Detection {}, score: {}, face_type:{}".format( d, scores[i], idx[i]))
2022年07月13日
107 阅读
0 评论
36 点赞
2022-07-12
【技术分享】Python3 模拟鼠标键盘
模块安装pip install pyautogui -i https://mirrors.aliyun.com/pypi/simple/找图坐标import pyautogui position = pyautogui.locateCenterOnScreen('1.png') pyautogui.moveTo(position[0], position[1],duration=0.2) print(position)基本操作import pyautogui pyautogui.FAILSAFE = True # 启用自动防故障功能,左上角的坐标为(0,0),将鼠标移到屏幕的左上角,来抛出failSafeException异常 x, y = 122, 244 pyautogui.onScreen(x, y) # 结果为true width, height = pyautogui.size() # 屏幕的宽度和高度 print(width, height)import pyautogui currentMouseX, currentMouseY = pyautogui.position() # 鼠标当前位置 print(currentMouseX, currentMouseY) # 控制鼠标移动,duration为持续时间 for i in range(2): pyautogui.moveTo(100, 100, duration=0.25) # 移动到 (100,100) pyautogui.moveTo(200, 100, duration=0.25) pyautogui.moveTo(200, 200, duration=0.25) pyautogui.moveTo(100, 200, duration=0.25) pyautogui.moveRel(50, 0, duration=0.25) # 从当前位置右移100像素 pyautogui.moveRel(0, 50, duration=0.25) # 向下 pyautogui.moveRel(-50, 0, duration=0.25) # 向左 pyautogui.moveRel(0, -50, duration=0.25) # 向上 # 按住鼠标左键,把鼠标拖拽到(100, 200)位置 pyautogui.dragTo(100, 200, button='left') # 按住鼠标左键,用2秒钟把鼠标拖拽到(300, 400)位置 pyautogui.dragTo(300, 400, 2, button='left') # 按住鼠标左键,用0.2秒钟把鼠标向上拖拽 pyautogui.dragRel(0, -60, duration=0.2) # pyautogui.click(x=moveToX, y=moveToY, clicks=num_of_clicks, interval=secs_between_clicks, button='left') # 其中,button属性可以设置成left,middle和right。 pyautogui.click(10, 20, 2, 0.25, button='left') pyautogui.click(x=100, y=200, duration=2) # 先移动到(100, 200)再单击 pyautogui.click() # 鼠标当前位置点击一下 pyautogui.doubleClick() # 鼠标当前位置左击两下 pyautogui.doubleClick(x=100, y=150, button="left") # 鼠标在(100,150)位置左击两下 pyautogui.tripleClick() # 鼠标当前位置左击三下 pyautogui.mouseDown() # 鼠标左键按下再松开 pyautogui.mouseUp() pyautogui.mouseDown(button='right') # 按下鼠标右键 pyautogui.mouseUp(button='right', x=100, y=200) # 移动到(100, 200)位置,然后松开鼠标右键 # scroll函数控制鼠标滚轮的滚动,amount_to_scroll参数表示滚动的格数。正数则页面向上滚动,负数则向下滚动 # pyautogui.scroll(clicks=amount_to_scroll, x=moveToX, y=moveToY) pyautogui.scroll(5, 20, 2) pyautogui.scroll(10) # 向上滚动10格 pyautogui.scroll(-10) # 向下滚动10格 pyautogui.scroll(10, x=100, y=100) # 移动到(100, 100)位置再向上滚动10格 # 缓动/渐变函数可以改变光标移动过程的速度和方向。通常鼠标是匀速直线运动,这就是线性缓动/渐变函数。 # PyAutoGUI有30种缓动/渐变函数,可以通过pyautogui.ease*?查看。 # 开始很慢,不断加速 pyautogui.moveTo(100, 100, 2, pyautogui.easeInQuad) # 开始很快,不断减速 pyautogui.moveTo(100, 100, 2, pyautogui.easeOutQuad) # 开始和结束都快,中间比较慢 pyautogui.moveTo(100, 100, 2, pyautogui.easeInOutQuad) # 一步一徘徊前进 pyautogui.moveTo(100, 100, 2, pyautogui.easeInBounce) # 徘徊幅度更大,甚至超过起点和终点 pyautogui.moveTo(100, 100, 2, pyautogui.easeInElastic)键盘操作import pyautogui pyautogui.typewrite('Hello world!') # 输入Hello world!字符串 pyautogui.typewrite('Hello world!', interval=0.25) # 每次输入间隔0.25秒,输入Hello world! pyautogui.press('enter') # 按下并松开(轻敲)回车键 pyautogui.press(['left', 'left', 'left', 'left']) # 按下并松开(轻敲)四下左方向键 pyautogui.keyDown('shift') # 按下`shift`键 pyautogui.keyUp('shift') # 松开`shift`键 pyautogui.keyDown('shift') pyautogui.press('4') pyautogui.keyUp('shift') # 输出 $ 符号的按键 pyautogui.hotkey('ctrl', 'v') # 组合按键(Ctrl+V),粘贴功能,按下并松开'ctrl'和'v'按键 # pyautogui.KEYBOARD_KEYS数组中就是press(),keyDown(),keyUp()和hotkey()函数可以输入的按键名称 pyautogui.KEYBOARD_KEYS = ['\t', '\n', '\r', ' ', '!', '"', '#', '$', '%', '&', "'", '(', ')', '*', '+', ',', '-', '.', '/', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ':', ';', '<', '=', '>', '?', '@', '[', '\\', ']', '^', '_', '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '{', '|', '}', '~', 'accept', 'add', 'alt', 'altleft', 'altright', 'apps', 'backspace', 'browserback', 'browserfavorites', 'browserforward', 'browserhome', 'browserrefresh', 'browsersearch', 'browserstop', 'capslock', 'clear', 'convert', 'ctrl', 'ctrlleft', 'ctrlright', 'decimal', 'del', 'delete', 'divide', 'down', 'end', 'enter', 'esc', 'escape', 'execute', 'f1', 'f10', 'f11', 'f12', 'f13', 'f14', 'f15', 'f16', 'f17', 'f18', 'f19', 'f2', 'f20', 'f21', 'f22', 'f23', 'f24', 'f3', 'f4', 'f5', 'f6', 'f7', 'f8', 'f9', 'final', 'fn', 'hanguel', 'hangul', 'hanja', 'help', 'home', 'insert', 'junja', 'kana', 'kanji', 'launchapp1', 'launchapp2', 'launchmail', 'launchmediaselect', 'left', 'modechange', 'multiply', 'nexttrack', 'nonconvert', 'num0', 'num1', 'num2', 'num3', 'num4', 'num5', 'num6', 'num7', 'num8', 'num9', 'numlock', 'pagedown', 'pageup', 'pause', 'pgdn', 'pgup', 'playpause', 'prevtrack', 'print', 'printscreen', 'prntscrn', 'prtsc', 'prtscr', 'return', 'right', 'scrolllock', 'select', 'separator', 'shift', 'shiftleft', 'shiftright', 'sleep', 'space', 'stop', 'subtract', 'tab', 'up', 'volumedown', 'volumemute', 'volumeup', 'win', 'winleft', 'winright', 'yen', 'command', 'option', 'optionleft', 'optionright'] 鼠标操作import pyautogui import time # 获取鼠标位置 def get_mouse_positon(): print('开始获取鼠标位置') try: for i in range(30): # Get and print the mouse coordinates. x, y = pyautogui.position() positionStr = '鼠标坐标点(X,Y)为:{},{}'.format(str(x).rjust(4), str(y).rjust(4)) pix = pyautogui.screenshot().getpixel((x, y)) # 获取鼠标所在屏幕点的RGB颜色 positionStr += ' RGB:(' + str(pix[0]).rjust(3) + ',' + str(pix[1]).rjust(3) + ',' + str(pix[2]).rjust( 3) + ')' print(positionStr) time.sleep(0.1) # 停顿时间 except: print('获取鼠标位置失败') if __name__ == "__main__": get_mouse_positon()弹窗操作import pyautogui # 显示一个简单的带文字和OK按钮的消息弹窗。用户点击后返回button的文字。 pyautogui.alert(text='', title='', button='OK') b = pyautogui.alert(text='要开始程序么?', title='请求框', button='OK') print(b) # 输出结果为OK # 显示一个简单的带文字、OK和Cancel按钮的消息弹窗,用户点击后返回被点击button的文字,支持自定义数字、文字的列表。 pyautogui.confirm(text='', title='', buttons=['OK', 'Cancel']) # OK和Cancel按钮的消息弹窗 pyautogui.confirm(text='', title='', buttons=range(10)) # 10个按键0-9的消息弹窗 a = pyautogui.confirm(text='', title='', buttons=range(10)) print(a) # 输出结果为你选的数字 # 可以输入的消息弹窗,带OK和Cancel按钮。用户点击OK按钮返回输入的文字,点击Cancel按钮返回None。 pyautogui.prompt(text='', title='', default='') # 样式同prompt(),用于输入密码,消息用*表示。带OK和Cancel按钮。用户点击OK按钮返回输入的文字,点击Cancel按钮返回None。 pyautogui.password(text='', title='', default='', mask='*')图像操作import pyautogui pyautogui.screenshot('1.png') # 截全屏并设置保存图片的位置和名称 im = pyautogui.screenshot('1.png') # 截全屏并设置保存图片的位置和名称 print(im) # 打印图片的属性 # 不截全屏,截取区域图片。截取区域region参数为:左上角XY坐标值、宽度和高度 pyautogui.screenshot(r'1.png', region=(0, 0, 300, 400)) pix = pyautogui.screenshot().getpixel((220, 200)) # 获取坐标(220,200)所在屏幕点的RGB颜色 positionStr = ' RGB:(' + str(pix[0]).rjust(3) + ',' + str(pix[1]).rjust(3) + ',' + str(pix[2]).rjust(3) + ')' print(positionStr) # 打印结果为RGB:( 60, 63, 65) pix = pyautogui.pixel(220, 200) # 获取坐标(220,200)所在屏幕点的RGB颜色与上面三行代码作用一样 # 如果你只是要检验一下指定位置的像素值,可以用pixelMatchesColor(x,y,RGB)函数,把X、Y和RGB元组值穿入即可 # 如果所在屏幕中(x,y)点的实际RGB三色与函数中的RGB一样就会返回True,否则返回False # tolerance参数可以指定红、绿、蓝3种颜色误差范围 pyautogui.pixelMatchesColor(100, 200, (255, 255, 255)) pyautogui.pixelMatchesColor(100, 200, (255, 255, 245), tolerance=10) # 获得文件图片在现在的屏幕上面的坐标,返回的是一个元组(top, left, width, height) # 如果截图没找到,pyautogui.locateOnScreen()函数返回None a = pyautogui.locateOnScreen(r'1.png') print(a) # 打印结果为Box(left=0, top=0, width=300, height=400) x, y = pyautogui.center(a) # 获得文件图片在现在的屏幕上面的中心坐标 print(x, y) # 打印结果为150 200 x, y = pyautogui.locateCenterOnScreen('1.png') # 这步与上面的四行代码作用一样 # 匹配屏幕所有与目标图片的对象,可以用for循环和list()输出 pyautogui.locateAllOnScreen(r'1.png') for pos in pyautogui.locateAllOnScreen(r'1.png'): print(pos) # 打印结果为Box(left=0, top=0, width=300, height=400) a = list(pyautogui.locateAllOnScreen('1.png')) print(a) # 打印结果为[Box(left=0, top=0, width=300, height=400)]{dotted startColor="#ff6c6c" endColor="#1989fa"/}
2022年07月12日
170 阅读
0 评论
39 点赞
2022-03-09
【数学笔记】编程中的数学小知识
简介编程里藏了好多数学小知识,别看这些知识点基础,吃透了能让代码跑得更快、逻辑更通透!我把平时写代码常用的二进制位运算、等差数列求和、进位原理这些整理出来,结合Python代码掰扯清楚,记下来复习用,全是大白话,不搞虚的,重点是“能看懂、能直接用”。一、二进制与位运算在我们写代码时偶尔会碰到位运算,一开始我也懵,后来发现这玩意儿就是直接操弄二进制的0和1,比普通加减乘除快多了!Python里就四种核心位运算,和布尔逻辑的与、或、异或、非对应,先把规则和用法记死,用到的时候直接套就行。位运算符号大白话通俗别名按位与&俩位都为1才是1,否则0AND按位或\只要有一个1就是1,否则0OR按位异或^俩位不一样才是1,一样就是0XOR按位取反~0变1、1变0(Python里要注意补码)NOT左位移<<二进制整体左移,右边补0,等价乘2ⁿ左移右位移>>二进制整体右移,左边补符号位,等价除2ⁿ右移1. 按位与(AND:&)说白了就是“严要求”,必须俩位都为1才给1,不然全是0。我亲测最常用的场景就是判断奇偶——任何数和1做按位与,结果为1就是奇数,为0就是偶数,比取模%快多了!# 二进制数 0b101(十进制5)和 0b111(十进制7)的 AND 运算 a = 0b101 # 十进制5,二进制就是101 b = 0b111 # 十进制7,二进制就是111 c = a & b # 逐位比:1&1=1,0&1=0,1&1=1 → 结果0b101(十进制5) print(f"二进制结果:{bin(c)},十进制结果:{c}") # 输出:0b101,5 # 实用场景:判断奇偶(亲测比num % 2快) def is_odd(num): return num & 1 == 1 # 结果为True就是奇数,False是偶数 print(f"15是奇数吗?{is_odd(15)}") # True print(f"20是奇数吗?{is_odd(20)}") # False2. 按位或(OR:|)这玩意儿是“松要求”,只要有一个位是1,结果就为1,特别适合给二进制的特定位赋值1,比如权限控制里合并多个权限标记。# 二进制数 0b101(十进制5)和 0b110(十进制6)的 OR 运算 a = 0b101 # 十进制5 b = 0b110 # 十进制6 c = a | b # 逐位比:1|1=1,0|1=1,1|0=1 → 结果0b111(十进制7) print(f"二进制结果:{bin(c)},十进制结果:{c}") # 输出:0b111,7 # 实用场景:给数字的二进制某位置1(比如给0b101的第二位设为1) num = 0b101 # 5 mask = 0b010 # 要设置的位 result = num | mask print(f"置1后:{bin(result)}") # 0b111,也就是73. 按位异或(XOR:^)这个是我最喜欢的!俩位不一样就给1,一样就给0,最牛的用法是不用临时变量交换两个数,代码贼简洁,面试也常考!# 二进制数 0b101(十进制5)和 0b110(十进制6)的 XOR 运算 a = 0b101 # 十进制5 b = 0b110 # 十进制6 c = a ^ b # 逐位比:1^1=0,0^1=1,1^0=1 → 结果0b011(十进制3) print(f"二进制结果:{bin(c)},十进制结果:{c}") # 输出:0b11,3 # 实用场景:交换两个数(不用temp变量) x = 10 y = 20 print(f"交换前:x={x},y={y}") x ^= y # 第一步:x = x^y = 10^20 y ^= x # 第二步:y = y^(x^y) = x x ^= y # 第三步:x = (x^y)^x = y print(f"交换后:x={x},y={y}") # x=20,y=10,完美交换!4. 按位取反(NOT:~)这个要注意坑!表面是按位取反,但Python里整数用补码存储,所以取反后不是单纯的0变1,而是 ~x = -(x + 1),记着这个公式就不会错了。# 对二进制数 0b101(十进制5)进行 NOT 运算 a = 0b101 # 十进制5,补码是...00000101 b = ~a # 取反后补码是...11111010 → 对应十进制-6,符合~x=-(x+1) print(f"二进制结果:{bin(b)},十进制结果:{b}") # 输出:-0b110,-6 # 验证公式:~x = -(x+1) print(f"~5 = {~5},-(5+1) = {-6}") # 两边相等,记死这个公式!5. 位移运算(<< / >>)位运算里位移也超常用,说白了就是“二进制搬家”,左移1位等于乘2,右移1位等于除2,比直接乘除快多了!# 左位移:0b101(5)左移1位 → 0b1010(10),等价5*2 num = 5 print(f"5左移1位:{num << 1}") # 10 print(f"5左移2位:{num << 2}") # 20(等价5*4) # 右位移:0b1010(10)右移1位 → 0b101(5),等价10/2 num = 10 print(f"10右移1位:{num >> 1}") # 5 print(f"10右移2位:{num >> 2}") # 2(等价10//4)二、等差数列求和写代码时经常要算连续数字的和,比如统计1到100的和、100到1000的和,要是用for循环累加太笨了!等差数列求和公式直接套,一秒出结果,效率拉满。核心公式基础版(公差=1,比如1,2,3...):项数 $n = a_n - a_1 + 1$($a_1$首项,$a_n$末项)和 $S = n \times (a_1 + a_n) // 2$通用版(公差≠1,比如1,3,5,7...):项数 $n = [(a_n - a_1) ÷ 公差d] + 1$和 $S = n \times [2a_1 + (n-1)d] // 2$Python 实现''' 自定义等差数列求和函数,支持公差≠1的情况 a1:首项,an:末项,d:公差(默认1) 返回值:数列的和 ''' def arithmetic_sum(a1, an, d=1): # 先校验参数,避免传错 if a1 > an or d <= 0: raise ValueError("首项不能大于末项,公差得是正数!") # 计算项数 n = (an - a1) // d + 1 # 通用求和公式 total = n * (2 * a1 + (n - 1) * d) // 2 return total # 测试1:100至1000的连续整数和(公差1) print(f"100到1000的和:{arithmetic_sum(100, 1000)}") # 输出495550 # 测试2:1,3,5,...,99的奇数和(公差2) print(f"1到99的奇数和:{arithmetic_sum(1, 99, 2)}") # 输出2500 # 测试3:2,4,6,...,100的偶数和(公差2) print(f"2到100的偶数和:{arithmetic_sum(2, 100, 2)}") # 输出2550为什么不用循环?我测过,算1到1000000的和,循环要跑0.1秒左右,用公式直接0.0001秒搞定,数据量越大,公式的优势越明显!记着:只要是“相邻数差值固定”的数列,都用这个公式,别傻乎乎写循环。三、进位原理咱平时用的+号,计算机底层其实是靠“进位”实现的!不管十进制还是二进制,核心都是“逢n进1”(十进制逢10进1,二进制逢2进1)。我写了个手动实现加法的函数,吃透这个逻辑,能搞懂计算机加法的底层。1. 十进制进位加法说白了就是“从个位开始加,满10进1”,我写的这个函数能模拟计算机的加法过程,哪怕是999+1这种要连续进位的情况也能搞定!''' 手动实现十进制加法,模拟进位逻辑 a、b:两个加数(整数) 返回值:两数之和 ''' def carry_add(a, b): # 把数字反转,方便从个位开始计算(比如123→"321") a_str = str(a)[::-1] b_str = str(b)[::-1] result = 0 # 最终结果 carry = 0 # 进位标记(0或1) max_len = max(len(a_str), len(b_str)) # 取最长位数,避免漏位 # 逐位计算 for i in range(max_len): # 取出当前位的数字,超出长度就补0 a_digit = int(a_str[i]) if i < len(a_str) else 0 b_digit = int(b_str[i]) if i < len(b_str) else 0 # 当前位总和 = 个位数字和 + 上一位的进位 digit_sum = a_digit + b_digit + carry # 判断是否进位 if digit_sum >= 10: carry = 1 # 满10进1 digit_sum -= 10 # 只保留个位 else: carry = 0 # 无进位 # 把当前位结果加到最终结果里(恢复位数) result += digit_sum * (10 ** i) # 最后还有进位的话,追加到最高位(比如999+1=1000) if carry == 1: result += 10 ** max_len return result # 测试案例,覆盖普通情况和连续进位 print(f"10 + 21 = {carry_add(10, 21)}") # 31 print(f"999 + 1 = {carry_add(999, 1)}") # 1000 print(f"1234 + 5678 = {carry_add(1234, 5678)}") # 69122. 二进制进位加法计算机底层用的是二进制加法,核心是“逢2进1”,和十进制逻辑一样,就是把10换成2而已。搞懂这个,再看位运算加法就通透了!''' 手动实现二进制加法,模拟计算机底层加法逻辑 a_bin、b_bin:二进制字符串(比如"101") 返回值:二进制和的字符串 ''' def binary_carry_add(a_bin, b_bin): # 反转二进制字符串,从最低位开始算 a_rev = a_bin[::-1] b_rev = b_bin[::-1] result = [] # 存储每一位的结果 carry = 0 # 进位标记(0或1) max_len = max(len(a_rev), len(b_rev)) for i in range(max_len): # 取出当前位,补0 a_bit = int(a_rev[i]) if i < len(a_rev) else 0 b_bit = int(b_rev[i]) if i < len(b_rev) else 0 # 当前位总和 = 位和 + 进位 bit_sum = a_bit + b_bit + carry # 逢2进1 if bit_sum >= 2: carry = 1 bit_sum -= 2 else: carry = 0 result.append(str(bit_sum)) # 最后有进位就加上 if carry == 1: result.append("1") # 反转回来,得到最终二进制字符串 return ''.join(result[::-1]) # 测试案例 print(f"101(5) + 110(6) = {binary_carry_add('101', '110')}") # 1011(11) print(f"111(7) + 1(1) = {binary_carry_add('111', '1')}") # 1000(8)总结位运算直接操作二进制,比普通运算快,重点记&判断奇偶、^交换变量、<<>>等价乘除2ⁿ;等差数列求和别用循环,套公式:基础版$S = n×(a1+an)//2$,通用版加个公差d就行;加法的核心是进位,十进制逢10进1,二进制逢2进1,手动实现一遍就能懂计算机加法的底层逻辑。
2022年03月09日
66 阅读
0 评论
8 点赞
2022-03-06
【每日随记】Python3 打包程序密码泄露
简介Python是解释型脚本语言,打包成可执行程序,通过运行中的进程内存,还是能看到一些东西的,会有一些字符串之类的,在没加密的情况下在内存中完全是可以看到的。实现步骤1.这里我用Python自带的tkinter库,写了一个有GUI的登录程序,没有进行加密处理,登录的账号和密码也是在程序代码里。2.用Cheat Engine查找下账号和密码,首先运行程序,选择下程序的进程。3.输入错误的账号和密码,绝对会报错提示,这个时候用Cheat Engine查找下关键字符就可以找到。4.结合源码观察,看看是不是在内存中很容易就找到账号密码啦。
2022年03月06日
57 阅读
0 评论
6 点赞
2016-03-01
【每日随记】软件逆向破解工具篇
简介说到破解肯定少不了Ollydbg还有IDA,制作外挂就用到Cheat engine,Ollydbg是动态调试IDA是静态分析也可以动态调试,动态就是软件可以被运行起来断点调试进行修改,静态是直接分析反汇编代码进行修改,当然od也是可以看反汇编的两者各有各的优缺点,IDA可以直接伪代码构造大概的伪C代码很方便。调试工具IDA Proradare2Github传送门hopperOllydbgx64dbgGithub传送门Open ARK工具箱Github传送门针对.NETdnSpyGithub传送门ILSpyGithub传送门针对 Pythondecompile3Github传送门针对 AndroidjadxGithub传送门
2016年03月01日
833 阅读
0 评论
45 点赞
1
2
3
0:00