how to convert byte to string in C# [duplicate] - c#

This question already has answers here:
How to convert UTF-8 byte[] to string
(16 answers)
Closed 7 years ago.
I want to know how to convert a byte[] to string. I have variable K an integer array and pwd a byte[] hence the the code bellow is giving me errors?
public void temp()
{
int[] k = new int[256];
byte[] pwd;
int temp = 50;
k[tmp] = pwd[(tmp % Convert.ToString((string)pwd).Length)];
}

Presumably if it's in a byte array, it's encoded. If you know what encoding, simply call GetString on the encoding. For example, if it's UTF8 encoded:
Encoding.UTF8.GetString(pwd);

Related

Convert String to Bytes Array in C# [duplicate]

This question already has answers here:
How do you convert a byte array to a hexadecimal string, and vice versa?
(53 answers)
How can I convert a hex string to a byte array? [duplicate]
(4 answers)
Closed 5 years ago.
I used the following code to convert a bytes array to a string:
byte[] saltBytes;
// ... filling the array with some values
for (int i = 0; i < saltBytes.Length; i++)
{
builder.Append(saltBytes[i].ToString("x2"));
}
string salt = builder.ToString();
Now, I would like to revert this process and convert my string to a bytes array. I was reading this question: Converting string to byte array in C#.
The answer mentions:
If you already have a byte array then you will need to know what type
of encoding was used to make it into that byte array.
I'm not sure about the encoding used in the snippet I posted. Does this have to do with the ToString("x2") part?

I'm looking for a way to convert char to bin array in C#

I'm trying to find the way to convert char to bit array, mess a bit with it and then convert it back. All answers are about string to byte.
Do you mean BitArray?
If so:
char c = 'X';
byte[] bytes = BitConverter.GetBytes(c);
BitArray bits = new BitArray(bytes);

How to read bytes as string [duplicate]

This question already has answers here:
Converting 2 bytes to Short in C#
(3 answers)
Closed 6 years ago.
I am trying to read bytes.
Bytes:
0x83 0xF6
Those bytes are equal to 33782.
I need a code to convert those bytes to 33782.
I have tried using this code:
Encoding.ASCII.GetString(new byte[] { 0x83, 0xF6 });
But it give this as a response: ??
You are using the wrong Conversion, converting this byte array with ASCII String will not give the correct result. The reason you get ?? is because the values 0xF6, 0x83 lie outside the ASCII Table which is used to make the conversion in your case.
You should use BitConverter.ToUInt16()
var number = BitConverter.ToUInt16(new byte[] { 0xF6, 0x83}, 0).ToString();
You have to reverse the byte array first though for the Little/Big Endians.
Perhaps this?
(0x83 * 256 + 0xF6).ToString()

Add values to byte array [duplicate]

This question already has answers here:
c# how to add byte to byte array
(7 answers)
Closed 7 years ago.
I have a byte array which contains an image; I converted the image to byte array using this method:
public byte[] imageToByteArray(System.Drawing.Image imageIn)
{
MemoryStream ms = new MemoryStream();
imageIn.Save(ms,System.Drawing.Imaging.ImageFormat.Gif);
return ms.ToArray();
}
This is the byte array:
Bitmap bmp = Image.FromFile("xxx");
byte[] buffer = ImageToByteArray(bmp);
Now I would like to add some minor minor information about the image in the byte array,such as the position it should be drawn to,etc.
How could it be done? Lets say i want to add these 2 values:1209,540.
First I don't think you can use these values 1209,540 since byte's max val is 255.
But if your values are between that range 0-255, why don't you add two bytes at the end of the array and when the conversion is to be made remove the last two values?

Difference between using Encoding.GetBytes or cast to byte [duplicate]

This question already has answers here:
Encoding used in cast from char to byte
(3 answers)
Closed 9 years ago.
I was wondering if there's any difference between converting characters to byte with Encoding.UTF8.GetBytes or manually using (byte) before characters and convert them to byte?
For an example, look at following code:
public static byte[] ConvertStringToByteArray(string str)
{
int i, n;
n = str.Length;
byte[] x = new byte[n];
for (i = 0; i < n; i++)
{
x[i] = (byte)str[i];
}
return x;
}
var arrBytes = ConvertStringToByteArray("Hello world");
or
var arrBytes = Encoding.UTF8.GetBytes("Hello world");
I liked the question so I executed your code on an ANSI text in Hebrew I read from a text file.
The text was "שועל"
string text = System.IO.File.ReadAllText(#"d:\test.txt");
var arrBytes = ConvertStringToByteArray(text);
var arrBytes1 = Encoding.UTF8.GetBytes(text);
The results were
As you can see there is a difference when the code point of any of your characters exceeds the 0-255 range of byte.
Your ConvertStringToByteArray method is incorrect.
you are casting each char to byte. char's numerical value is its Unicode code point which could be larger than a byte, so the casting will often result in an arithmetic overflow.
Your example works because you've used characters with code points within the byte range.
when wanna cast characters that have encoding, you cant use first one, and you must say chose encoding standard
Yes there is a difference. All .Net strings are stored as UTF16 LE.
Use this code to make a test string, so you get high order bytes in your chars, i.e chars that have a different representation in UTF8 and UTF16.
var testString = new string(
Enumerable.Range(char.MinValue, char.MaxValue - char.MinValue)
.Select(Convert.ToChar)
.ToArray());
This makes a string with every possible char value. If you do
ConvertStringToByteArray(testString).SequenceEqual(
Encoding.UTF8.GetBytes(testString));
It will return false, demonstrating that the results differ.

Categories

Resources