Haskell 字节串更改 ASCII?
import qualified Data.ByteString.Lazy.Char8 as BS
stuff <- BS.readFile "stuff.txt"
如何从字节串中获取特定字符然后更改其 ASCII,然后将其放回去? 我使用 readInt 还是其他东西?
例如:“aaaaa”,“a”是 97,所以减 1,你得到“aa`aa”
import qualified Data.ByteString.Lazy.Char8 as BS
stuff <- BS.readFile "stuff.txt"
How do take a specific character from a bytestring then change its ASCII and then put it back?
Do I use readInt or something?
Ex: "aaaaa" ,"a" is 97 so minus 1 and you have "aa`aa"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
其他人已经解决了执行字节操作的问题,因此我将重点讨论问题的另一半:选择和更新 ByteString 中的特定字节。让我们开始使用更熟悉的接口实现普通列表的操作:
您可以使用
take
和drop
而不是splitAt
来实现此操作。现在,我们如何将其转换为在ByteString
上工作?那么,ByteString 接口提供了 take、drop、splitAt、append 和缺点;我们唯一还没有完全可用的是我们在上面的 x:ending 部分中所做的模式匹配。幸运的是,ByteString
确实提供了类似的功能:因此,使用它,我们可以编写一个适用于
ByteString
的新onNth
函数:最后,我们可以讨论我们应该使用什么函数作为
f :: Word8 ->上面的 Word8
参数。尽管您在上面讨论了文本,但我会指出您无论如何都不应该使用ByteString
来表示文本(ByteString
是字节序列,而不是序列)字符)。因此,如果您选择使用
ByteString
,那么您必须谈论的是字节,而不是文本。 ;-)因此,您实际上是想询问一个将字节减一的函数,大概是在边界上环绕。
subtract 1
正是执行此操作的函数,因此要将pack [97, 97, 97, 97, 97]
转换为pack [97, 97, 96 , 97, 97]
,你可以写onNth 2 (subtract 1)
。读起来几乎像英语!Others have addressed the problem of doing the byte operations, so I will focus on the other half of your question: selecting and updating a particular byte within a
ByteString
. Let's start with implementing the operation for plain lists, using a more familiar interface:You might equivalently implement this using
take
anddrop
instead ofsplitAt
. Now, how can we translate this to work onByteString
s? Well, theByteString
interface offerstake
,drop
,splitAt
,append
, andcons
; the only thing we haven't quite got available is the pattern matching that we did in thex:ending
part above. Luckily,ByteString
does offer something similar:So, using that, we can write a new
onNth
function that works forByteString
s:Finally, we can discuss what function we should use as the
f :: Word8 -> Word8
argument above. Although you talk about text above, I will point out that you shouldn't be usingByteString
for text anyway (ByteString
s are sequences of bytes, not sequences ofChar
s). Therefore, if you have chosen to useByteString
, you must be talking about bytes, not text. ;-)Therefore, you really meant to ask about a function which decreases a byte by one, presumably wrapping around on a boundary.
subtract 1
is a function that does exactly that, so to convertpack [97, 97, 97, 97, 97]
topack [97, 97, 96, 97, 97]
, you might writeonNth 2 (subtract 1)
. Reads almost like English!BS.map pred
怎么样?您还可以使用fromEnum
和toEnum
来进行与Int
之间的转换。How about
BS.map pred
? You can also usefromEnum
andtoEnum
to make convert to / fromInt
s.使用
BS.unpack
将字节转换为Char
后,您可以使用fromEnum :: Char -> Int
(或者等效地,来自Data.Char
的ord
)将Char
转换为其 ASCII 值,然后您可以像普通整数一样进行操作。要将 ASCII 值从Int
转换回Char
,请使用toEnum
或Data.Char.chr
,然后可以使用BS.pack
或类似方法将Char
转换回ByteString
。Once you've converted the bytes to
Char
s withBS.unpack
, you can usefromEnum :: Char -> Int
(or, equivalently,ord
fromData.Char
) to convert aChar
to its ASCII value, which you can then manipulate like a normal integer. To convert an ASCII value from anInt
back to aChar
, usetoEnum
orData.Char.chr
, and theChar
s can then be converted back to aByteString
withBS.pack
or similar.