Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 330 Vote(s) - 3.53 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Converting string to byte array in C#

#1
I'm converting something from VB into C#. Having a problem with the syntax of this statement:

if ((searchResult.Properties["user"].Count > 0))
{
profile.User = System.Text.Encoding.UTF8.GetString(searchResult.Properties["user"][0]);
}
I then see the following errors:

> Argument 1: cannot convert from 'object' to 'byte[]'
>
> The best overloaded method match for
> 'System.Text.Encoding.GetString(byte[])' has some invalid arguments

I tried to fix the code based on [this][1] post, but still no success:

string User = Encoding.UTF8.GetString("user", 0);

Any suggestions?


[1]:

[To see links please register here]

Reply

#2
static byte[] GetBytes(string str)
{
byte[] bytes = new byte[str.Length * sizeof(char)];
System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
return bytes;
}

static string GetString(byte[] bytes)
{
char[] chars = new char[bytes.Length / sizeof(char)];
System.Buffer.BlockCopy(bytes, 0, chars, 0, bytes.Length);
return new string(chars);
}
Reply

#3
use this

byte[] myByte= System.Text.ASCIIEncoding.Default.GetBytes(myString);
Reply

#4
First of all, add the `System.Text` namespace

using System.Text;
Then use this code

string input = "some text";
byte[] array = Encoding.ASCII.GetBytes(input);

Hope to fix it!
Reply

#5
A refinement to JustinStolle's edit (Eran Yogev's use of BlockCopy).

The proposed solution is indeed faster than using Encoding.
Problem is that it doesn't work for encoding byte arrays of uneven length.
As given, it raises an out-of-bound exception.
Increasing the length by 1 leaves a trailing byte when decoding from string.

For me, the need came when I wanted to encode from `DataTable` to `JSON`.
I was looking for a way to encode binary fields into strings and decode from string back to `byte[]`.

I therefore created two classes - one that wraps the above solution (when encoding from strings it's fine, because the lengths are always even), and another that handles `byte[]` encoding.

I solved the uneven length problem by adding a single character that tells me if the original length of the binary array was odd ('1') or even ('0')

As follows:

public static class StringEncoder
{
static byte[] EncodeToBytes(string str)
{
byte[] bytes = new byte[str.Length * sizeof(char)];
System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
return bytes;
}
static string DecodeToString(byte[] bytes)
{
char[] chars = new char[bytes.Length / sizeof(char)];
System.Buffer.BlockCopy(bytes, 0, chars, 0, bytes.Length);
return new string(chars);
}
}

public static class BytesEncoder
{
public static string EncodeToString(byte[] bytes)
{
bool even = (bytes.Length % 2 == 0);
char[] chars = new char[1 + bytes.Length / sizeof(char) + (even ? 0 : 1)];
chars[0] = (even ? '0' : '1');
System.Buffer.BlockCopy(bytes, 0, chars, 2, bytes.Length);

return new string(chars);
}
public static byte[] DecodeToBytes(string str)
{
bool even = str[0] == '0';
byte[] bytes = new byte[(str.Length - 1) * sizeof(char) + (even ? 0 : -1)];
char[] chars = str.ToCharArray();
System.Buffer.BlockCopy(chars, 2, bytes, 0, bytes.Length);

return bytes;
}
}
Reply

#6
Does anyone see any reason why not to do this?

mystring.Select(Convert.ToByte).ToArray()
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through