NB. This script contains J functions for various conversions NB. studied in MAT1101 -- Discrete Mathematics for Computing. NB. (Run the file in J to establish all functions listed.) NB. NB. ----------------- Contents ------------------------------ NB. NB.-------- Character string/Bit list conversions -------------- NB.---------------- ascii conversions -------------------------- NB.-------------- decimal/binary conversions ------------------- NB.---------------- binary/hex conversions --------------------- NB.--------------- decimal/hex conversions --------------------- NB.--- Computer representation of integers/signed numbers ------ NB.------- Floating point/real number conversions -------------- NB.------------- Number/Character conversions ------------------ NB. NB. NB. NB.-------- Character string/Bit list conversions -------------- L2s =: {&'01' NB. binary list to character string NB. st=: (#~ ' '&~:)@": s2L =: '1'&= NB. Binary list from character string NB. not for use when binary point is present ms2L =: 3 : 0 NB. Input is a character string of bits with a binary point NB. Output is a pair of boxed lists of numeric bits NB. for integral and fractional parts. NB. If there is no binary point, both boxes contain the same list p=. ('.'=y)#i.#y (:p)}.y ) NB.---------------- ascii conversions -------------------------- asciidec =: 3 : '(y = a.) # i. # a.' asciicode =: 3 : '_8 {.''0000000'',(d2b asciidec y)' displaycode=. 3 : '((>.(#y)%64),64)$(64*(>.(#y)%64)){.y' char2ascii =: 3 : ',> asciicode each y' ascii2char =: 3 : '(b2d"1 (((#y)%8),8)$ y){a.' NB.-------------- decimal/binary conversions ------------------- b2d =: 3 : 0 if. -.'.' e. y do. NB. If there is no binary point y =. y,'.' end. NB. add one to the end p =.('.'= y)#i.#y NB. location of binary point bits=. ;,. ms2L y NB. bit list ignoring point powers =. ,(<:p) -/ i.<:#y NB. string out the list of powers d =. +/ bits * 2^powers NB. add required powers ) d2b =: 3 : 0 }: 0 d2b y NB. the number of binary places is given by the left argument. NB. If no left argument is given it is taken to be 0 NB. The right argument is a decimal to be converted to binary : bits=. '' p=. 0 >. (<. 2 ^. y) NB. >. Is the greater of function for_k. i. p+x+1 do. if. y >: 2^ p-k do. bits =. bits, '1' y =. y - 2^p-k else. bits=. bits , '0' end. end. ((p+1) {. bits) , '.' , (p+1) }. bits NB. Output is a character bit string ) NB.---------------- binary/hex conversions --------------------- b2hex =: 3 : 0 NB. Input is a character bit string possibly with binary fractions NB. Output is the hexadecimal representation (character string) n =. #y NB. the number of characters in the input p =. ('.'= y)#i.n NB. determines index of binary point if. 0=#p do. r =. b2hexWhole y else. integer_part =. s2L p {. y NB. bits for integer part fractional_part =. s2L (1+p)}.y NB. bits for fractional part ip=. b2hexWhole L2s(-(4((|-)+])#integer_part)){.integer_part fp=. b2hexWhole L2s(4((|-)+])#fractional_part){.fractional_part r=. ip,'.',fp end. r ) hex2b =: 3 : 0 NB. Input is a character string for a hexadecimal representation NB. Output converts string to binary character string symbols=.'.0123456789ABCDEF' table=. (<'.'), L2s each _4{. each s2L each d2b each i.16 ;((y=/ symbols)#i.17){ table ) b2hexWhole =: 3 : 0 NB. Input is a character bit string for a natural number NB. Output is the hexadecimal representation (character string) NB. This function does not work with a binary point symbols=.'.0123456789ABCDEF' table=. (<'.'), L2s each _4{. each s2L each d2b each i.16 n=.#y m=.(<"1)((n%4),4)$y ;((m=/ table)#i.17){ symbols ) NB.--------------- decimal/hex conversions --------------------- hex2d =: b2d @ hex2b NB. Input is a hex representation; output corresponding decimal d2hex =: 3 : 0 NB. Input is a decimal mixed fraction NB. Output is character string of hex representation }: 0 d2hex y : digits=. '' symbols=.'0123456789ABCDEF' p=. 0 >. <. 16 ^. y for_k. i.p+x+1 do. if. y >: 16^p-k do. rem=. <. y%(16^p-k) digits =. digits,rem{symbols y =. y - rem*16^p-k else. digits=. digits,'0' end. end. ((p+1){.digits),'.',(p+1)}. digits ) NB.--- Computer representation of integers/signed numbers ------ Z2cr =: st@(-@[ {. [: s2L d2b@((2&^@[ * 0&>@]) + ])) NB. comp. integer rep. NB. OR: Z2cr =: 3 : 0 NB. Input y is an integer and x a natural number NB. Output computer representation of integer in x bits 16 Z2cr y NB. If no left argument is given 16 bit representation is assumed : r =. (-x){. s2L d2b |y NB. gets bit list of the right length if. y < 0 do. s=. r=0 NB. Ones complement t=. >./(0=s)#i.#s NB. index of last 0 in s r=.(#s){.(t{.s),1 NB. replace last 0 with 1 and 0's after end. r=. L2s r NB. converts to character string ) cr2Z =: (('0'={.)*b2d) + ('1'={.)*(b2d - 2&^@#) NB. integer from comp.rep. NB. OR: cr2Z =: 3 : 0 NB. Input is a computer representation of an integer NB. Output is the signed integer r =. b2d y if. '1' = {.y do. r - 2x^#y end. ) NB.------- Floating point/real number conversions -------------- d2float =: 3 : 0 NB. Input y is a decimal mixed fraction NB. Input x is a pair of integers(>0) for the number of bits NB. 1) in the representation and 2) in the characteriistic NB. Output is a floating point representation 32 11 d2float y NB. Left input is given default values if not present : m =. <:-/x if. 1>|y do. L =. 2^.|y p =. (>.L)+(L=>.L) b =. (m-p) d2b |y mantissa =. (2-p)}.b else. b =. m d2b |y p =. ('.'= b)#i.#b NB. determines index of binary point if. 0=#p do. b =. b,'.' n =. #b p =.('.'= b)#i.#b end. mantissa=. m{.('.'~:b)#b end. characteristic =. L2s(-}.x){.s2L d2b p + <:2^<:}.x (":y<0),characteristic,mantissa ) float2d =: 3 : 0 NB. Input x is the number of bits in the characteristic NB. Input y is the floating point representation NB. Output is the smallest decimal represented by y if. 32=#y do. 11 float2d y NB. Assuming a 32 bit rep has 11 bit characteristic else. 'Please enter number of bits in characteristic as left variable' end. : s =. {.y sign =. (s='0')-(s='1') cm =. }. y c =. b2d x{. cm m =. x}. cm p =. c - <:2^<:x sign*(b2d m)* 2x^p-#m ) NB.---------- Number/Character conversions --------------------- num2char =: 3 : 0 a=.<'This function has no pre-fix interpretation.' a=.a,"1<'Correct syntax is, for example:' a=.a,"1<' N numchar 20 12 where N is a stored number with' >a,"1<'20 spaces given to a display showing 12 decimal places.' : NB. N numchar m n displays N as a character in m spaces NB. with n decimal places. space =. {. y places=. }. y c =. space + j.places c ": x ) char2num =: 3 : 0 ".y NB. returns a number displayed in 6 digit presentation NB. assuming input is a character representation of a number. ) NB.---------- UPPER case/lower case conversions ------------------ UCletters =: ,@({&'ABCDEFGHIJKLMNOPQRSTUVWXYZ')@(>@(#&(i.26)@(=&'abcdefghijklmnopqrstuvwxyz')&.>)@((#~ +/"1@(0&<)@(=/&'abcdefghijklmnopqrstuvwxyz'))~)) lcletters =: ,@({&'abcdefghijklmnopqrstuvwxyz')@(>@(#&(i.26)@(=&'ABCDEFGHIJKLMNOPQRSTUVWXYZ')&.>)@((#~ +/"1@(0&<)@(=/&'ABCDEFGHIJKLMNOPQRSTUVWXYZ'))~)) UCplaces =: +/"1@(0&<)@(=/&'ABCDEFGHIJKLMNOPQRSTUVWXYZ') # i.@# lcplaces =: +/"1@(0&<)@(=/&'abcdefghijklmnopqrstuvwxyz') # i.@# lc2UC =: 3 : '(UCletters y)(lcplaces y) } y' UC2lc =: 3 : '(lcletters y)(UCplaces y) } y' NB. ============================================================== r2d =: 3 : 0 c =. ": y NB. rational to decimal conversion p =.('r'= c)i.1 (".p{.c)% ".(>:p)}.c )