Top Tweets for #webtok
I have a 256-bit quantity in Java and I want to encode it using Base64. Here's how I'm doing it from scratch.
The 256-bit information is stored in 4 long values in a class named 'WebToken'. I've sketched the following diagram to help us understand the problem we need to solve. The diagram itself uses int values instead of long values as they fit easier in the image. But the main idea remains the same:
To encode our data into a Base64 string we must split it into 6-bit chunks. As shown in the diagram, we'll obtain these chunks by reading the bits from left to right and from top to bottom.
Each 6-bit chunk is then converted into one of the 64 characters of our Base64 alphabet.
The problem appears at the boundaries. For example, look at the end of the topmost long value: it only contributes 2 bits to a 6-bit chunk. The remaining 4 bits are obtained from the start of the next long value.
The same issue happens at the very end of the data: it only contributes 2 bits to a 6-bit chunk. In this case, the remaining 4 bits are assumed to be zero.
Encoding the Main Section
As our data has a fixed and known size, we can provide a specific implementation instead of a general one. In other words, let's not "what if someday our data size changes?"
So, we'll create a method for encoding the main section of a long value. In other words, this method deals only with the non-boundary section. Here's the code:
Let's break it down:
- The method takes a StringBuilder instance for building the encoded string.
- The method takes an int value named 'bitsUsed', possibly zero, representing the number of bits already consumed of the long value.
- The method starts by declaring the 'bits' local variable representing the number of bits which remains to be encoded.
- Next, it goes into a do-while loop, first subtracting 6, the size of our chunk, from 'bits'.
- Then, we calculate 'shifted' by performing an unsigned right-shift operation on the 'value' using the current 'bits' count. This moves the 6-bit chunk we want to encode into the least significant position.
- Next, we extract our 6-bit chunk, called 'sextet', by masking out everything except the lowest 6 bits.
- We convert the 'sextet' value to a character from the alphabet and append it to the output.
- The loop continues as long as a whole 6-bit chunk can be processed.
- Once bits drops below 6, we exit the loop, and the method returns the number of unprocessed bits.
Encoding the Boundary
Next, we have to encode the 6-bit chunk that is possibly split between the boundaries of two consecutive long values. Here's a method that handles this case:
The method takes the 'bits' int value returned by the previous method, representing the number of unprocessed bits in the 'prev' long value.
As an example, let's assume the value of 'bits' is 4.
The method reads the last 4 bits from the 'prev' long value and the first 2 bits of the 'next' long value. These two are joined together into our 6-bit chunk and we use it to find one of the 64 characters of our alphabet.
Finally, the method returns the number of bits which were consumed from 'next'. In our example, the method returns the value 2.
The toString Method and Testing
We'll have the 'toString' method of our class return the Base64 encoded string. Let's implement it.
The resulting string will have 44 characters. There are 256 bits and each character takes 6 bits resulting in 43 "whole" characters. And, we'll pad the output with a '=' character, so that the length of the encoded string is a multiple of 4.
To ensure our implementation is correct, we'll add a test that generates a number of random instances, and compares our encoding with the output from JDK's 'Base64.Encoder' class:
It works.
You can find the source code used in the post in the tweet below. It's a single Java file so it can be run with 'java <filename>'.
You can increase the number of test iterations, get the number of iterations from the command arguments or even run the test indefinitely.

@umshouldstudy Neden herkes karanlık temalara yöneldi green flag görmek çok nadir olmaya başladı webtoknlarda da aynı
Last Seen Hashtags on Sotwe
mmdr2000
MLBBnsfw
Seen from Singapore
تانجولايف
Seen from Kuwait
spying #mom
Seen from Algeria
crying #forced filter:videos
Seen from Turkey
aapopalonen
Seen from United States
liseli()**********************************************filter:videos
Seen from Turkey
kotekli
Seen from Turkey
ShrutiHaasanHot
Seen from India
texastour2024
Seen from Korea
Trends for you
Most Popular Users

Elon Musk 
@elonmusk
239.3M followers

Barack Obama 
@barackobama
118.5M followers

Cristiano Ronaldo 
@cristiano
114.3M followers

Donald J. Trump 
@realdonaldtrump
111.7M followers

Narendra Modi 
@narendramodi
106.9M followers

Rihanna 
@rihanna
98.6M followers

NASA 
@nasa
92M followers

Justin Bieber 
@justinbieber
91.7M followers

KATY PERRY 
@katyperry
89.9M followers

Taylor Swift 
@taylorswift13
83.9M followers

Lady Gaga 
@ladygaga
75.4M followers

Virat Kohli 
@imvkohli
73.3M followers

Kim Kardashian 
@kimkardashian
70.8M followers

YouTube 
@youtube
68.6M followers

Neymar Jr 
@neymarjr
66.3M followers

Bill Gates 
@billgates
65.1M followers

Selena Gomez 
@selenagomez
63M followers

The Ellen Show
@theellenshow
62.1M followers

CNN 
@cnn
61.6M followers

X 
@x
60.6M followers




