Function to process bytes in a string?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
isacmahad
Posts: 44
Joined: 29 Oct 2009 23:08

Function to process bytes in a string?

#1 Post by isacmahad » 29 Oct 2009 23:16

Hi,
Im looking for a few different ideas on how one might process a string byte by byte. I know of a couple ways problem being is that my functions don't process white spaces at the end of the string only between tokens. Any help is greatly appreciated.
Thnx.

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

Re: Function to process bytes in a string?

#2 Post by ghostmachine4 » 29 Oct 2009 23:52

isacmahad wrote:Hi,
Im looking for a few different ideas on how one might process a string byte by byte. I know of a couple ways problem being is that my functions don't process white spaces at the end of the string only between tokens. Any help is greatly appreciated.
Thnx.

show the bigger picture. Are you processing a file? show examples of input and output.

isacmahad
Posts: 44
Joined: 29 Oct 2009 23:08

#3 Post by isacmahad » 30 Oct 2009 12:12

Thank you for the reply.
So for example, the strings are in a file.
I have the way to grab each string line by line now I need to process each byte,so in say, FILE.txt there is.

Line 1 This is a string Hello world
Line2 The quick brown fox jumped over the lazy dog.

Now we need to get each byte in all the tokens and all the whites spaces and then compare them as we go along.
So some sort of function to get byte L then i then n then e etc...
And a block of if statements to compare them to so if tokens Line = Line check byte 1 or 2. This needs to be done on a line per line bases not as a whole.

Code Snippet:

Code: Select all

@echo off
 setlocal

:: BEGIN_FUNCTION

   set val=1
   :BeginLoop
   set Str=Hello World.
   call set "StrByte=%%Str:~0,%val%%%"
   echo.%StrByte%
   if /i "%StrByte%"=="%Str%" (
   goto :Script
   )
   set /a val=%val%+1
   goto :BeginLoop

:: END_FUNCTION

:Script
echo.String=%Str%
echo.Bytes=%val%


Just adds 1 to the value as it goes along, so if there was an if statement that said if %strbyte%==w echo.found w
would not work because at the given time the actual string is Hello W not just W
The above produces output like so,
H
He
Hel
Hell
Hello
Hello
Hello W
Hello Wo
Hello Wor
Hello Worl
Hello World
Hello World.
String=Hello World.
Bytes=12
-----------------------------------------
Where as it should produce like so,
H
e
l
l
o

W
o
r
l
d
.
String=Hello World.
Bytes=12

So to solve this problem I do this

Code: Select all

   set val=1
   :BeginLoop1
   set Str1=Hello World.
   call set "StrByte=%%Str1:~0,%val%%%"
   set Str2=%StrByte%
   call set "StrChar=%Str2:~-1%"
   if /i "%StrChar%"=="W" echo.Found W
   echo.%StrChar%
   if /i "%StrByte%"=="%Str1%" (
   goto :Script
   )
   set /a val=%val%+1
   goto :BeginLoop1

Which gives me the desired result.
Now I am wondering if there are some other work arounds for accomplishing this as in a neater nicer function, or just some other ideas.

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

#4 Post by ghostmachine4 » 30 Oct 2009 20:25

isacmahad wrote:Now I am wondering if there are some other work arounds for accomplishing this as in a neater nicer function, or just some other ideas.

so eventually you just want to find the length of a string? If you can download and use gawk: http://gnuwin32.sourceforge.net/packages/gawk.htm

Code: Select all

c:\test> echo hello world | gawk '{print length}'


Use better tools for processing strings/bytes/file manipulation, not batch(cmd.exe)

isacmahad
Posts: 44
Joined: 29 Oct 2009 23:08

#5 Post by isacmahad » 31 Oct 2009 20:38

Thank you for the reply, but I would like to keep this in pure batch otherwise it defeats the purpose.

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

#6 Post by ghostmachine4 » 31 Oct 2009 23:26

isacmahad wrote:Thank you for the reply, but I would like to keep this in pure batch otherwise it defeats the purpose.

good luck to you then.

isacmahad
Posts: 44
Joined: 29 Oct 2009 23:08

#7 Post by isacmahad » 01 Nov 2009 15:42

Why good luck?
The code I posted above does this.
You act like it can't be done.
I never said it had to be easy, just something different, maybe in the form of a for loop. Obviously if it was meant to be easy I would just switch over to c++. Seems like a rather lazy answer, but thnx anyways. :roll:

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

#8 Post by ghostmachine4 » 01 Nov 2009 19:25

isacmahad wrote:Why good luck?
good luck searching for your answer, that's what i meant

You act like it can't be done.

that's your assumption. I didn't say it can't be done. If i really want to comment on that, i would only say its laborious to do, but it can be done.

Obviously if it was meant to be easy I would just switch over to c++.

if you want it easy, C++ isn't the right tool to use. languages like gawk/Perl/Python makes it easy for you for string parsing. Even the natively installed Vbscript has len() function to find length of string for you.

avery_larry
Expert
Posts: 391
Joined: 19 Mar 2009 08:47
Location: Iowa

#9 Post by avery_larry » 02 Nov 2009 11:16

Clarify a little more what you're trying to do?

If a line contains a certain string, then you want to echo the entire line and its length?

If that's what you mean, then you don't need to compare byte by byte. First do a find function on the entire line, and if it finds the string you want, then you can echo the line and calculate the length.

isacmahad
Posts: 44
Joined: 29 Oct 2009 23:08

#10 Post by isacmahad » 02 Nov 2009 15:22

Ok here is the complete function as of now.
Is there a better more proficient way to do
the code below.

Code: Select all

@echo off
 setlocal

:: BEGIN_FUNCTION
set MAINStr=Hel $lo $Wor $ld

   set BYTEval=0
   set NEXTval=0
   :BeginBYTELoop
   set /a BYTEval=%BYTEval%+1
   call set "StrByte=%%MAINStr:~0,%BYTEval%%%"
   set "StrChar=%StrByte:~-1%"
   if "%BYTE%"=="1" goto :NXTByte
   if /i "%StrChar%"=="$" set BYTE=1&&goto :BeginBYTELoop
   if /i "%StrByte%"=="%MAINStr%" goto :eof
   goto :BeginBYTELoop

   :NXTByte
   set /a NEXTval=%NEXTval%+1
   if "%StrChar%"==" " (
      call set "SUBStr=%%StrByte:~-%NEXTval%%%"
      call set "BYTE="
      call :FoundString
      call set NEXTval=0
   )
   goto :BeginBYTELoop

   :FoundString
   for /f "tokens=1 delims= " %%- in ("%SUBStr%") do (
   echo.%%-
   )
   goto :eof
:: END_FUNCTION

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

#11 Post by ghostmachine4 » 02 Nov 2009 18:12

isacmahad wrote:Ok here is the complete function as of now.
Is there a better more proficient way to do

of course there are, but not with pure cmd.exe

isacmahad
Posts: 44
Joined: 29 Oct 2009 23:08

#12 Post by isacmahad » 02 Nov 2009 19:11

And why not?
Is there not a way to stuff this all into a for loop?

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

#13 Post by ghostmachine4 » 02 Nov 2009 19:37

isacmahad wrote:And why not?

because batch(cmd.exe) is never meant to be a good text/string processing tool, perod

isacmahad
Posts: 44
Joined: 29 Oct 2009 23:08

#14 Post by isacmahad » 03 Nov 2009 00:02

ghostmachine4 wrote:
isacmahad wrote:And why not?

because batch(cmd.exe) is never meant to be a good text/string processing tool, perod

Ok so lets build one.
With a little bit of work I'm sure this could be accomplished using pure ("CMD.EXE"). If not to disagree at least agree on what we disagree on.
No? And if your going to quote me and respond with the suspected "NO" don't bother responding. :P

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

#15 Post by ghostmachine4 » 03 Nov 2009 00:44

isacmahad wrote:
ghostmachine4 wrote:
isacmahad wrote:And why not?

because batch(cmd.exe) is never meant to be a good text/string processing tool, perod

With a little bit of work I'm sure this could be accomplished using pure ("CMD.EXE").

I have already provided you with one solution. And that solution takes me less than 10 seconds to come up with. you can spend your time crafting a batch solution, that's fine with me. (btw, i NEVER said that you CAN'T do it with pure batch, i just said its troublesome). But i live in the real world, where my time is precious. You may not have this experience yet, but that's true for me. I never use tools that doesn't provide me what i need to get the job done, easily in terms of development speed as well as code maintainability.

you can wait for your batch solution, otherwise, i suggest you move on and learn to use something better.

Post Reply