Function to process bytes in a string?
Moderator: DosItHelp
Function to process bytes in a string?
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.
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.
-
- Posts: 319
- Joined: 12 May 2006 01:13
Re: Function to process bytes in a string?
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.
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:
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
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.
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.
-
- Posts: 319
- Joined: 12 May 2006 01:13
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)
-
- Posts: 319
- Joined: 12 May 2006 01:13
-
- Posts: 319
- Joined: 12 May 2006 01:13
good luck searching for your answer, that's what i meantisacmahad wrote:Why good luck?
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.
-
- Expert
- Posts: 391
- Joined: 19 Mar 2009 08:47
- Location: Iowa
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.
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.
Ok here is the complete function as of now.
Is there a better more proficient way to do
the code below.
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
-
- Posts: 319
- Joined: 12 May 2006 01:13
-
- Posts: 319
- Joined: 12 May 2006 01:13
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.
-
- Posts: 319
- Joined: 12 May 2006 01:13
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.