C++Builder What You Need For Encoding Strings Using Bit Shifting

FireWind

Свой
Регистрация
2 Дек 2005
Сообщения
1,957
Реакции
1,199
Credits
4,009
What You Need For Encoding Strings Using Bit Shifting
By Yilmaz Yoru December 13, 2021

What is the fastest string coding and decoding method? What is the fastest method to secure the contents of your string? Can we use shifting to encode or decode a string? Can we use shifting on Strings or on Binary data? Let’s answer these questions.

If you are working with Wide Strings, then you should read Для просмотра ссылки Войди или Зарегистрируйся which specifically discusses wide strings.

What are bitwise operations?​

The Для просмотра ссылки Войди или Зарегистрируйся is the most basic unit of information in computing and digital communications. In real all operators are mainly based on Bit Operations which are also called Bitwise Operations. In computer programming, a Для просмотра ссылки Войди или Зарегистрируйся operates on a bit string, a bit array, or a binary numeral (considered as a bit string) at the level of its individual bits, 1s, and 0s. The Bitwise Operation is basic to the higher-level arithmetic operations and it is a fast and simple action because it is directly supported by the processors. Most bitwise operations are presented as two-operand instructions where the result replaces one of the input operands.

Because of all these basics of the microarchitecture of computers, it is very important to know Bitwise Operators. C Programming language is one of the oldest programming languages and a lot of operands, operators in other programming languages got inspiration from this language. C and C++ have the same operators and most of them are the same in other programming languages. We have explained well about operators in general in this Для просмотра ссылки Войди или Зарегистрируйся post before. Now let’s see some Bit Shifting and Encoding – Decoding examples.

How can I simply encode and decode a String in C++?​

As in this bitset, we can do this operation on integers, floating numbers, chars, strings or binary data. Because all data operations are in done bit form in real. We can use bitwise left shifting operation to encode a data or we can use right shifting operation to decode this encoded data.

We can simply left shift (or right shift) the character of a string and we can simply right shift (or left shif) the character of a string as below,
C++:
std::string str  =  "This is a char string that will be encoded!";
str[i] = ((unsigned char)str[i])<<1;  // left bit shifting string char
str[i] = ((unsigned char)str[i])>>1;  // right bit shifting string char
This example below encodes and decodes string.
C++:
#include <iostream>
#include <string>
#include <bitset>
 
int main()
{
 std::string str  =  "This is a char string that will be encoded!";
 
 std::cout << "Original String: " << str << '\n';
 
 for(int i=0; i< str.length(); i++)
 {
 str[i] = ((unsigned char)str[i])<<1; // shifting each char bits to the left
 }
 std::cout <<  "Encoded String : " << str << '\n';
 
 for(int i=0; i< str.length(); i++)
 {
 str[i] = ((unsigned char)str[i])>>1;  // shifting each char bits to the right
 }
 std::cout <<  "Decoded String : " << str << '\n';
 
 getchar();
 return 0;
}
Output should be somethings like this, encoded characters may be different because of font will be used.
Код:
Original String: This is a char string that will be encoded!
Encoded String : ¿ºÊµ@ʵ@┬@ãº┬õ@µ×õÊ▄╬@׺┬×@¯ÊÏÏ@─╩@╩▄ãÌ╚╩╚B
Decoded String : This is a char string that will be encoded!

How can we encrypt a String with more complex encoding?​

If you check this example output above each character symbol refers to another character symbol. If we want to avoid this we can add a complexity function which is iterated by the index number of the String Member. We can add complexity function to bitwise operation for each index of elements. For example we can use (index%8). So these kind of data is hard to encode if you don’t know this complexity function. You can formulate your own function. Be sure it is reversible and depends on index number, and some other parameters. We can use Circular Bit Shifting with Complexity to encode or decode wide strings as below,
C++:
#include <iostream>
#include <string>
#include <bitset>
 
int main()
{
 std::string str  =  "This is a char string that will be encoded!";
 
 std::cout << "Original String: " << str << '\n';
 
 for(int i=0; i< str.length(); i++)
 {
 str[i] = ((unsigned char)str[i]) << (1+i%7) | ((unsigned char)str[i] >> 8- (1+i%7)); // shifting each char bits to the left with a complexity function
 }
 std::cout <<  "Encoded String : " << str << '\n';
 
 for(int i=0; i< str.length(); i++)
 {
 str[i] = ((unsigned char)str[i])>> (1+i%7) | | ((unsigned char)str[i] >> 8- (1+i%7);  // shifting each char bits to the right with a complexity function
 }
 std::cout <<  "Decoded String : " << str << '\n';
 
 getchar();
 return 0;
}
We can use this method to protect our data, user names, passwords etc. We can also hash this Encoded String by using powerful Cryptographic Hash Functions In Modern C++ On Windows (SHA, SHA2, MD5, BobJenkins, etc.). So you can put another dimension to your passwords and user names etc. See this Для просмотра ссылки Войди или Зарегистрируйся