Home > Mobile >  String to byte not working and generate PDF
String to byte not working and generate PDF

Time:12-10

When

byte[] bytes = {105,122,101,32 ......... 105,122,101,32}
System.IO.File.WriteAllBytes("E:\\hello-"   DateTime.Now.ToString("yyyyMMddhhmmssff")   ".pdf", bytes);

this work


string data = "105,122,101,32 ......... 105,122,101,32"
byte[] bytes = Encoding.ASCII.GetBytes(data );
System.IO.File.WriteAllBytes("E:\\hello-"   DateTime.Now.ToString("yyyyMMddhhmmssff")   ".pdf", bytes);

not work

why can't i convert string to byte well? Thanks

CodePudding user response:

OK; so the problem here is that for whatever reason, you have a comma-separated string that contains the bytes as integers, and you need to parse that into bytes. Horrible (base-64 would be a far better choice), but: fine: simply do that. This is not pretty, but should work fine:

string data = ...
var bytes = Array.ConvertAll(data.Split(','), byte.Parse);
System.IO.File.WriteAllBytes("E:\\hello-"   DateTime.Now.ToString("yyyyMMddhhmmssff")   ".pdf", bytes);
  •  Tags:  
  • c#
  • Related