From fd4d32f68206d3f5477228cb5333c3430bdbd34a Mon Sep 17 00:00:00 2001 From: jetstream0 <49297268+jetstream0@users.noreply.github.com> Date: Sat, 16 Sep 2023 00:13:36 -0400 Subject: [PATCH] new posts --- posts/_metadata.json | 16 +++++++++ posts/hex_to_bytes_and_back.md | 61 ++++++++++++++++++++++++++++++++++ posts/llm.md | 11 ++++++ posts/wikipedia_rabbitholes.md | 32 ++++++++++++++++-- 4 files changed, 118 insertions(+), 2 deletions(-) create mode 100644 posts/hex_to_bytes_and_back.md create mode 100644 posts/llm.md diff --git a/posts/_metadata.json b/posts/_metadata.json index e26940c..a3a9d3c 100644 --- a/posts/_metadata.json +++ b/posts/_metadata.json @@ -7,6 +7,22 @@ "author": "jetstream0/Prussia", "tags": ["meta", "code", "project", "web", "markdown", "typescript_javascript", "css"] }, + "llm": { + "title": "LLM", + "slug": "llm", + "filename": "llm", + "date": "16/09/2023", + "author": "jetstream0/Prussia", + "tags": ["opinion"] + }, + "hex-to-bytes-and-back": { + "title": "Hex to Bytes and Back", + "slug": "hex-to-bytes-and-back", + "filename": "hex_to_bytes_and_back", + "date": "15/09/2023", + "author": "jetstream0/Prussia", + "tags": ["typescript_javascript", "code", "math"] + }, "rss-feed": { "title": "RSS!", "slug": "rss-feed", diff --git a/posts/hex_to_bytes_and_back.md b/posts/hex_to_bytes_and_back.md new file mode 100644 index 0000000..aab9982 --- /dev/null +++ b/posts/hex_to_bytes_and_back.md @@ -0,0 +1,61 @@ +I've written a lot of Javascript over the past few years. How much? I'm not sure, but 100 thousand lines is probably a good estimate^\[0\]^^\[1\]^. The two functions that I've written the most, over and over, are no doubt the function to turn hexadecimals to bytes, and vice versa. + +Part of this is because I do a lot of stuff related to cryptography and cryptocurrency (which, no surprise, is basically just *more* cryptography), which involves tons of work with bytes and often, converting them to hex for storage or display. The other part is because Javascript doesn't have a builtin way to convert hex to bytes or the other way around (Node.js apparently has `Buffer.from` but I never use that), and also because I just like writing things from scratch, which you may notice is a common theme in this blog. In addition to my trademark unnecessarily long sentences, of course. + +## Bytes + +I assume you already know what bytes and hexadecimals are, but in case you don't, here's a brief overview. + +Bits have two states. Bytes are made out of eight bits, so one byte can have 256 (`2^8=256`) states. + +Now, there are a couple ways you can represent bytes. One way could be representing them in binary, with 1s and 0s. Another would be just using our normal decimal (base 10 numbers), where a byte could be represented by a number from 0 to 255. But the best way (in my opinion) is to use hexadecimals (base 16 numbers) which uses the digits 0-9 and A-F. `A` represents 10, `B` represents 11, and so on. `FF` would represent 255 in decimal (`15*16+15=255`), `10` would represent 16 (`1*16+0`), and `32` would represent 50 (`3*16+2=50`). + +Why base 16? If you remember, one byte can have 256 states, meaning that two hexadecimal digits can perfectly represent one byte (`16^2=256`) which is a lot more elegant than decimal, and a lot more concise than binary. With decimal, it isn't exactly clear how many bytes 2402655566 is, while it is very clear how many bytes 8F359D4E is (8 hex digits, so 4 bytes). + +## Uint8Array + +Anyways, back on topic. In Javascript, bytes are often represented by [Uint8Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array), which are shockingly an array of Uint8s. What are Uint8s? Uint means "unsigned integer", or basically a non-negative whole number. The 8 stands for the 8 bits, so a Uint8 is an array of one byte unsigned integers^\[2\]^. Basically, it's a way to represent bytes in Javascript by storing in as an array of decimal numbers from 0-255. + +## Converting Bytes to Hexadecimal + +```js +function uint8_to_hex(uint8) { + const hex_chars = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F']; + let hex = ""; + for (let i=0; i < uint8.length; i++) { + hex += hex_chars[Math.floor(uint8[i]/16)]; + hex += hex_chars[uint8[i] % 16]; + } + return hex; +} +``` + +The loop iterates through through the `Uint8Array`, first dividing it by 16 and rounding down, to find the first hex character. Then, it divides by 16 and takes the remainder for the second hex character. + +## Converting Hexadecimal to Bytes + +```js +function hex_to_uint8(hex) { + const hex_chars = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F']; + hex = hex.toUpperCase(); + let uint8 = new Uint8Array(Math.floor(hex.length/2)); + for (let i=0; i < Math.floor(hex.length/2); i++) { + uint8[i] = hex_chars.indexOf(hex[i*2])*16; + uint8[i] += hex_chars.indexOf(hex[i*2+1]); + } + return uint8; +} +``` + +Here, we determine how many whole bytes^\[4\]^ are in the hex string, by diving it by two. We then loop that many times, each time convering the two hex characters into a number by finding the value of the first hex character (`indexOf`), multiplying it by 16, then finding the value of the second of the second hex character, and adding it. + +By the way, doing, for example, `new Uint8Array(5)`, will initialize an `Uint8Array` of all 0s, of length 5. + +This function, as written, isn't designed to take in invalid input, so make sure to valid any inputs. In fact, I would encourage you to go and write your own conversion functions, instead of copy pasting these examples. You'll (hopefully) understand the concepts much faster that way. + +=== +- \[0\]: ±50 thousand lines (estimating skills are not my strong suit). +- \[1\]: [my 6000 lines of unfinished code in one horrific file](https://github.com/jetstream0/Muskets-and-Bayonets/blob/main/script.js). +- \[2\]: Signed integers have "signs", ie, they can represent negative numbers. +- \[3\]: In case you were wondering, I do write the `hex_chars` array out every time... slightly painful, but it's too much work to copy paste it from somewhere +- \[4\]: Note that the `Math.floor` means that this function only works with an even hex string length, since an odd hex string length would mean there's half of a byte (aka a nybble) being used, which is rare-ish. diff --git a/posts/llm.md b/posts/llm.md new file mode 100644 index 0000000..1bf8471 --- /dev/null +++ b/posts/llm.md @@ -0,0 +1,11 @@ +"LLMs can write all your cod-" +Don't care. + +"LLMs can supercharge your produc-" +Don't care. + +"We can force LLMs to return valid JSON by not letting them use invalid tokens!" +Fine, that's pretty cool. + +"If you pay for this subscription..." +Please leave. diff --git a/posts/wikipedia_rabbitholes.md b/posts/wikipedia_rabbitholes.md index 2dbd0d7..d87327f 100644 --- a/posts/wikipedia_rabbitholes.md +++ b/posts/wikipedia_rabbitholes.md @@ -18,7 +18,7 @@ Here's a very incomplete (and maybe actively updated) list of ones that led to m - [Diffie-Hellman key exchange](https://en.wikipedia.org/wiki/Diffie%E2%80%93Hellman_key_exchange) - [Xi'an Incident](https://en.wikipedia.org/wiki/Xi%27an_Incident), where [Chiang Kai-shek](https://en.wikipedia.org/wiki/Chiang_Kai-shek) (leader of the Nationalists) is kidnapped by his generals [Yang Hucheng](https://en.wikipedia.org/wiki/Yang_Hucheng) and [Chang Hsueh-liang](https://en.wikipedia.org/wiki/Chang_Hsueh-liang), and forced to cooperate with the Communists against the invading Japanese - [Abdullah Öcalan](https://en.wikipedia.org/wiki/Abdullah_%C3%96calan), imprisoned [PKK](https://en.wikipedia.org/wiki/Kurdistan_Workers%27_Party) leader -- [Oda Nobunaga](https://en.wikipedia.org/wiki/Oda_Nobunaga), Japanese warlord, who died because of the [Honnō-ji Incident](https://en.wikipedia.org/wiki/Honn%C5%8D-ji_Incident) +- [Oda Nobunaga](https://en.wikipedia.org/wiki/Oda_Nobunaga), extremely notable Japanese warlord, who was killed in the [Honnō-ji Incident](https://en.wikipedia.org/wiki/Honn%C5%8D-ji_Incident) - [Ishiyama Hongan-ji](https://en.wikipedia.org/wiki/Ishiyama_Hongan-ji), former [Jōdo Shinshū](https://en.wikipedia.org/wiki/J%C5%8Ddo_Shinsh%C5%AB) temple/fortress, was burned down and replaced by [Osaka Castle](https://en.wikipedia.org/wiki/Osaka_Castle), and the reason why the city of [Osaka](https://en.wikipedia.org/wiki/Osaka) exists - [Peninsular War](https://en.wikipedia.org/wiki/Peninsular_War), [Napoleon](https://en.wikipedia.org/wiki/Napoleon)'s invasion of Spain and Portugal - [Thomas Cochrane, 10th Earl of Dundonald](https://en.wikipedia.org/wiki/Thomas_Cochrane,_10th_Earl_of_Dundonald), successful British Navy officer accused of stock exchange fraud, later participating in [Liberating Expedition of Peru](https://en.wikipedia.org/wiki/Liberating_Expedition_of_Peru) from the Spanish @@ -85,7 +85,7 @@ Here's a very incomplete (and maybe actively updated) list of ones that led to m - [An Lushan Rebellion](https://en.wikipedia.org/wiki/An_Lushan_Rebellion), An Lushan rebels, greatly weakened the Tang Dynasty - [Bahmani Sultanate](https://en.wikipedia.org/wiki/Bahmani_Sultanate), South Indian empire - [Pagan Empire](https://en.wikipedia.org/wiki/Pagan_Kingdom), the first Burmese kingdom -- [warsaw Uprising](https://en.wikipedia.org/wiki/Warsaw_Uprising) +- [Warsaw Uprising](https://en.wikipedia.org/wiki/Warsaw_Uprising) - [Polish-Lithuanian Commonwealth](https://en.wikipedia.org/wiki/Polish%E2%80%93Lithuanian_Commonwealth) - [Atoll](https://en.wikipedia.org/wiki/Atoll) - [Chambre introuvable](https://en.wikipedia.org/wiki/Chambre_introuvable), ultra-royalists Chamber of Deputies elected after the Second Bourbon Restoration @@ -93,3 +93,31 @@ Here's a very incomplete (and maybe actively updated) list of ones that led to m - [Battle of Nagashino](https://en.wikipedia.org/wiki/Battle_of_Nagashino), where Takeda Katsuyori learns it is not a good idea to cavalry charge into gunfire - [Twenty-Four Generals of Takeda Shingen](https://en.wikipedia.org/wiki/Twenty-Four_Generals_of_Takeda_Shingen), related to the Battle of Nagashino above, but too interesting to leave out - [Battle of Bannockburn](https://en.wikipedia.org/wiki/Battle_of_Bannockburn), decisive Scottish victory in the First War of Scottish Independence +- [Three Gorges](https://en.wikipedia.org/wiki/Three_Gorges), are three gorges, in China +- [Annexation of Hyderabad](https://en.wikipedia.org/wiki/Annexation_of_Hyderabad) +- [Emperor Xuanzong of Tang (9th century)](https://en.wikipedia.org/wiki/Emperor_Xuanzong_of_Tang_%289th_century%29) +- [List of coups and coup attempts](https://en.wikipedia.org/wiki/List_of_coups_and_coup_attempts) +- [Lazarus Group](https://en.wikipedia.org/wiki/Lazarus_Group), North Korean hackers +- [Pasquale Paoli](https://en.wikipedia.org/wiki/Pasquale_Paoli), Corsican nationalist admired by Napoleon +- [Doge (title)](https://en.wikipedia.org/wiki/Doge_%28title%29), like a King, but elected +- [Frank Serpico](https://en.wikipedia.org/wiki/Frank_Serpico), New York Police Department whistleblower +- [Inner Mongolia Incident](https://en.wikipedia.org/wiki/Inner_Mongolia_incident), part of the Cultural Revolution +- [Nationalist Party of Puerto_Rico](https://en.wikipedia.org/wiki/Nationalist_Party_of_Puerto_Rico) +- [Rotating locomotion in living systems](https://en.wikipedia.org/wiki/Rotating_locomotion_in_living_systems) +- [Japanese dialects](https://en.wikipedia.org/wiki/Japanese_dialects) +- [Ihor Kolomoyskyi](https://en.wikipedia.org/wiki/Ihor_Kolomoyskyi), Ukrainian oligarch +- [Anglophone Crisis](https://en.wikipedia.org/wiki/Anglophone_Crisis), war in Cameroon due to tensions between English speakers and French speakers +- [Kivu conflict](https://en.wikipedia.org/wiki/Kivu_conflict) +- [History of Somalia](https://en.wikipedia.org/wiki/History_of_Somalia) +- [Migration Period](https://en.wikipedia.org/wiki/Migration_Period), which led to the fall of the Western Roman Empire +- [Hydrofoil](https://en.wikipedia.org/wiki/Hydrofoil) +- [General Sherman (tree)](https://en.wikipedia.org/wiki/General_Sherman_%28tree%29), very big and very old +- [Simón Bolívar](https://en.wikipedia.org/wiki/Sim%C3%B3n_Bol%C3%ADvar), South American revolutionary hero +- [Battle of Dibrivka](https://en.wikipedia.org/wiki/Battle_of_Dibrivka) +- [Battle of the Teutoburg Forest](https://en.wikipedia.org/wiki/Battle_of_the_Teutoburg_Forest) +- [Intentional Community](https://en.wikipedia.org/wiki/Intentional_community) +- [Jellyfish](https://en.wikipedia.org/wiki/Jellyfish) are apparently "the informal common names given to the medusa-phase of certain gelatinous members of the subphylum Medusozoa" +- [Sinecure](https://en.wikipedia.org/wiki/Sinecure), get paid to do nothing +- [Yelü Dashi](https://en.wikipedia.org/wiki/Yel%C3%BC_Dashi), founder of the Western Liao dynasty +- [List of Unicode characters](https://en.wikipedia.org/wiki/List_of_Unicode_characters) +- [Operation Cyclone](https://en.m.wikipedia.org/wiki/Operation_Cyclone), where the CIA funds Islamist groups to fight against the communist Afghan government