且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将ASCII数组(图像)转换为单个字符串

更新时间:2023-11-04 22:48:04

是的,还有更好的方法。你真的想看一下原始数据流的章节:

Yes, there is a better way. You really want to look into the chapter of raw-data streaming:

如果您在stream对象中保存原始数据,则可以以任何您喜欢的形式进行读取和写入。所以你的问题的解决方案是

If you hold raw data in a "stream" object, you can read and write it in any form you like. So the solution to your problem is to


  • 创建一个流

  • 添加图像到流(写入二进制数据)

  • 将蒸汽位置重置为开始

  • 读取二进制数据字符串

  • Create a stream
  • Add the "image" to the stream (writing binary data)
  • Reset the steam position to the start
  • Read out the binary data a string

这是代码:

{
    number sx = 10
    number sy = 10
    image textImg := IntegerImage( "Text", 1, 0 , sx, sy )
    textImg = 97 + random()*26 
    textImg.showimage()

    object stream = NewStreamFromBuffer( 0 )
    ImageWriteImageDataToStream( textImg, stream, 0 )
    stream.StreamSetPos(0,0)
    string asString = StreamReadAsText( stream, 0, sx*sy )
    Result("\n as string:\n\t"+asString)
}

请注意,您可以创建链接到光盘上文件的流,如果您知道起始位置(以字节为单位),也可以直接从文件中读取。

Note that you could create a stream linked to file on disc and, provided you know the starting position in bytes, read from the file directly as well.