AnsweredAssumed Answered

Conversion binary to string

Question asked by VRFuser on Jun 24, 1997
Gonzalo Jimenez <gjim@mdr.indra-espacio.es> wrote:
> Hi all again,
>
> this is another question about converting an integer to a string...in
> this case I would like to know if it is possible to convert a binary
> number to a string as a sequence of "0"'s and  "1"'s. Like the function
> bits("str") but just the opposite.
>
> Thank you very much in advance and regards,
>
> Gonzalo Jimenez
>
> INDRA Espacio S.A.
> <gjim@mdr.indra-espacio.es>

Here's the function I use for this. I'm sending as a user object,
but would suggest changing to user function and then including
in your local library.
Inputs are the value and a number of nybbles.

Stan Bischof
stanb@sr.hp.com

(saveFormat "2.3")
(date "Tue 24/Jun/1997 07:49:31 PDT")
(veerev "4.0")
(platform "HP9000s700")
(execMode fast)
(filterNAN 0)
(device 0 ROOTCONTEXT
(deviceList
(component 0 CONTEXT
(properties
(name "toBinaryN")
(trigMode deg)
(nextID 29))
(interface
(input 1
(name "A")
(optional yes))
(input 2
(name "n")
(optional yes))
(output 1
(type data)
(name "X")
(lock constraints)
(optional yes)))
(deviceList
(device 0 IFTHENELSE
(properties
(name "check range")
(cases 2 "a>(2^(n-1)-1)" "a<( -2^(n-1))"))
(interface
(input 1
(name "A")
(optional yes))
(input 2
(name "n")
(optional yes))
(output 1
(name "Then")
(lock name constraints))
(output 2
(name "Else If")
(lock name constraints))
(output 3
(name "Else")
(lock name constraints))))
(device 1 LITERALCONSTANT
(properties
(name "overflow"))
(interface
(output 1
(name "Text")
(lock name constraints)))
(implementation
(value Text
(data  "overflow"))
(initValue Text
(data  ""))))
(device 2 LITERALCONSTANT
(properties
(name "underflow"))
(interface
(output 1
(name "Text")
(lock name constraints)))
(implementation
(value Text
(data  "underflow"))
(initValue Text
(data  ""))))
(device 3 JUNCTION
(properties
(name "JCT"))
(interface
(input 1
(name "A")
(lock name constraints))
(input 2
(name "B")
(lock name constraints))
(input 3
(name "C")
(optional yes))
(output 1
(name "Data")
(lock name constraints))))
(device 7 TOSTRING
(properties
(transactions 1 "WRITE TEXT a HEX EOL"))
(interface
(input 1
(name "A")
(optional yes))
(output 1
(name "result")
(lock name constraints)))
(implementation
(attr iopath string write "string"
(readTerm "
")
(fs " ")
(eol "
")
(multiField fullSyntax)
(arrayFormat block))))
(device 8 FROMSTRING
(properties
(transactions 1 "READ TEXT x REAL"))
(interface
(input 1
(name "AString")
(requires
(datatype Text))
(lock name constraints))
(output 1
(name "X")
(optional yes)))
(implementation
(attr iopath string read "string"
(readTerm "
")
(fs " ")
(eol "
")
(multiField fullSyntax)
(arrayFormat block))))
(device 9 FORMULA
(properties
(expr 1 "asText(a)"))
(interface
(input 1
(name "A")
(optional yes))
(output 1
(name "Result")
(tag "Result")
(lock name constraints)
(optional yes))))
(device 12 LABEL
(properties
(name "turn input into integer value")
(labelValue "turn input into integer value"))
(implementation))
(device 14 FORMULA
(properties
(name "round(x)")
(expr 1 "round(x)"))
(interface
(input 1
(name "X")
(optional yes))
(output 1
(name "Result")
(tag "Result")
(lock name constraints)
(optional yes))))
(device 15 LABEL
(properties
(name "n is number of nybbles")
(labelValue "n is number of nybbles"))
(implementation))
(device 16 LABEL
(properties
(name "Turn HEX char into index")
(labelValue "Turn HEX char into index"))
(implementation))
(device 17 LABEL
(properties
(name "Turn index into binary string")
(labelValue "Turn index into binary string"))
(implementation))
(device 18 ACCUMULATOR
(interface
(input 1
(name "Data")
(tag "InData")
(requires
(shape "Scalar"))
(lock name constraints))
(output 1
(name "Data")
(tag "accumData")
(lock name constraints))))
(device 19 LITERALCONSTANT
(properties
(name "binary srings")
(sizeFixed 1)
(isSizeFixed 1))
(interface
(output 1
(name "Text")
(lock name constraints)))
(implementation
(value Text
(numDims 1)
(size 16)
(data  [ "0000" "0001" "0010" "0011" "0100" "0101" "0110" "0111" "1000" "1001"
"1010" "1011" "1100" "1101" "1110" "1111" ]))
(initValue Text
(data  ""))))
(device 20 FORMULA
(properties
(expr 1 "a[n]"))
(interface
(input 1
(name "n")
(optional yes))
(input 2
(name "A")
(optional yes))
(output 1
(name "Result")
(tag "Result")
(lock name constraints)
(optional yes))))
(device 21 FROMSTRING
(properties
(transactions 1 "READ TEXT x HEX"))
(interface
(input 1
(name "AString")
(requires
(datatype Text))
(lock name constraints))
(output 1
(name "X")
(optional yes)))
(implementation
(attr iopath string read "string"
(readTerm "
")
(fs " ")
(eol "
")
(multiField fullSyntax)
(arrayFormat block))))
(device 22 FORMULA
(properties
(expr 1 "strFromLen(a, n, 1)"))
(interface
(input 1
(name "A")
(optional yes))
(input 2
(name "n")
(optional yes))
(output 1
(name "Result")
(tag "Result")
(lock name constraints)
(optional yes))))
(device 23 FORCOUNT
(properties
(count 3))
(interface
(input 1
(name "Count")
(tag "Count")
(requires
(datatype Int32)
(shape "Scalar"))
(lock constraints)
(optional yes)
(buffer YES))
(output 1
(name "Count")
(lock name constraints))))
(device 24 FORMULA
(properties
(name "strLen(str)")
(expr 1 "strLen(str)"))
(interface
(input 1
(name "str")
(optional yes))
(output 1
(name "Result")
(tag "Result")
(lock name constraints)
(optional yes))))
(device 26 FORMULA
(properties
(name "trim leading characters if too long")
(expr 1 "(strLen(a)>n ? strFromLen(a, strLen(a)-n, n) : a)"))
(interface
(input 1
(name "A")
(optional yes))
(input 2
(name "n")
(optional yes))
(output 1
(name "Result")
(tag "Result")
(lock name constraints)
(optional yes))))
(device 27 FORMULA
(properties
(name "nybbles to bits")
(expr 1 "4*a"))
(interface
(input 1
(name "A")
(optional yes))
(output 1
(name "Result")
(tag "Result")
(lock name constraints)
(optional yes))))
(device 28 FORMULA
(properties
(name "pad with leading zeroes if too short")
(expr 1 "(strLen(a)<n ? (n-strLen(a))*"0"+a : a)"))
(interface
(input 1
(name "A")
(optional yes))
(input 2
(name "n")
(optional yes))
(output 1
(name "Result")
(tag "Result")
(lock name constraints)
(optional yes))))
(configuration
(connect D8:1 D0:1)
(connect D20:1 D0:2)
(connect D0:1 D1:0)
(connect D0:2 D2:0)
(connect D21:1 D3:1)
(connect D1:1 D3:2)
(connect D2:1 D3:3)
(connect D0:3 D4:0)
(connect D8:1 D4:1)
(connect D6:1 D5:1)
(connect I1:1 D6:1)
(connect D5:1 D8:1)
(connect D14:1 D12:1)
(connect D15:1 D14:1)
(connect D13:1 D14:2)
(connect D16:1 D15:1)
(connect D4:1 D16:1)
(connect D17:1 D16:2)
(connect D18:1 D17:1)
(connect D4:1 D18:1)
(connect D17:0 D19:0)
(connect D12:1 D19:1)
(connect D20:1 D19:2)
(connect I2:1 D20:1)
(connect D19:1 D21:1)
(connect D20:1 D21:2)
(connect D3:1 O1:1)))
(contextCarrier
(active detail)
(detail
(extent 998 506)
(anchorPt -11 388)
(configuration
(devCarrierFor 0
(active icon)
(icon
(extent 93 34))
(open
(extent 150 62))
(terminals on)
(pinCenter 0 0))
(devCarrierFor 1
(active icon)
(icon
(extent 69 15))
(open
(extent 105 28))
(pinCenter 160 -20))
(devCarrierFor 2
(active icon)
(icon
(extent 77 15))
(open
(extent 113 28))
(pinCenter 130 30))
(devCarrierFor 3
(active icon)
(icon
(extent 28 34))
(open
(extent 12 62))
(terminals on)
(pinCenter 860 30))
(devCarrierFor 7
(active icon)
(icon
(extent 71 15))
(open
(extent 279 74))
(terminals on)
(pinCenter 100 -300))
(devCarrierFor 8
(active icon)
(icon
(extent 92 15))
(open
(extent 279 74))
(terminals on)
(pinCenter -90 -300))
(devCarrierFor 9
(active icon)
(icon
(extent 64 15))
(open
(extent 154 31))
(terminals on)
(pinCenter -190 -300))
(devCarrierFor 12
(active open)
(icon
(extent 220 0))
(open
(extent 212 27)
(just l))
(title off)
(pinCenter -70 -350))
(devCarrierFor 14
(active icon)
(icon
(extent 63 15))
(open
(extent 94 31))
(terminals on)
(pinCenter 10 -300))
(devCarrierFor 15
(active open)
(icon
(extent 168 0))
(open
(extent 165 27)
(just l))
(title off)
(pinCenter -160 60))
(devCarrierFor 16
(active open)
(icon
(extent 191 0))
(open
(extent 186 27)
(just l))
(title off)
(pinCenter 420 -360))
(devCarrierFor 17
(active open)
(icon
(extent 220 0))
(open
(extent 212 27)
(just l))
(title off)
(pinCenter 560 -140))
(devCarrierFor 18
(active icon)
(icon
(extent 101 15))
(open
(extent 137 31))
(pinCenter 690 -200))
(devCarrierFor 19
(active icon)
(icon
(extent 97 15))
(open
(extent 133 264))
(pinCenter 430 -190))
(devCarrierFor 20
(active icon)
(icon
(extent 64 25))
(open
(extent 81 47))
(terminals on)
(pinCenter 570 -200))
(devCarrierFor 21
(active icon)
(icon
(extent 92 15))
(open
(extent 279 74))
(terminals on)
(pinCenter 540 -300))
(devCarrierFor 22
(active icon)
(icon
(extent 64 25))
(open
(extent 169 42))
(terminals on)
(pinCenter 430 -300))
(devCarrierFor 23
(active icon)
(icon
(extent 77 51)
(iconImage "loop.icn"))
(open
(extent 113 31))
(pinCenter 320 -250))
(devCarrierFor 24
(active icon)
(icon
(extent 81 15))
(open
(extent 110 31))
(terminals on)
(pinCenter 250 -300))
(devCarrierFor 26
(active icon)
(icon
(extent 270 25))
(open
(extent 364 42))
(terminals on)
(pinCenter 530 -80))
(devCarrierFor 27
(active icon)
(icon
(extent 111 15))
(open
(extent 80 31))
(terminals on)
(pinCenter -150 10))
(devCarrierFor 28
(active icon)
(icon
(extent 268 25))
(open
(extent 364 42))
(terminals on)
(pinCenter 640 -20))
(connect D8:1 D0:1
(points 6 44 -300 50 -300 50 -270 -70 -270 -70 -10 -49 -10))
(connect D20:1 D0:2
(points 2 -92 10 -49 10))
(connect D0:1 D1:0
(points 5 49 -10 70 -10 70 -50 160 -50 160 -30))
(connect D0:2 D2:0
(points 3 49 0 130 0 130 20))
(connect D21:1 D3:1
(points 4 776 -20 800 -20 800 20 843 20))
(connect D1:1 D3:2
(points 4 197 -20 220 -20 220 30 843 30))
(connect D2:1 D3:3
(points 4 171 30 190 30 190 40 843 40))
(connect D0:3 D4:0
(points 7 49 10 80 10 80 -60 160 -60 160 -330 100 -330 100 -310))
(connect D8:1 D4:1
(points 2 44 -300 62 -300))
(connect D6:1 D5:1
(points 2 -156 -300 -139 -300))
(connect I1:1 D6:1
(points 4 11 -266 -250 -266 -250 -300 -225 -300))
(connect D5:1 D8:1
(points 2 -42 -300 -24 -300))
(connect D14:1 D12:1
(points 2 604 -200 637 -200))
(connect D15:1 D14:1
(points 6 588 -300 610 -300 610 -270 530 -270 530 -210 535 -210))
(connect D13:1 D14:2
(points 2 481 -190 535 -190))
(connect D16:1 D15:1
(points 2 464 -300 491 -300))
(connect D4:1 D16:1
(points 6 138 -300 190 -300 190 -330 310 -330 310 -310 395 -310))
(connect D17:1 D16:2
(points 4 361 -250 380 -250 380 -290 395 -290))
(connect D18:1 D17:1
(points 6 293 -300 300 -300 300 -290 270 -290 270 -250 279 -250))
(connect D4:1 D18:1
(points 2 138 -300 207 -300))
(connect D17:0 D19:0
(points 4 320 -222 320 -110 530 -110 530 -95))
(connect D12:1 D19:1
(points 8 743 -200 760 -200 760 -230 380 -230 380 -210 360 -210 360 -90 392 -90)
)
(connect D20:1 D19:2
(points 4 -92 10 -80 10 -80 -70 392 -70))
(connect I2:1 D20:1
(points 4 11 -6 -283 -6 -283 10 -208 10))
(connect D19:1 D21:1
(points 6 667 -80 690 -80 690 -50 490 -50 490 -30 503 -30))
(connect D20:1 D21:2
(points 6 -92 10 -80 10 -80 -70 370 -70 370 -10 503 -10))
(connect D3:1 O1:1
(points 4 876 30 900 30 900 -136 1008 -136)))
(stackingOrder 7 5 8 9 2 10 13 3 4 18 17 15 0 19 12 11 1 6 14 16 20 21))))
(configuration))
(contextCarrier
(active detail)
(detail
(configuration
(devCarrierFor 0
(active icon)
(icon
(extent 72 25))
(pinCenter 674 668))))))


Outcomes