1

My code here reads all the bytes of a image and stores it in the byte array. Is there a way to convert these bytes into ascii then split them up to 512-char(ascii char) long pieces? Like when you try splitting a string based on the length, you can do that. Can you do something similar to splitting this into 512 lengths? This is to send to the server.

byte[] imagesize; 
imagesize = File.ReadAllBytes(@"C:\image.jpeg");
Console.Write(imagesize[1].ToString());

What I really want is to convert these bytes into plain ASCII format (Which in C# would be Encoding.ASCII), then split that long ASCII line from converting all the bytes into 512-char(?) long chunks into a byte array (byte[] chunks). So when I send the packets I can do

 for(i=0; i<AmountOfChunks; i++)
 {
      Packet.payload = chunks[i];
      //The "chunks" is the ASCII formated array.
 }

If someone knows how to do this, it would greatly help. Thanks, if there's anything more, i'll try to explain it in more detail if i can. If this is wrong, because i know a byte is 8-bit each. Then somehow to be able to do it, maybe the bytes into a list?

10
  • Why don't you just read it in chunks then? Commented May 13, 2012 at 20:13
  • Those comments don't really explain anything I'm afraid, could you update your question with detailed information on what exactly you need to send to the server? Commented May 13, 2012 at 20:36
  • @Nom what do you expect from ASCII represantation of this int 1234567890, 0gKWSQ==, D2-02-96-49 or D2029649? They are all correct. Commented May 13, 2012 at 21:04
  • Convert to ASCII how? base64? base85? 7-bit encoding? hexadecimal? Commented May 13, 2012 at 21:04
  • @L.B To be honest, I don't know exactly how this server makes the ASCII characters. This link (screen shot) is an example of the 512-char prntscr.com/97iec. I'm assuming it looks like ASCII, though if i'm wrong. My great apologies for the confusion. Commented May 13, 2012 at 21:10

2 Answers 2

2

Not clear why you needs this, but you might be looking for Convert.ToBase64String() to get a string representation. For chunking you can just walk over the resulting string and split at the appropriate indexes:

byte[] imagesize = File.ReadAllBytes(@"C:\image.jpeg");
string base64String = Convert.ToBase64String(imagesize);

List<string> chunks = new List<string>();
for (int i = 0; i < base64String.Length; i+=512)
{
    chunks.Add(base64String.Substring(i, Math.Min(512, base64String.Length - i)));
}
Sign up to request clarification or add additional context in comments.

4 Comments

I need this to send a packet to a server, which the max length is 512 characters, so to send a whole image to the server, it needs to be split up into 512 chunks then sent one after another. So I needed to find a way to split the readallbytes. Thanks!
Yeah, thats why I also need to convert it to ASCII, which I can attempt to do, but if you know the proper code to do it, it can be a great help.
@Nom There are many ways to convert the binary data to ASCII strings. What format does your server expects?
@L.B I believe it's just the standard ASCII string. It just doesn't accept raw bytes.
0

Try to make this

int i=0;  
do
{
     sendBytes = imagesize.Skip(512*i).Take(512).ToArray();
     //Your function of send
     i++;
}
while(imagesize.Count()-512*i>0)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.