bytevector-utils.scm 906 B

12345678910111213141516171819202122232425262728293031323334
  1. (library (bytevector-utils)
  2. (export
  3. string->bytevector
  4. bytevector->string)
  5. (import
  6. (except (rnrs base) let-values)
  7. (only (guile)
  8. lambda* λ
  9. error
  10. eof-object?
  11. call-with-output-string
  12. call-with-input-string)
  13. (prefix (logging) log:)
  14. (ice-9 binary-ports)
  15. (rnrs bytevectors)))
  16. (define string->bytevector
  17. (λ (the-string)
  18. (call-with-input-string the-string
  19. (λ (port)
  20. (get-bytevector-all port)))))
  21. (define bytevector->string
  22. (λ (bytevec)
  23. "Translate all bytes in a bytevector to characters and make a string from
  24. them. Since bytevectors contain only bytes, which consist of 8 bits, which makes
  25. for 2 to the power of 8 possible values, the characters can only range from the
  26. zeroth character to the 255th character."
  27. (list->string
  28. (map integer->char
  29. (bytevector->u8-list bytevec)))))