Series Part 1 of 3 >> Continue
_______________________________
Um so I did this little release to help people understand the encoding and decoding of bytes in a packet for the Habbo Hotel emulation protocols. Now this is stretching to the core of how this works so I did it in a language most will understand... PHP (better love me for that). Once you have grasped the concepts behind how each method works you will then be able to understand how the files such as 'ByteUtil.cs', 'HexEncoding.cs' and 'ByteEncoding.cs' work.
It's fairly simple and you will be like 'oo-hh!' once you understand how strings, booleans and integers can be converted to and from base64, hexadecimal and binary values. This is in my opinion the hardest thing to grasp when it comes to Habbo Hotel emulation although once you are able to understand this and write your own equivalent methods, all you need to do is figure out the structure and voila you have an emulator.
The release focuses only on encoding and not decoding. To decode, simply reverse the methods at their core.
Important
Do not confuse this with something you can use live. It's a) done in PHP and b) does not use any native methods therefore it's quite inefficient as it's only supposed to show you the meat behind the native method to help you better understand everything.
Habbo uses base64 (among other encoding) which at it's core works like this:
string -> encodes ascii (depending on type) -> 8 bit binary values -> joined together into a string containing 'x' amount of bytes -> groups into 6 bits since base64 has a maximum of 64 values -> converted to integers -> used to map to encoded values.
Once you understand how the core works you can go about creating your own methods to handle base64 encoding and decoding (among other things).
This will only help you get the decoded packet, not give you the packet id and blah blah blah. This release/tutorial is here to help you decode the Habbo packets.
Other Important Things
Um in case you guys don't understand what binary being base 2 and hexadecimal being base 16 means then hear this out.
Normally, what you are taught in school is called base 10 counting. Meaning the numerical system goes from 0 to 9 and then 10 to 19. If we dissect any number (19 for example) you need to think of it like this. The number 19 is '1' - '9' meaning one set of 10 and 9, totalling 19. The same methodology of thinking is applied to binary base 2 and hexadecimal base 16.
To represent these in your program, it all depends on the language. For C++ to represent a hexadecimal number it must begin with the prefix '0x'. For binary it must begin with the prefix '0b'. Decimal numbers can be represented as standalone numbers. Make sure you are aware of this, all depends on the language you are programming!
Release #1 (Encoding/Decoding Basics)
To convert a decimal value to a binary or hexadecimal value take a look at this:
So in turn if you want to try encoding a decimal value to a hexadecimal value, try this:
I found converting to ascii pretty straight forward so no need on a snippet for that. But in case you did not know, once it's encoded to ascii you can grab the corresponding decimal values assigned to the letter enabling you to apply these methods.
In practice, your methods will be quite a bit shorter as the language you choose will have native methods to handle all of this. I am just showing you how they work so that when you type them or copy them from google you know how it works as this will come in handy if you plan to have a successful emulator.
Release #2 (Bitwise Operations)
This is very important for handling the encoding and decoding of the Habbo packet structure as you will need to shuffle bits to 're-assemble' the packet (so to speak). Make sure you are familiar with that or else you will have some problems. It's pretty straight forward, if you need help just google the Bitwise operators. Some examples of those are the & (and), | (or), ^ (xor), << (shuffle left), >> (shuffle right).
_______________________________
Um so I did this little release to help people understand the encoding and decoding of bytes in a packet for the Habbo Hotel emulation protocols. Now this is stretching to the core of how this works so I did it in a language most will understand... PHP (better love me for that). Once you have grasped the concepts behind how each method works you will then be able to understand how the files such as 'ByteUtil.cs', 'HexEncoding.cs' and 'ByteEncoding.cs' work.
It's fairly simple and you will be like 'oo-hh!' once you understand how strings, booleans and integers can be converted to and from base64, hexadecimal and binary values. This is in my opinion the hardest thing to grasp when it comes to Habbo Hotel emulation although once you are able to understand this and write your own equivalent methods, all you need to do is figure out the structure and voila you have an emulator.
The release focuses only on encoding and not decoding. To decode, simply reverse the methods at their core.
Important
Do not confuse this with something you can use live. It's a) done in PHP and b) does not use any native methods therefore it's quite inefficient as it's only supposed to show you the meat behind the native method to help you better understand everything.
Habbo uses base64 (among other encoding) which at it's core works like this:
string -> encodes ascii (depending on type) -> 8 bit binary values -> joined together into a string containing 'x' amount of bytes -> groups into 6 bits since base64 has a maximum of 64 values -> converted to integers -> used to map to encoded values.
Once you understand how the core works you can go about creating your own methods to handle base64 encoding and decoding (among other things).
This will only help you get the decoded packet, not give you the packet id and blah blah blah. This release/tutorial is here to help you decode the Habbo packets.
Other Important Things
Um in case you guys don't understand what binary being base 2 and hexadecimal being base 16 means then hear this out.
Normally, what you are taught in school is called base 10 counting. Meaning the numerical system goes from 0 to 9 and then 10 to 19. If we dissect any number (19 for example) you need to think of it like this. The number 19 is '1' - '9' meaning one set of 10 and 9, totalling 19. The same methodology of thinking is applied to binary base 2 and hexadecimal base 16.
To represent these in your program, it all depends on the language. For C++ to represent a hexadecimal number it must begin with the prefix '0x'. For binary it must begin with the prefix '0b'. Decimal numbers can be represented as standalone numbers. Make sure you are aware of this, all depends on the language you are programming!
Release #1 (Encoding/Decoding Basics)
To convert a decimal value to a binary or hexadecimal value take a look at this:
PHP:
/**
* Converts a decimal value to a binary string.
*
* @param int $decimal The decimal value to be converted.
*
* @return string
*/
function dec_to_bin($decimal)
{
$bin_map = array(
0 => 0,
1 => 1
);
$number = $decimal;
$binary = '';
while ($decimal > 0)
{
$bit = $decimal % 2;
// Appends the binary string in reverse order.
$binary .= $bin_map[$bit];
$decimal /= 2;
// Evens the decimal value if it was an odd decimal value (1, 3, 5, 7, 9, etc.).
if ($bit == 1)
{
$decimal -= 0.5;
}
}
// Reverses the binary string back to it's original order.
$binary = strrev($binary);
return $binary;
}
/**
* Converts a decimal value to a hexadecimal string.
*
* @param int $decimal The decimal value to be converted.
*
* @return string
*/
function dec_to_hex($decimal)
{
// Maps numerical values to hexadecimal values.
$hex_map = array(
0 => 0,
1 => 1,
2 => 2,
3 => 3,
4 => 4,
5 => 5,
6 => 6,
7 => 7,
8 => 8,
9 => 9,
10 => 'A',
11 => 'B',
12 => 'C',
13 => 'D',
14 => 'E',
15 => 'F',
);
// Copies decimal value.
//$number = $decimal;
// '0x' is the typical prefix or a trailing 'h'.
$hex = '0x';
// Binary base.
$bin_base = 2;
// Hexadecimal base.
$hex_base = 16;
// Number to group the bits into is log(hex base, bin base).
$group = log($hex_base, $bin_base);
// Converts the decimal string into binary.
$binary = dec_to_bin($decimal);
// Splits the binary string into an array for each bit.
$bit_array = str_split($binary, $group);
$new_binary = '';
// Padds the binary string so that it is divisable by the $group variable. This is very important.
foreach ($bit_array as $key => $value)
{
$modular = strlen($value) % $group;
if ($modular != 0)
{
for ($i = 0; $i < ($group - $modular); $i++)
{
$new_binary = 0 . $new_binary;
}
$new_binary .= $value;
}
else
{
$new_binary .= $value;
}
}
$fourbit_array = str_split($new_binary, $group);
//$size = sizeof($fourbit_array);
$j = 1;
// Loops through the half a byte array and assigns each half a byte a hexadecimal value based on
// their numerical value and appends it to the hexadecimal string variable.
foreach ($fourbit_array as $key => $value)
{
$value = strrev($value);
$new_array = str_split($value);
$i = 1;
$index = 0;
foreach ($new_array as $bit)
{
$index += $bit * $i;
// Increases base multiplier.
$i *= $bin_base;
}
$hex .= $hex_map[$index];
$j++;
}
return $hex;
}
So in turn if you want to try encoding a decimal value to a hexadecimal value, try this:
PHP:
print dec_to_hex(255); // Prints '0xFF' which is equivalent to the numerical value of 255.
I found converting to ascii pretty straight forward so no need on a snippet for that. But in case you did not know, once it's encoded to ascii you can grab the corresponding decimal values assigned to the letter enabling you to apply these methods.
In practice, your methods will be quite a bit shorter as the language you choose will have native methods to handle all of this. I am just showing you how they work so that when you type them or copy them from google you know how it works as this will come in handy if you plan to have a successful emulator.
Release #2 (Bitwise Operations)
This is very important for handling the encoding and decoding of the Habbo packet structure as you will need to shuffle bits to 're-assemble' the packet (so to speak). Make sure you are familiar with that or else you will have some problems. It's pretty straight forward, if you need help just google the Bitwise operators. Some examples of those are the & (and), | (or), ^ (xor), << (shuffle left), >> (shuffle right).
Last edited: