-1

So i have a string

string enc = ""hx0.+dhx0-pdhx0pzdhx0xx";

This is encrypted and when decrypted has the hexadecimal values, the starting values are "0xfc,0xe8,0x82,0x00"

Then this

string decrypted = encryptDecrypt(enc);

then this then i divided it after every comma to with the split command string[] hi = decrypted.Split(',');

When i check using this code

foreach (var item in hi )
{
    Console.WriteLine(item.ToString());
}

it shows all the hexadecimal in side it i want to turn string array values which are 0xfc,0xe8,0x82,0x00 and more into byte array values which are 0xfc,0xe8,0x82,0x00 too not some other values

Agent Magma
  • 41
  • 1
  • 7
  • 1
    The `Convert.ToByte` method will convert a `string` to a `byte`, e.g. `var b = Convert.ToByte(hexString, 16);`. You just have to do that for each element in whatever way you deem appropriate, e.g. using `Array.ConvertAll`. – jmcilhinney Jul 09 '18 at 02:52
  • 3
    Possible duplicate of [Converting string to byte array in C#](https://stackoverflow.com/questions/16072709/converting-string-to-byte-array-in-c-sharp) – Mick Jul 09 '18 at 02:54
  • Not 100% sure what you're asking, but check out Encoding.UTF8.GetBytes(item); – Josh Jul 09 '18 at 03:05

5 Answers5

1

Is that the only string, or does that value change? Does your array need to be dynamic?

     string [] arrayString = new string []; //Your Array.

     byte [] arrayByte = new byte[arrayString.Length];

     for (int i = 0; i < arrayString.Length; i++)
     {
          arrayByte[i] = Convert.ToByte(arrayString[i], 16);
     }
  • This code will take in any string type array of any size and convert it to a byte array of any size. Keep in mind it doesn't overwrite your string array but if you wanted to add a line of code w/in the for loop to do that you can. –  Jul 09 '18 at 16:11
  • System.FormatException: 'Could not find any recognizable digits.' i gete this error while trying to run Byte[] b = Array.ConvertAll(hi, h => Convert.ToByte(h, 16)); – Agent Magma Jul 10 '18 at 00:35
  • Remember that arrays start with 0. Take out the “=“ and it shouldn’t crash. –  Jul 10 '18 at 17:16
1

Sample input:

String[] hi = "00,01,fe,ff".Split(',');

Conversion using a lambda function to convert each hexadecimal string to a byte:

Byte[] b = Array.ConvertAll(hi, h => Convert.ToByte(h, 16));

If you want a different kind of delegate:

Byte[] b = Array.ConvertAll(hi, HexToByte);

private Byte HexToByte(String h) 
{
    return Convert.ToByte(h, 16);
}

Same, with an expression-bodied function:

Byte[] b = Array.ConvertAll(hi, HexToByte);

private Byte HexToByte(String h) => Convert.ToByte(h, 16);

Or yet a different kind of delegate:

Converter<String, Byte> hexToByte = h => Convert.ToByte(h, 16);
Byte[] b = Array.ConvertAll(hi, hexToByte);

Array.ConvertAll is doing the real work. Conversion from hex is either a trivial idea that can be done inline or an important idea that can be given a name and/or a full implementation block.

Tom Blodget
  • 20,260
  • 3
  • 39
  • 72
  • System.FormatException: 'Could not find any recognizable digits.' i gete this error while trying to run Byte[] b = Array.ConvertAll(hi, h => Convert.ToByte(h, 16)); – Agent Magma Jul 10 '18 at 00:34
  • It works for "0xfc,0xe8,0x82,0x00". You must have some other characters in the string that aren't hex digits or the 0x prefix, or separating commas. Or perhaps you just have a 0x without following hex digits. – Tom Blodget Jul 10 '18 at 10:08
  • FormatException is as informative as it could be so: `Byte[] b = Array.ConvertAll(hi, h => { try { return Convert.ToByte(h, 16); } catch (FormatException ex) { throw new FormatException($@"Can't parse ""{h}""", ex);} });` – Tom Blodget Jul 10 '18 at 10:15
0

Convert each of the strings into byte and then store it to str variable.

byte[,] str = new byte[50,50];
int i = 0;
foreach (var item in hi)
{
    Console.WriteLine(item.ToString());
    byte[] arr = Encoding.ASCII.GetBytes(item.ToString());
    str[i] = arr;
    i++;
}

For more information, see this link

david
  • 3,225
  • 9
  • 30
  • 43
0

For some text is not Ascii that you can use Utf-8 to convert string to byte.

System.Text.Encoding.UTF8.GetBytes(item);
lindexi
  • 4,182
  • 3
  • 19
  • 65
-1

To convert from a string to a byte array, you can use the GetBytes method:

System.Text.Encoding.ASCII.GetBytes(item);
mousebyte
  • 61
  • 7