home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / OL.LZH / PROCS.LZH / BINCVT.ICN < prev    next >
Text File  |  1991-07-13  |  2KB  |  61 lines

  1. ############################################################################
  2. #
  3. #    Name:    bincvt.icn
  4. #
  5. #    Title:    Convert binary data
  6. #
  7. #    Author:    Robert J. Alexander
  8. #
  9. #    Date:    December 5, 1989
  10. #
  11. ############################################################################
  12. #
  13. #  unsigned() -- Converts binary byte string into unsigned integer.
  14. #  Detects overflow if number is too large.
  15. #
  16. #  This procedure is normally used for processing of binary data
  17. #  read from a file.
  18. #
  19. #
  20. #  raw() -- Puts raw bits of characters of string s into an integer.  If
  21. #  the size of s is less than the size of an integer, the bytes are put
  22. #  into the low order part of the integer, with the remaining high order
  23. #  bytes filled with zero.  If the string is too large, the most
  24. #  significant bytes will be lost -- no overflow detection.
  25. #
  26. #  This procedure is normally used for processing of binary data
  27. #  read from a file.
  28. #
  29. #
  30. #  rawstring() -- Creates a string consisting of the raw bits in the low
  31. #  order "size" bytes of integer i.
  32. #
  33. #  This procedure is normally used for processing of binary data
  34. #  to be written to a file.
  35. #
  36. ############################################################################
  37.  
  38. procedure unsigned(s)
  39.    local i
  40.    i := 0
  41.    every i := ord(!s) + i * 256
  42.    return i
  43. end
  44.  
  45. procedure raw(s)
  46.    local i
  47.    i := 0
  48.    every i := ior(ord(!s),ishift(i,8))
  49.    return i
  50. end
  51.  
  52. procedure rawstring(i,size)
  53.    local s
  54.    s := ""
  55.    every 1 to size do {
  56.       s := char(iand(i,16rFF)) || s
  57.       i := ishift(i,-8)
  58.       }
  59.    return s
  60. end
  61.