Base64 length

  • Thread starter Thread starter Simon Pulo
  • Start date Start date
S

Simon Pulo

Hello

I've managed to encode an image file to base64 using
"convert.ToBase64String"
I know you cannot send the whole string to the server, but you need to send
so many char's at a time which i think is about 74. Is there a way in which
i can do this?
The whole base64 string is in the varible "b64String" but i wish to send 74
chars at a time.

Thanks

Si
 
Simon,

So why not use the substring method on the string? It will return
74-character chunks to you (assuming you ask for chunks of those sizes).

Also, the convention is to use 76 characters, not 74.

Finally, if you are using .NET 1.1 and before, you can call the overload
which takes a boolean, which will insert the line breaks for you into the
string. If you are using .NET 2.0 or above, you can call the overload that
takes a value from the Base64FormattingOptions which indicates to insert the
line breaks every 76 characters.

Hope this helps.
 
Simon said:
I've managed to encode an image file to base64 using
"convert.ToBase64String"
I know you cannot send the whole string to the server, but you need
to send so many char's at a time which i think is about 74. Is there
a way in which i can do this?
The whole base64 string is in the varible "b64String" but i wish to
send 74 chars at a time.

The appropriate RFC says the line length should be 76, but conforming
servers will accept fewer characters. Of course Convert.ToBase64String
is horribly inefficient, involving allocations of internal buffers to do
its work, plus you have to send the binary data in its entirity to this
function. What you rally need is an efficient stream class to do this:
one that allows you to write in data as it is received, one which only
uses a small buffer internally, one that returns data as a block, or
with line breaks. Indeed, you want something like this:

http://www.grimes.demon.co.uk/dotnet/encodedStreams.htm

Richard
 
so how can i do this?
this is the code i've tried

int size = b64String.Length;

int csize = 0;

string chunky;

while (csize < size + 1)

{

chunky = b64String.Substring(csize, 76);

csize = csize + 76;

Console.Write(chunky);

}


Nicholas Paldino said:
Simon,

So why not use the substring method on the string? It will return
74-character chunks to you (assuming you ask for chunks of those sizes).

Also, the convention is to use 76 characters, not 74.

Finally, if you are using .NET 1.1 and before, you can call the
overload which takes a boolean, which will insert the line breaks for you
into the string. If you are using .NET 2.0 or above, you can call the
overload that takes a value from the Base64FormattingOptions which
indicates to insert the line breaks every 76 characters.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Simon Pulo said:
Hello

I've managed to encode an image file to base64 using
"convert.ToBase64String"
I know you cannot send the whole string to the server, but you need to
send so many char's at a time which i think is about 74. Is there a way
in which i can do this?
The whole base64 string is in the varible "b64String" but i wish to send
74 chars at a time.

Thanks

Si
 
Simon said:
so how can i do this?
this is the code i've tried

int size = b64String.Length;
int csize = 0;
string chunky;
while (csize < size + 1)
{
chunky = b64String.Substring(csize, 76);
csize = csize + 76;
Console.Write(chunky);
}

It would help if you said what the problem with the above code is -
however, by inspection it's not too hard to work out.

Suppose you have a string of length 100. The first time through, it's
fine - the second time through, you're asking for the next 76
characters, when there are only 24 left. I'd rewrite the above as:

static void WriteInChunks (string input, int maxChunkSize)
{
int index=0;
while (index < input.Length)
{
int size = Math.Min(input.Length-index, maxChunkSize);
Console.WriteLine (input.Substring (index, size));
index += size;
}
}

Jon
 
Back
Top