How to convert byte[512] to string 1024 length [duplicate] - c#

This question already has answers here:
How do you convert a byte array to a hexadecimal string, and vice versa?
(53 answers)
Closed 5 years ago.
This bit of code produces an array of size 512, it converts a string that was originally 1024 characters in length and has hex data.
byte[] content = Enumerable.Range(0, data.Length)
.Where(x => x % 2 == 0)
.Select(x => Convert.ToByte(data.Substring(x, 2), 16))
.ToArray();
I like to know how to reverse this so taking a byte[512] I want to produce the 1024 length string containing the hex data.

Try with
var result = string.Join("",
inputString.Select(c => String.Format("{0:X2}", Convert.ToInt32(c))));

Related

Cast string to hex byte array [duplicate]

This question already has answers here:
How do you convert a byte array to a hexadecimal string, and vice versa?
(53 answers)
Converting string to byte array in C#
(20 answers)
Closed 2 years ago.
I need to cast a string to a byte array in hex representation.
For example:
Value: 06000002
What I need is:
30 36 30 30 30 30 30 32
I tried to implicit convert all chars to byte as following:
byte[] bytes = new byte[daten.Length];
for (int i = 0; i < daten.Length; i++)
{
int value = Convert.ToInt32(daten[i]);
bytes[i] = (byte)daten[i];
}
However I always get this result:
48 54 48 48 48 48 48 50
I do not need the result as string! I need it as byte array!
How do achive this?
All you should need is:
var value = "06000002";
byte[] bytes = Encoding.UTF8.GetBytes(value);
In .NET 5 you can then use
string hexString = Convert.ToHexString(bytes);
To verify your result is what you expected
3036303030303032
https://dotnetfiddle.net/6sUmgE
To get the desired output, you can convert each character to its hex representation:
var s = "06000002";
var bytes = s.Select(c => (byte) c);
var hexCodes = bytes.Select(b => b.ToString("X2"));
You could stop here, or perhaps convert it to an Array or List.
Note that bytes are just numbers, there is no such thing as "hex bytes". The only time where "hex" exists is after conversion to string format, as I did above.
To still get it as one string you can proceed with this:
var result = string.Join(' ', hexCodes);
Or all in one go:
var result = string.Join(' ', "06000002".Select(c => ((byte) c).ToString("X2")));

Convert to base 2 then split in an array [duplicate]

This question already has answers here:
Convert int to a bit array in .NET
(10 answers)
Closed 7 years ago.
I want to convert my base 10 number to base 2 and then store parts in an array.
Here are two examples:
my value is 5 so it will be converted to 101 then I have an array like this: {1,0,1}
or my value is 26 so it will be converted to 11010 then I will have an array like this: {0,1,0,1,1}
Thank you in advance for your time and consideration.
To convert the int 'x'
int x = 3;
One way, by manipulation on the int :
string s = Convert.ToString(x, 2); //Convert to binary in a string
int[] bits= s.PadLeft(8, '0') // Add 0's from left
.Select(c => int.Parse(c.ToString())) // convert each char to int
.ToArray(); // Convert IEnumerable from select to Array
Alternatively, by using the BitArray class-
BitArray b = new BitArray(new byte[] { x });
int[] bits = b.Cast<bool>().Select(bit => bit ? 1 : 0).ToArray();
Source: https://stackoverflow.com/a/6758288/1560697

Convert Hex string to normal String in C#

I have got this code from somewhere to convert hex string to normal string.
But I cannot understand this. Can anybody explain this please ?
In this string, The first line takes each two characters from the string and convert it to byte.
But, I dont understand why they are assigning array to only half length of byte array ?
Sometimes it gets error too, i.e if Inputstring length is 350, byte length would be 175, and char length is 87.5, and char array is assigned to 87 only, thats not enough to hold all the characters in byte array.
public static string HextoString(string InputText)
{
byte[] bb = Enumerable.Range(0, InputText.Length)
.Where(x => x % 2 == 0)
.Select(x => Convert.ToByte(InputText.Substring(x, 2), 16))
.ToArray();
//return Convert.ToBase64String(bb);
char[] chars = new char[bb.Length / sizeof(char)];
System.Buffer.BlockCopy(bb, 0, chars, 0, bb.Length);
return new string(chars);
}
That is because .Where(x => x % 2 == 0) filters the input string to only those values, that have even indexes, so the output will have half the length of the original.

Get start position of byte chunk from a byte array [duplicate]

This question already has answers here:
byte[] array pattern search
(29 answers)
Closed 9 years ago.
Supposing I have a byte array of size 4096, what's an efficient way to get the start position of a chunk of say... 5 bytes that matches a pattern?
For e.g., I would like to get the start position of the 1st possible match of the byte array
var match = new byte[] { 0x03, 0x04, 0x05, 0x06, 0x07 };
So if the above chunk is found within my byte array, it will return me the position of the 1st byte (0x03)
You can use Linq:
public static int IndexOfArray<T>(T[] source, T[] search)
{
var result = Enumerable.Range(0, source.Length - search.Length)
.Select(i => new
{
Index = i,
Found = source.Skip(i)
.Take(search.Length)
.SequenceEqual(search)
})
.FirstOrDefault(e => e.Found);
return result == null ? -1 : result.Index;
}

From hex string to byteArray C#

I have in input the string: "0080801D803480002A1168301FE16E09"
and when i convert it to byteArray with the code:
Convert.ToByte(inputWrite.Substring(i, 2), 16);
I get the byte array in first position = "0", but i need to have "00", so when i convert it again into a String a don't get "08" but "00" at the begining.
i get in the and the string "080801D80348002A1168301FE16E9" and like this i'm missing some important 0, that i need to convert then from this last string to byte again and to decimal values.
Once you have your byes in an array, there's no difference between 0 and 00.
What you need to do is, when converting those bytes back to a string, make sure you put any leading zeros back in. You can do this by calling
string byteAsTwoDigitString = myByte.ToString("X2");
The X says "as hexadecimal", the 2 says "with at least two digits".
You can also do this using LINQ:
public static byte[] StringToByteArray(string hex) {
return Enumerable.Range(0, hex.Length)
.Where(x => x % 2 == 0)
.Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
.ToArray();
}
You can also refer this
You seem to be confusing things. Because each byte is represented by two characters, bytes array will be two times shorter than the string. When parsing back, you have to make sure that each byte must be converted to two-characters string, even if it is less than 0x10, i.e. does not require second character.
That said, you can use following LINQ oneliner:
string convertedBack = string.Join(string.Empty, bytes.Select(x => x.ToString("X2")).ToArray());

Categories