6
6
6db2020-11-25 18:44:58
C++ / C#
6db, 2020-11-25 18:44:58

How to convert string to hex string byte by byte and vice versa?

There are 2 methods, encoding text into binary code, then writing it in binary form to a file.
The second method decodes the binary string from the file back to text.

//Конвертер String to Binary
        public string StrToBinary(string input)
        {
            StringBuilder str = new StringBuilder();

            //Итерируясь по каждому символу строки, переводим его в двоичную СС
            foreach (char L in input.ToCharArray())
            {
                str.Append(Convert.ToString(L, 2).PadLeft(8, '0'));
            }
            //Полученное выводим, как строку
            return str.ToString();

        }

//Перевод Binary в String
        public string BinToString (string input)
        {
            List<Byte> byteList = new List<Byte>();


            //Итерируюясь, извлекаем подстроку длинной 8 бит, конвертируем ее
            for (int i = 0; i < input.Length; i += 8)
            {
                
                byteList.Add(Convert.ToByte(input.Substring(i, 8), 2));
                
            }

            return Encoding.Default.GetString(byteList.ToArray());
            
        }


So, everything works fine with English, but Russian frankly gets up. Error:
5fbe7c710a032373179059.png

I tried all the encodings, looked at the Internet, but reached a dead end.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vasily Bannikov, 2020-11-25
@6db

Try like this:

using System;
using System.Linq;
using System.Text;

var originalString = "Lorem ipsum dolor sit amet. Мама мыла раму";
Console.WriteLine($"Original string: {originalString}");

var utf8Bytes = Encoding.UTF8.GetBytes(originalString);
var hexadecimalUtf8Bytes = ConvertBytesToHexString(utf8Bytes);
Console.WriteLine($"Hexadecimal bytes: {hexadecimalUtf8Bytes}");

var convertedBytes = ConvertHexStringToBytes(hexadecimalUtf8Bytes);
var convertedString = Encoding.UTF8.GetString(convertedBytes);
Console.WriteLine($"Decoded: {convertedString}");

static string ConvertBytesToHexString(byte[] bytes) =>
    BitConverter.ToString(bytes).Replace("-", "");

static byte[] ConvertHexStringToBytes(string hex) =>
    Enumerable.Range(0, hex.Length)
        .Where(x => x % 2 == 0)
        .Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
        .ToArray();

Original string: Lorem ipsum dolor sit amet. Мама мыла раму
Hexadecimal bytes: 4C6F72656D20697073756D20646F6C6F722073697420616D65742E20D09CD0B0D0BCD0B020D0BCD18BD0BBD0B020D180D0B0D0BCD183
Decoded: Lorem ipsum dolor sit amet. Мама мыла раму

F
freeExec, 2020-11-25
@freeExec

Russian characters in UTF-8 take up two bytes (to be precise, 11 bits, but this is not accurate). Accordingly, the trailing zeros of the second byte disappear, and as a result, everything goes to hell.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question