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”
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
takeanddropinstead ofsplitAt. Now, how can we translate this to work onByteStrings? Well, theByteStringinterface offerstake,drop,splitAt,append, andcons; the only thing we haven’t quite got available is the pattern matching that we did in thex:endingpart above. Luckily,ByteStringdoes offer something similar:So, using that, we can write a new
onNthfunction that works forByteStrings:Finally, we can discuss what function we should use as the
f :: Word8 -> Word8argument above. Although you talk about text above, I will point out that you shouldn’t be usingByteStringfor text anyway (ByteStrings are sequences of bytes, not sequences ofChars). 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 1is 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!