convert number

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
denday
Posts: 10
Joined: 16 Dec 2011 02:19

convert number

#1 Post by denday » 11 Jan 2012 03:24

hi
I have an 8 bit address of a variable and then want to turn it into a decimal variable address

example "source.txt"

Code: Select all

0.0,Some-Variable
0.1,Some-Variable
0.2,Some-Variable
0.3,Some-Variable
0.4,Some-Variable
0.5,Some-Variable
0.6,Some-Variable
0.7,Some-Variable
1.0,Some-Variable
1.2,Some-Variable
1.5,Some-Variable
1.6,Some-Variable
1.7,Some-Variable

my batch program

Code: Select all

@echo off
set inFile="source.txt"
set outFile="result.csv"
set cnt=0
(
   for /f "usebackq tokens=1-3 delims=.," %%A in (%inFile%) do (
   set /a "cnt=(%%A*8)+(%%B+201)"
   call echo %%cnt%%,%%C,#
   )
)>%outFile%

the results of my program "result.csv"

Code: Select all

201,Some-Variable,#
202,Some-Variable,#
203,Some-Variable,#
204,Some-Variable,#
205,Some-Variable,#
206,Some-Variable,#
207,Some-Variable,#
208,Some-Variable,#
209,Some-Variable,#
211,Some-Variable,#
214,Some-Variable,#
215,Some-Variable,#
216,Some-Variable,#

what should I add to my batch program so that the expected outcomes are as follows:

Code: Select all

201,Some-Variable,#
202,Some-Variable,#
203,Some-Variable,#
204,Some-Variable,#
205,Some-Variable,#
206,Some-Variable,#
207,Some-Variable,#
208,Some-Variable,#
209,Some-Variable,#
210,Null,#
211,Some-Variable,#
212,Null,#
213,Null,#
214,Some-Variable,#
215,Some-Variable,#
216,Some-Variable,#

thanks for the time you spend.

trebor68
Posts: 146
Joined: 01 Jul 2011 08:47

Re: convert number

#2 Post by trebor68 » 11 Jan 2012 14:00

Here the complete source.txt file:

Code: Select all

0.0,Some-Variable
0.1,Some-Variable
0.2,Some-Variable
0.3,Some-Variable
0.4,Some-Variable
0.5,Some-Variable
0.6,Some-Variable
0.7,Some-Variable
1.0,Some-Variable
1.1,Some-Variable
1.2,Some-Variable
1.3,Some-Variable
1.4,Some-Variable
1.5,Some-Variable
1.6,Some-Variable
1.7,Some-Variable


Here the code:

Code: Select all

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
set inFile="source.txt"
set outFile="result.csv"
set cnt=0
(
   for /f "usebackq tokens=1-3 delims=.," %%A in (%inFile%) do (
   set /a "cnt=%%A*8+%%B"
   set /a "cnt2=cnt+201"
   set var=%%C
   if !cnt!==9 set var=Null
   if !cnt!==11 set var=Null
   if !cnt!==12 set var=Null
   call echo !cnt2!,!var!,#
   )
)>%outFile%

Aacini
Expert
Posts: 1927
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: convert number

#3 Post by Aacini » 11 Jan 2012 17:15

Code: Select all

@echo off
setlocal EnableDelayedExpansion
set inFile="source.txt"
set outFile="result.csv"
set cnt=0
set last=201
(
   for /f "usebackq tokens=1-3 delims=.," %%A in (%inFile%) do (
   set /a "cnt=(%%A*8)+(%%B+201), cntM1=cnt-1"
   for /L %%i in (!last!,1,!cntM1!) do echo %%i,Null,#
   echo !cnt!,%%C,#
   set /A last=cnt+1
   )
)>%outFile%

denday
Posts: 10
Joined: 16 Dec 2011 02:19

Re: convert number

#4 Post by denday » 11 Jan 2012 20:28

@trebor68
Your script is correct but that's not what I need.
thanks for the time you spent.

@Aacini
This script is perfect for my needs.
I am very grateful for your help.

have a nice day
-denDay

Post Reply