You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

107 lines
3.6 KiB

  1. package main
  2. import (
  3. "encoding/hex"
  4. "fmt"
  5. "iconv"
  6. "io/ioutil"
  7. "os"
  8. )
  9. func main() {
  10. // read bytes from sample.utf8
  11. utf8Bytes, err := ioutil.ReadFile("sample.utf8")
  12. if err != nil {
  13. fmt.Println("Could not open 'sample.utf8': ", err)
  14. }
  15. // read bytes from sample.ebcdic-us
  16. ebcdicBytes, err := ioutil.ReadFile("sample.ebcdic-us")
  17. if err != nil {
  18. fmt.Println("Could not open 'sample.ebcdic-us': ", err)
  19. }
  20. // use iconv to check conversions both ways
  21. utf8String := string(utf8Bytes)
  22. ebcdicString := string(ebcdicBytes)
  23. // convert from utf-8 to ebcdic
  24. utf8ConvertedString, err := iconv.ConvertString(utf8String, "utf-8", "ebcdic-us")
  25. if err != nil || ebcdicString != utf8ConvertedString {
  26. // generate hex string
  27. ebcdicHexString := hex.EncodeToString(ebcdicBytes)
  28. utf8ConvertedHexString := hex.EncodeToString([]byte(utf8ConvertedString))
  29. fmt.Println("utf-8 was not properly converted to ebcdic-us by iconv.ConvertString, error: ", err)
  30. fmt.Println(ebcdicHexString, " - ", len(ebcdicString))
  31. fmt.Println(utf8ConvertedHexString, " - ", len(utf8ConvertedString))
  32. } else {
  33. fmt.Println("utf-8 was properly converted to ebcdic-us by iconv.ConvertString")
  34. }
  35. // convert from ebcdic to utf-8
  36. ebcdicConvertedString, err := iconv.ConvertString(ebcdicString, "ebcdic-us", "utf-8")
  37. if err != nil || utf8String != ebcdicConvertedString {
  38. // generate hex string
  39. utf8HexString := hex.EncodeToString(utf8Bytes)
  40. ebcdicConvertedHexString := hex.EncodeToString([]byte(ebcdicConvertedString))
  41. fmt.Println("ebcdic-us was not properly converted to utf-8 by iconv.ConvertString, error: ", err)
  42. fmt.Println(utf8HexString, " - ", len(utf8String))
  43. fmt.Println(ebcdicConvertedHexString, " - ", len(ebcdicConvertedString))
  44. } else {
  45. fmt.Println("ebcdic-us was properly converted to utf-8 by iconv.ConvertString")
  46. }
  47. testBuffer := make([]byte, len(ebcdicBytes)*2)
  48. // convert from ebdic bytes to utf-8 bytes
  49. bytesRead, bytesWritten, err := iconv.Convert(ebcdicBytes, testBuffer, "ebcdic-us", "utf-8")
  50. if err != nil || bytesRead != len(ebcdicBytes) || bytesWritten != len(utf8Bytes) {
  51. fmt.Println("ebcdic-us was not properly converted to utf-8 by iconv.Convert, error: ", err)
  52. } else {
  53. fmt.Println("ebcdic-us was properly converted to utf-8 by iconv.Convert")
  54. }
  55. // convert from utf-8 bytes to ebcdic bytes
  56. bytesRead, bytesWritten, err = iconv.Convert(utf8Bytes, testBuffer, "utf-8", "ebcdic-us")
  57. if err != nil || bytesRead != len(utf8Bytes) || bytesWritten != len(ebcdicBytes) {
  58. fmt.Println("utf-8 was not properly converted to ebcdic-us by iconv.Convert, error: ", err)
  59. } else {
  60. fmt.Println("utf-8 was properly converted to ebcdic-us by iconv.Convert")
  61. }
  62. // test iconv.Reader
  63. utf8File, _ := os.Open("sample.utf8")
  64. utf8Reader, _ := iconv.NewReader(utf8File, "utf-8", "ebcdic-us")
  65. bytesRead, err = utf8Reader.Read(testBuffer)
  66. if err != nil || bytesRead != len(ebcdicBytes) {
  67. fmt.Println("utf8 was not properly converted to ebcdic-us by iconv.Reader", err)
  68. } else {
  69. fmt.Println("utf8 was property converted to ebcdic-us by iconv.Reader")
  70. }
  71. ebcdicFile, _ := os.Open("sample.ebcdic-us")
  72. ebcdicReader, _ := iconv.NewReader(ebcdicFile, "ebcdic-us", "utf-8")
  73. bytesRead, err = ebcdicReader.Read(testBuffer)
  74. if err != nil || bytesRead != len(utf8Bytes) {
  75. fmt.Println("ebcdic-us was not properly converted to utf-8 by iconv.Reader: ", err)
  76. if bytesRead > 0 {
  77. fmt.Println(string(testBuffer[:bytesRead]))
  78. fmt.Println(hex.EncodeToString(testBuffer[:bytesRead]))
  79. fmt.Println(hex.EncodeToString(utf8Bytes))
  80. }
  81. } else {
  82. fmt.Println("ebcdic-us was properly converted to utf-8 by iconv.Reader")
  83. }
  84. }