H
H
holymotion2016-02-11 00:11:56
Programming
holymotion, 2016-02-11 00:11:56

How to convert string[] to char[]?

The bottom line is, I have a string string[] str2 = ["255","244","233","222"] - can I get an array of characters from it, for example chr[] chr1 - 255, 244, 233 etc. - I need this in order to get a string (for example, "DAD"), in fact, I have a word in byte form in str2.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
MiiNiPaa, 2016-02-11
@MiiNiPaa

1) Use stoi to turn each line into a number.
2) Assign each received number to an array spell element.
Code (character encoding in the string - CP866):

#include <algorithm>
#include <iostream>
#include <string>

int main()
{
    std::string strings[] = {"143", "224", "168", "162", "165", "226"};
    char line[16] {};

    std::transform(std::begin(strings), std::end(strings), std::begin(line),
                   [](const std::string& s) {
                        return std::stoi(s); });
    std::cout << line;
}

A
ar4ebaldello, 2016-02-11
@ar4ebaldello

Version with Russian encoding.

private static readonly Encoding Windows1251Encoding = Encoding.GetEncoding(1251);
static string FromCyrillicStringArray(string[] values)
{
    var bytes = new byte[values.Length];
    for (var i = 0; i < values.Length; i++)
        bytes[i] = Convert.ToByte(values[i]);
    return Windows1251Encoding.GetString(bytes);
}
...
var codes = new[] { "207", "192", "207", "192" };
Console.WriteLine(FromCyrillicStringArray(codes) == "ПАПА"); // true

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question