5

I have a project which is using (I assume) .NET Core. I have a library which stores securely key-pair values. The key is string and the value should be a byte[], so I have to convert the string that I want to store as byte[]:

bytes[] my_bytes = Encoding.Unicode.GetBytes(txtSomeInfo.Text)

The problem is when i retrieve the value, because is an byte[] array i should convert it back to string like this:

my_string = Encoding.Unicode.GetString(my_bytes)

I found it some answers in Stack Overflow, but... I can't use it just like that because this version of GetString method of System.Text.Encoding is asking for two additional parameters: int index, and int count

How can I get my string back?

0

1 Answer 1

11
my_string = Encoding.Unicode.GetString(my_bytes, 0, my_bytes.Length);

As Jon points out in the comments, the C# naming standards use camelCase for local variables so you should rename my_bytes and my_string to myBytes and myString respectively.

Sign up to request clarification or add additional context in comments.

2 Comments

I think you mean Length rather than Count. (But otherwise, yes, this is what's needed. I'd personally fix the naming convention as well, but...)
Don't worry, I actually not using those names for variables in my project, so feel free to edit them if you wish.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.