I have some binary data in a large string. I get the binary data by selecting a sub-string of the large one with the correct start and end indexes. Everything looks fine.
Then I save it to a file in binary mode. It is opened in"wb" mode, fconfigured, it is written with nonewlines, etc. However, the new file
ends up corrupted. It has the same number of bytes as the original, and
I can confirm they are also identical content-wise by opening them in an editor.
Is there something else I can check?
Am 16.02.2026 um 19:16 schrieb saito:
Is there something else I can check?
Sounds strange...
TCL version ?
What I can imagine is, that there are unicode codepoints not in range 0-255...
On 2/16/2026 1:19 PM, Harald Oehlmann wrote:
Am 16.02.2026 um 19:16 schrieb saito:
Is there something else I can check?
Sounds strange...
TCL version ?
What I can imagine is, that there are unicode codepoints not in range 0-255...
I suspect some cr or crnl characters somewhere in there. I trim for them but it doesn't seem to help.
A quick check might be:
puts "String length: [string length $data]"
puts "Byte length: [string length [encoding convertto binary $data]]"
On 2/16/2026 3:44 PM, et99 wrote:
A quick check might be:
puts "String length: [string length $data]"
puts "Byte length: [string length [encoding convertto binary $data]]"
I tried it but it says "unknown encoding "binary"". This is on version 8.6.
Here is the data. I can include a few characters before and after it if you want:
set data [base64::decode { iVBORw0KGgoAAAANSUhEUgAAABEAAAATCAIAAAD5x3GmAAAACXBIWXMAAAsSAAALEgHS3X78AAAA sklEQVQokZ1SQQ7CMAyL3fz/lfwAicNgNYeMqEBTMXzYYY1ruzEu15skmwHA9L9v28OsF5yW15EE wMB0+htBltR7d0nk3EPYS6mk+cJ3DOeppKA5yUpHwnh9fr21BszfzQypE0kOb601aS/CcEwS2SQ5 gGIg9X72/V6dxd5e7yDSSEr4dT+j+AlO2mFtbEIAAMD/0PGxHevRxCpPluUEp4L33uuO6sP50Tcz iyItaPae6gkuMnIItElP1QAAAABJRU5ErkJggg==}]
On 2/16/2026 3:44 PM, et99 wrote:
A quick check might be:
puts "String length: [string length $data]"
puts "Byte length: [string length [encoding convertto binary $data]]"
I tried it but it says "unknown encoding "binary"". This is on version 8.6.
Here is the data. I can include a few characters before and after it if
you want:
set data [base64::decode { iVBORw0KGgoAAAANSUhEUgAAABEAAAATCAIAAAD5x3GmAAAACXBIWXMAAAsSAAALEgHS3X78AAAA sklEQVQokZ1SQQ7CMAyL3fz/lfwAicNgNYeMqEBTMXzYYY1ruzEu15skmwHA9L9v28OsF5yW15EE wMB0+htBltR7d0nk3EPYS6mk+cJ3DOeppKA5yUpHwnh9fr21BszfzQypE0kOb601aS/CcEwS2SQ5 gGIg9X72/V6dxd5e7yDSSEr4dT+j+AlO2mFtbEIAAMD/0PGxHevRxCpPluUEp4L33uuO6sP50Tcz iyItaPae6gkuMnIItElP1QAAAABJRU5ErkJggg==}]
saito <saitology9@gmail.com> wrote:
On 2/16/2026 3:44 PM, et99 wrote:
A quick check might be:
puts "String length: [string length $data]"
puts "Byte length: [string length [encoding convertto binary $data]]"
I tried it but it says "unknown encoding "binary"". This is on version 8.6. >>
Here is the data. I can include a few characters before and after it if
you want:
set data [base64::decode {
iVBORw0KGgoAAAANSUhEUgAAABEAAAATCAIAAAD5x3GmAAAACXBIWXMAAAsSAAALEgHS3X78AAAA >> sklEQVQokZ1SQQ7CMAyL3fz/lfwAicNgNYeMqEBTMXzYYY1ruzEu15skmwHA9L9v28OsF5yW15EE >> wMB0+htBltR7d0nk3EPYS6mk+cJ3DOeppKA5yUpHwnh9fr21BszfzQypE0kOb601aS/CcEwS2SQ5 >> gGIg9X72/V6dxd5e7yDSSEr4dT+j+AlO2mFtbEIAAMD/0PGxHevRxCpPluUEp4L33uuO6sP50Tcz >> iyItaPae6gkuMnIItElP1QAAAABJRU5ErkJggg==}]
$ cat b64
iVBORw0KGgoAAAANSUhEUgAAABEAAAATCAIAAAD5x3GmAAAACXBIWXMAAAsSAAALEgHS3X78AAAA
sklEQVQokZ1SQQ7CMAyL3fz/lfwAicNgNYeMqEBTMXzYYY1ruzEu15skmwHA9L9v28OsF5yW15EE
wMB0+htBltR7d0nk3EPYS6mk+cJ3DOeppKA5yUpHwnh9fr21BszfzQypE0kOb601aS/CcEwS2SQ5
gGIg9X72/V6dxd5e7yDSSEr4dT+j+AlO2mFtbEIAAMD/0PGxHevRxCpPluUEp4L33uuO6sP50Tcz
iyItaPae6gkuMnIItElP1QAAAABJRU5ErkJggg==
$ base64 --decode b64 > png
$ sha256sum png
043a502edc956c6cc750a0182a6309b5e9c2a02d372f16990b9d9b5d414b494d png
$ rlwrap tclsh
% set tcl_patchLevel
8.6.12
% package require base64
2.5
% set data [base64::decode {
iVBORw0KGgoAAAANSUhEUgAAABEAAAATCAIAAAD5x3GmAAAACXBIWXMAAAsSAAALEgHS3X78AAAA
sklEQVQokZ1SQQ7CMAyL3fz/lfwAicNgNYeMqEBTMXzYYY1ruzEu15skmwHA9L9v28OsF5yW15EE
wMB0+htBltR7d0nk3EPYS6mk+cJ3DOeppKA5yUpHwnh9fr21BszfzQypE0kOb601aS/CcEwS2SQ5
gGIg9X72/V6dxd5e7yDSSEr4dT+j+AlO2mFtbEIAAMD/0PGxHevRxCpPluUEp4L33uuO6sP50Tcz
iyItaPae6gkuMnIItElP1QAAAABJRU5ErkJggg==}] ; list
% package require sha256
1.0.4
% sha2::sha256 -hex -- $data
043a502edc956c6cc750a0182a6309b5e9c2a02d372f16990b9d9b5d414b494d
% set middle [string range $data 1 end-1] ; list
% sha2::sha256 -hex -- $middle
1b886148980fddaa2f333c7e6ecad3f31d1385b831b17431ed93d2f9133cb760
% set fd [open middle {WRONLY CREAT TRUNC BINARY}]
file3
% puts -nonewline $fd $middle
% close $fd
% exit
$ sha256sum middle
1b886148980fddaa2f333c7e6ecad3f31d1385b831b17431ed93d2f9133cb760 middle
With 8.6.12 everything works as expected.
Thanks for checking this Rich. What I still am unsure of is how the
binary data string is created. If done with a base64::decode, then there can't be any codepoints > 0xFF but if the string is read in and the file
is not read as binary, then the data could be corrupted there.
In my code, I used -encoding iso8859-1, but after thinking some more, I think this could be -encoding binary. The only advantage of iso8859-1
would be that it should error if there are any codepoints >= 0xFF. I
don't know if -encoding binary does that too.
Also, there's [string bytelength $data] that can be used, which if >
[string length $data] then there are some bytes that are >= 0x80 and
need 2 bytes to encode them.
So, I'm still not sure why Saito was getting any errors on taking a
slice with string range.
"encoding binary" and "-encoding iso-8859-1" are identical.
The more important parameters are -translation and -eofchar
saito <saitology9@gmail.com> wrote:
set data [base64::decode {
iVBORw0KGgoAAAANSUhEUgAAABEAAAATCAIAAAD5x3GmAAAACXBIWXMAAAsSAAALEgHS3X78AAAA >> sklEQVQokZ1SQQ7CMAyL3fz/lfwAicNgNYeMqEBTMXzYYY1ruzEu15skmwHA9L9v28OsF5yW15EE >> wMB0+htBltR7d0nk3EPYS6mk+cJ3DOeppKA5yUpHwnh9fr21BszfzQypE0kOb601aS/CcEwS2SQ5 >> gGIg9X72/V6dxd5e7yDSSEr4dT+j+AlO2mFtbEIAAMD/0PGxHevRxCpPluUEp4L33uuO6sP50Tcz >> iyItaPae6gkuMnIItElP1QAAAABJRU5ErkJggg==}]
$ cat b64
iVBORw0KGgoAAAANSUhEUgAAABEAAAATCAIAAAD5x3GmAAAACXBIWXMAAAsSAAALEgHS3X78AAAA
sklEQVQokZ1SQQ7CMAyL3fz/lfwAicNgNYeMqEBTMXzYYY1ruzEu15skmwHA9L9v28OsF5yW15EE
wMB0+htBltR7d0nk3EPYS6mk+cJ3DOeppKA5yUpHwnh9fr21BszfzQypE0kOb601aS/CcEwS2SQ5
gGIg9X72/V6dxd5e7yDSSEr4dT+j+AlO2mFtbEIAAMD/0PGxHevRxCpPluUEp4L33uuO6sP50Tcz
iyItaPae6gkuMnIItElP1QAAAABJRU5ErkJggg==
$ sha256sum middle
1b886148980fddaa2f333c7e6ecad3f31d1385b831b17431ed93d2f9133cb760 middle
With 8.6.12 everything works as expected.
On 2/16/2026 9:07 PM, Rich wrote:
So, I'm still not sure why Saito was getting any errors on taking a
slice with string range.
However, the new file ends up corrupted. It has the same number of bytes as the original, and I can confirm they are also identical content-wise by opening them in an editor.
On 2/17/2026 12:07 AM, Rich wrote:
saito <saitology9@gmail.com> wrote:
set data [base64::decode {
iVBORw0KGgoAAAANSUhEUgAAABEAAAATCAIAAAD5x3GmAAAACXBIWXMAAAsSAAALEgHS3X78AAAA
sklEQVQokZ1SQQ7CMAyL3fz/lfwAicNgNYeMqEBTMXzYYY1ruzEu15skmwHA9L9v28OsF5yW15EE
wMB0+htBltR7d0nk3EPYS6mk+cJ3DOeppKA5yUpHwnh9fr21BszfzQypE0kOb601aS/CcEwS2SQ5
gGIg9X72/V6dxd5e7yDSSEr4dT+j+AlO2mFtbEIAAMD/0PGxHevRxCpPluUEp4L33uuO6sP50Tcz
iyItaPae6gkuMnIItElP1QAAAABJRU5ErkJggg==}]
-a-a $ cat b64
-a-a iVBORw0KGgoAAAANSUhEUgAAABEAAAATCAIAAAD5x3GmAAAACXBIWXMAAAsSAAALEgHS3X78AAAA
-a-a sklEQVQokZ1SQQ7CMAyL3fz/lfwAicNgNYeMqEBTMXzYYY1ruzEu15skmwHA9L9v28OsF5yW15EE
-a-a wMB0+htBltR7d0nk3EPYS6mk+cJ3DOeppKA5yUpHwnh9fr21BszfzQypE0kOb601aS/CcEwS2SQ5
-a-a gGIg9X72/V6dxd5e7yDSSEr4dT+j+AlO2mFtbEIAAMD/0PGxHevRxCpPluUEp4L33uuO6sP50Tcz
-a-a iyItaPae6gkuMnIItElP1QAAAABJRU5ErkJggg==
-a-a $ sha256sum middle
-a-a 1b886148980fddaa2f333c7e6ecad3f31d1385b831b17431ed93d2f9133cb760-a middle
With 8.6.12 everything works as expected.
The issue is that the final image, after the data is written to a file, is corrupted. Can you open that image file and see it? For me, if I try to open from a File Epxlorer, it can't. Even the small preview image is messed up. Some image programs may open it but after complaining and with a lot of distortions.
Are you saying that after taking a slice of that 256 bytes and writing
that out you can't display the picture any longer?
On 2/17/2026 12:07 AM, Rich wrote:
saito <saitology9@gmail.com> wrote:
set data [base64::decode {
iVBORw0KGgoAAAANSUhEUgAAABEAAAATCAIAAAD5x3GmAAAACXBIWXMAAAsSAAALEgHS3X78AAAA
sklEQVQokZ1SQQ7CMAyL3fz/lfwAicNgNYeMqEBTMXzYYY1ruzEu15skmwHA9L9v28OsF5yW15EE
wMB0+htBltR7d0nk3EPYS6mk+cJ3DOeppKA5yUpHwnh9fr21BszfzQypE0kOb601aS/CcEwS2SQ5
gGIg9X72/V6dxd5e7yDSSEr4dT+j+AlO2mFtbEIAAMD/0PGxHevRxCpPluUEp4L33uuO6sP50Tcz
iyItaPae6gkuMnIItElP1QAAAABJRU5ErkJggg==}]
$ cat b64
iVBORw0KGgoAAAANSUhEUgAAABEAAAATCAIAAAD5x3GmAAAACXBIWXMAAAsSAAALEgHS3X78AAAA
sklEQVQokZ1SQQ7CMAyL3fz/lfwAicNgNYeMqEBTMXzYYY1ruzEu15skmwHA9L9v28OsF5yW15EE
wMB0+htBltR7d0nk3EPYS6mk+cJ3DOeppKA5yUpHwnh9fr21BszfzQypE0kOb601aS/CcEwS2SQ5
gGIg9X72/V6dxd5e7yDSSEr4dT+j+AlO2mFtbEIAAMD/0PGxHevRxCpPluUEp4L33uuO6sP50Tcz
iyItaPae6gkuMnIItElP1QAAAABJRU5ErkJggg==
$ sha256sum middle
1b886148980fddaa2f333c7e6ecad3f31d1385b831b17431ed93d2f9133cb760 middle >>
With 8.6.12 everything works as expected.
The issue is that the final image, after the data is written to a file,
is corrupted. Can you open that image file and see it? For me, if I try
to open from a File Epxlorer, it can't. Even the small preview image is messed up. Some image programs may open it but after complaining and
with a lot of distortions.
On 2/17/2026 3:10 PM, et99 wrote:
Are you saying that after taking a slice of that 256 bytes and writing
that out you can't display the picture any longer?
Yes, it is corrupted.
I made a page on this. Hopefully someone can shed some light on it and
help solve it.
saito <saitology9@gmail.com> wrote:
On 2/17/2026 12:07 AM, Rich wrote:
saito <saitology9@gmail.com> wrote:
set data [base64::decode {
iVBORw0KGgoAAAANSUhEUgAAABEAAAATCAIAAAD5x3GmAAAACXBIWXMAAAsSAAALEgHS3X78AAAA
sklEQVQokZ1SQQ7CMAyL3fz/lfwAicNgNYeMqEBTMXzYYY1ruzEu15skmwHA9L9v28OsF5yW15EE
wMB0+htBltR7d0nk3EPYS6mk+cJ3DOeppKA5yUpHwnh9fr21BszfzQypE0kOb601aS/CcEwS2SQ5
gGIg9X72/V6dxd5e7yDSSEr4dT+j+AlO2mFtbEIAAMD/0PGxHevRxCpPluUEp4L33uuO6sP50Tcz
iyItaPae6gkuMnIItElP1QAAAABJRU5ErkJggg==}]
$ cat b64
iVBORw0KGgoAAAANSUhEUgAAABEAAAATCAIAAAD5x3GmAAAACXBIWXMAAAsSAAALEgHS3X78AAAA
sklEQVQokZ1SQQ7CMAyL3fz/lfwAicNgNYeMqEBTMXzYYY1ruzEu15skmwHA9L9v28OsF5yW15EE
wMB0+htBltR7d0nk3EPYS6mk+cJ3DOeppKA5yUpHwnh9fr21BszfzQypE0kOb601aS/CcEwS2SQ5
gGIg9X72/V6dxd5e7yDSSEr4dT+j+AlO2mFtbEIAAMD/0PGxHevRxCpPluUEp4L33uuO6sP50Tcz
iyItaPae6gkuMnIItElP1QAAAABJRU5ErkJggg==
$ sha256sum middle
1b886148980fddaa2f333c7e6ecad3f31d1385b831b17431ed93d2f9133cb760 middle
With 8.6.12 everything works as expected.
The issue is that the final image, after the data is written to a file,
is corrupted. Can you open that image file and see it? For me, if I try
to open from a File Epxlorer, it can't. Even the small preview image is
messed up. Some image programs may open it but after complaining and
with a lot of distortions.
The original base64 data was a tiny png image. It opened in an image
viewer just fine.
My "slice" was just taking everything minus the very first and very
last byte. That is no longer a valid png image. However what
sha256sum found as the hash while still in Tcl's binary string matched
what was written to the file.
Is there a reason you are trying to "clip" pieces out of a binary
string? Do you have a series of concatenated png's and you are trying
to separate them? Then perhaps your indexes are not exactly where they
need to be to extract all the bytes necessary to produce a valid
output?
Is there a reason you are trying to "clip" pieces out of a binary
string?-a Do you have a series of concatenated png's and you are trying
to separate them?-a Then perhaps your indexes are not exactly where they
need to be to extract all the bytes necessary to produce a valid
output?
On 2/17/2026 8:11 PM, Rich wrote:
saito <saitology9@gmail.com> wrote:
On 2/17/2026 12:07 AM, Rich wrote:
saito <saitology9@gmail.com> wrote:
set data [base64::decode {
iVBORw0KGgoAAAANSUhEUgAAABEAAAATCAIAAAD5x3GmAAAACXBIWXMAAAsSAAALEgHS3X78AAAA
sklEQVQokZ1SQQ7CMAyL3fz/lfwAicNgNYeMqEBTMXzYYY1ruzEu15skmwHA9L9v28OsF5yW15EE
wMB0+htBltR7d0nk3EPYS6mk+cJ3DOeppKA5yUpHwnh9fr21BszfzQypE0kOb601aS/CcEwS2SQ5
gGIg9X72/V6dxd5e7yDSSEr4dT+j+AlO2mFtbEIAAMD/0PGxHevRxCpPluUEp4L33uuO6sP50Tcz
iyItaPae6gkuMnIItElP1QAAAABJRU5ErkJggg==}]
$ cat b64
iVBORw0KGgoAAAANSUhEUgAAABEAAAATCAIAAAD5x3GmAAAACXBIWXMAAAsSAAALEgHS3X78AAAA
sklEQVQokZ1SQQ7CMAyL3fz/lfwAicNgNYeMqEBTMXzYYY1ruzEu15skmwHA9L9v28OsF5yW15EE
wMB0+htBltR7d0nk3EPYS6mk+cJ3DOeppKA5yUpHwnh9fr21BszfzQypE0kOb601aS/CcEwS2SQ5
gGIg9X72/V6dxd5e7yDSSEr4dT+j+AlO2mFtbEIAAMD/0PGxHevRxCpPluUEp4L33uuO6sP50Tcz
iyItaPae6gkuMnIItElP1QAAAABJRU5ErkJggg==
$ sha256sum middle
1b886148980fddaa2f333c7e6ecad3f31d1385b831b17431ed93d2f9133cb760 middle
With 8.6.12 everything works as expected.
The issue is that the final image, after the data is written to a file,
is corrupted. Can you open that image file and see it? For me, if I try
to open from a File Epxlorer, it can't. Even the small preview image is
messed up. Some image programs may open it but after complaining and
with a lot of distortions.
The original base64 data was a tiny png image. It opened in an image
viewer just fine.
My "slice" was just taking everything minus the very first and very
last byte. That is no longer a valid png image. However what
sha256sum found as the hash while still in Tcl's binary string matched
what was written to the file.
Is there a reason you are trying to "clip" pieces out of a binary
string? Do you have a series of concatenated png's and you are trying
to separate them? Then perhaps your indexes are not exactly where they
need to be to extract all the bytes necessary to produce a valid
output?
This is what has confused me.
If Saito is trying to extract portions of a png file, to somehow
display them, this won't work.
If some picture viewers are actually able to display something, it's
likely they are treating the data as raw.
The old photoshop used to do this when it only had a raw string of
bytes.
But if any of the png data is compressed, there's no way
treating it as raw pixels should display anything meaningful.
And even when it doesn't compress, it still has filters and even uncompressed blocks have header bytes.
Bottom line: He needs to decode the PNG first, then extract pixels
and create a new image to output, not extract PNG file bytes.
On 2/17/2026 8:41 PM, et99 wrote:
Is there a reason you are trying to "clip" pieces out of a binary
string?-a Do you have a series of concatenated png's and you are trying
to separate them?-a Then perhaps your indexes are not exactly where they
need to be to extract all the bytes necessary to produce a valid
output?
Ah, this could be it. But with png files, one needs to skip over
each chunk until the end of each png, or at least scan for the iend
chunk, which is probably safely unique, but not guaranteed. And then
there could be junk data between the pngs, so you'd need to find the
next signature for the start of the next one.
et99 <et99@rocketship1.me> wrote:
This is what has confused me.
Not just you. Whatever Saito is doing, it has been massively
underspecified here to the point that it is hard to help.
In general, yes. One cannot simply [string range ...] on a PNG and get
back a valid image.
saito <saitology9@gmail.com> wrote:
On 2/17/2026 3:10 PM, et99 wrote:
Are you saying that after taking a slice of that 256 bytes and writing
that out you can't display the picture any longer?
Yes, it is corrupted.
That would have been helpful to have mentioned in the article where you posted the base64 encoding.
I made a page on this. Hopefully someone can shed some light on it and
help solve it.
We can't read your mind. Where's "the page"?
On 2/17/2026 11:27 PM, Rich wrote:
saito <saitology9@gmail.com> wrote:
On 2/17/2026 3:10 PM, et99 wrote:
Are you saying that after taking a slice of that 256 bytes and writing >>>> that out you can't display the picture any longer?
Yes, it is corrupted.
That would have been helpful to have mentioned in the article where you
posted the base64 encoding.
By "it", I mean the png file.
In the context of the question above, "you can't display the picture
any longer?"
I made a page on this. Hopefully someone can shed some light on it and
help solve it.
We can't read your mind. Where's "the page"?
Fair point. I meant a wiki page. There aren't that many places to get
help on Tcl.
On 2/18/2026 9:15 AM, Rich wrote:
et99 <et99@rocketship1.me> wrote:
This is what has confused me.
Not just you. Whatever Saito is doing, it has been massively
underspecified here to the point that it is hard to help.
Sorry to hear that. I believe I was pretty clear. In my first post, I
said this:
"I have some binary data in a large string. I get the binary data by selecting a sub-string of the large one with the correct start and end indexes."
In general, yes. One cannot simply [string range ...] on a PNG and get
back a valid image.
I never said anything about parsing png's or clipping png's or
anything like that.
I see that et99 guessed that this *might* be the case. It was just a
guess, no need to treat it as fact or as if I said it.
Sadly, the wiki is thousands of pages. A URL of the actual page is
what would be helpful to us here who can't read your mind to know which
page you created.
On 2/18/2026 2:59 PM, Rich wrote:
Sadly, the wiki is thousands of pages. A URL of the actual page is
what would be helpful to us here who can't read your mind to know which
page you created.
It is listed on the main page. It is a new page so it shows up under Recently changed. It is titled "Help Tclhttpd".
I don't have the tools you have nor the familiarity with them. So I
really hope that you could identify what the issue is.
I trust the string because it is the query string.
Tcl httpd works from this same string and is able to create a proper
png file from the binary part of it. However, that method doesn't
work for me for a few reasons, one being not providing the type of
the file.
If we can fix the proc on that page as described, that will be the ideal solution. I see that it does a lot of regsubs and reverses them later.
Based on your statement that the string is indeed corrupted, I suspect
that the reverse regsub's have not been applied yet. But I am not sure
where or how it happens so I can fix it.
| Sysop: | Amessyroom |
|---|---|
| Location: | Fayetteville, NC |
| Users: | 59 |
| Nodes: | 6 (0 / 6) |
| Uptime: | 19:53:09 |
| Calls: | 812 |
| Calls today: | 2 |
| Files: | 1,287 |
| D/L today: |
20 files (23,248K bytes) |
| Messages: | 210,075 |