Skip to content

Instantly share code, notes, and snippets.

@zhangzhhz
zhangzhhz / create_data_file_filled_with_0x00.md
Last active November 25, 2023 15:33
create a binary file of 50MB size, all 0x00

python

with open("50M.dat", "wb") as outfile:
    outfile.write(b'\x00' * 50 * 1024 * 1024)
    # outfile.write(bytes(50 * 1024 * 1024))

go

package main
@zhangzhhz
zhangzhhz / dump_table.lua
Last active February 11, 2023 16:39
Dump a Lua table
#!/usr/bin/env lua
t = { a = 1, b = 2, h = "hello", x = { m = 1, n = "hello world" }, 3,
y = { "y", z = "hello there", aa = { a1 = 1, a2 = 2, x = "xyz" } },
bool = { b1 = true, b2 = false, [true] = true, [false] = false },
[100] = "one hundred", ["hello world"] = 100, f = function() return "Returned from a function" end }
function dump(t, indentLevel)
local indentLevel = indentLevel or 1
local str = "{\n"
@zhangzhhz
zhangzhhz / print_chars_randomly_2.py
Last active July 9, 2022 19:21
Print all English alphabets randomly but in order on one line
#!/usr/bin/env python3
from string import ascii_lowercase, ascii_uppercase
from random import shuffle
from time import sleep
for letters in [ascii_lowercase, ascii_uppercase]:
l = list(range(26))
shuffle(l)
c_list = [" "] * 26
@zhangzhhz
zhangzhhz / print_chars_randomly_1.py
Last active July 9, 2022 19:19
Print all English alphabets in order but at random positions on one line
#!/usr/bin/env python3
from string import ascii_lowercase, ascii_uppercase
from random import shuffle
from time import sleep
for letters in [ascii_lowercase, ascii_uppercase]:
l = list(range(26))
shuffle(l)
c_list = [" "] * 26
@zhangzhhz
zhangzhhz / caret-and-tilde-in-git.md
Last active April 10, 2022 04:12
caret (^) and tilde (~) in Git

Both ^ and ~ themselves mean the parent.

~ followed by a number works the same as putting the same number of ~. For example, my_commit~3 is the same as my_commit~~~ which refers to the 3rd parent of my_commit.

But ^2 only makes sense for a merge commit, i.e., merge_commit^2 means the second parent of the merge_commit, merge_commit^1 is the same as merge_commit^ or merge_commit~ or merge_commit~1.

On the other hand, repetitive ^ is the same as repetitive ~. For example, ~~~ is the same as ^^^, and the same as ~3.

Some examples:

@zhangzhhz
zhangzhhz / base64-image.md
Created March 25, 2022 16:54
Encoding an image using base64 and decoding it

NodeJS

// base64 encoding a file
const base64EncodedString = fs.readFileSync(imageFileName).toString('base64');
// saving a base64 encoded image to a file
fs.writeFileSync(imageFileName, Buffer.from(base64EncodedString, 'base64'))

Python

Notes from Practical Git for Everyday Professional Use on egghed.io.

git init

Initialize the current local directoy as a git repository locally.

git init --bare will make the local directory a bare repository which will act as a remote respository.

git clone <link>

Clone a remote repository locally. New directory will be created.

#!/usr/bin/env python3
'''
You have notes in 3 denominations: two, five and ten.
Write a function `change` which takes in one argument which is the money amount,
and return the combination of the 3 notes using minimum number of notes
that adds up to the given amount.
Return None if it is not able to add up to the amount.
Assume you have infinite supply of notes in all 3 denominations.
Examples:
1: the function should return None
@zhangzhhz
zhangzhhz / non-printable characters.MD
Last active October 8, 2020 07:52
Keep only printable characters in ASCII character set

https://en.wikipedia.org/wiki/ASCII#Printable_characters

Codes 20 to 7E, known as the printable characters, represent letters, digits, punctuation marks, and a few miscellaneous symbols. There are 95 printable characters in total.

Code 20, the "space" character, denotes the space between words, as produced by the space bar of a keyboard. Since the space character is considered an invisible graphic (rather than a control character) it is listed in the table below instead of in the previous section.

Code 7F corresponds to the non-printable "delete" (DEL) control character and is therefore omitted from this chart; it is covered in the previous section's chart. Earlier versions of ASCII used the up arrow instead of the caret (5E) and the left arrow instead of the underscore (5F).

Javascript:

@zhangzhhz
zhangzhhz / check_certs.py
Created August 31, 2020 11:18
Get issuer, subject and dates for all certs in a single cert file
#!/usr/bin/env python3
import os
import sys
import subprocess
import tempfile
f = 'mycerts.pem' if len(sys.argv) == 1 else sys.argv[1]
if os.path.exists(f) and os.path.isfile(f):