I would want to scan binary string by following commands:
set point_file_fileID [open $point_file_full_name r]
fconfigure $point_file_fileID -translation binary -encoding binary
set line [read $point_file_fileID 52]
set ret [binary scan $line "iiiissica21a5" \
PtSWLongitude \
PtSWLatitude \
PtNELongitude \
PtNELatitude \
PtLine \
PtIndex \
PtInt \
PtType \
PtComName \
PtShort ]
The problem is that there are NULL-character (0x00) in data regarding to
PtComName and PtShort.
This NULL-character can be in various positions. I do not want anything
after NULL-character.
How can i detect NULL-character correctly and how can i discard characters
after NULL-character ?
I think that code above should work, but it does not.
---
Esa
>How can i detect NULL-character correctly and how can i discard characters
>after NULL-character ?
Hi Esa,
you can detect a NULL-character via
set pos [string first \x00 $line]
and then delimit your string via
if { $pos =! -1 } {
set line [string range $line 0 [incr pos -1]]
}
HTH
Helmut Giese
set PtComName [string range $PtComName 0 [expr {[string first "\0"
$PtComName]-1}]]
Regards,
Arjen
64 % regsub {(.+\x00).+} "foo-\x00-bar" {\1}
foo-
Regards,
Arjen
---
Esa
"Helmut Giese" <hgi...@ratiosoft.com> wrote in message
news:43e75aa6...@News.Individual.DE...
Use the 'A' format to scan the strings and not the 'a' format.
Donal.
In the given context, this probably isn't it, since A${n} might
consume fewer than $n bytes from input, thus offsetting the start
of all following elements.
Actually, I tested before making that comment, and it works. Witness:
% set s [binary format a10c abc 100]; string length $s
11
% binary scan $s a10c x y
2
% string length $x
10
% string length $y
3
% set y
100
% unset x y
% binary scan $s A10c x y
2
% set x
abc
% string length $x
3
% set y
100
As you can see, the 'A' specifier is exactly right. :-)
Donal.
ok, ok, I misread the doku (which is actually very clear on that, saying
that the *resulting* string is stripped - I don't know how I could ever
misunderstand it. I guess I'd have used it a couple of times already if
it were not for the misunderstanding)
> As you can see, the 'A' specifier is exactly right. :-)
Not exactly :-) The OP asked for removal of anything starting
with the first \0 not for a [string trim ... " \0"]-effect.
Even if we assume that \0's appear only towards the end,
it will still also strip leading and trailing blanks,
which the OP didn't ask for.
Bet it's what they really want though, even if they don't know it yet.
;-)
Donal.
uwe