Writing raw binary to a file in Haskell?












0















I'm trying to do some Huffman coding in Haskell, but am having a hard time figuring out how to save the resulting 1s and 0s as they are instead of as a string. So far, I've read the following--



https://wiki.haskell.org/Dealing_with_binary_data



https://wiki.haskell.org/Binary_IO



http://hackage.haskell.org/package/binary-0.10.0.0/docs/Data-Binary.html



--but am still pretty confused as to how to deal with my specific situation. Similarly, I'm having difficulty figuring out how to write the tree structure



data HTree = Leaf Char Int
| Branch HTree HTree Int
deriving (Show)


used to decode the data to a file. (HTree is the structure I'm using to store the Huffman Tree).










share|improve this question




















  • 2





    Take a look at Data.ByteString

    – typetetris
    Nov 22 '18 at 6:23











  • See: wiki.haskell.org/Dealing_with_binary_data

    – AJFarmar
    Nov 22 '18 at 9:28
















0















I'm trying to do some Huffman coding in Haskell, but am having a hard time figuring out how to save the resulting 1s and 0s as they are instead of as a string. So far, I've read the following--



https://wiki.haskell.org/Dealing_with_binary_data



https://wiki.haskell.org/Binary_IO



http://hackage.haskell.org/package/binary-0.10.0.0/docs/Data-Binary.html



--but am still pretty confused as to how to deal with my specific situation. Similarly, I'm having difficulty figuring out how to write the tree structure



data HTree = Leaf Char Int
| Branch HTree HTree Int
deriving (Show)


used to decode the data to a file. (HTree is the structure I'm using to store the Huffman Tree).










share|improve this question




















  • 2





    Take a look at Data.ByteString

    – typetetris
    Nov 22 '18 at 6:23











  • See: wiki.haskell.org/Dealing_with_binary_data

    – AJFarmar
    Nov 22 '18 at 9:28














0












0








0








I'm trying to do some Huffman coding in Haskell, but am having a hard time figuring out how to save the resulting 1s and 0s as they are instead of as a string. So far, I've read the following--



https://wiki.haskell.org/Dealing_with_binary_data



https://wiki.haskell.org/Binary_IO



http://hackage.haskell.org/package/binary-0.10.0.0/docs/Data-Binary.html



--but am still pretty confused as to how to deal with my specific situation. Similarly, I'm having difficulty figuring out how to write the tree structure



data HTree = Leaf Char Int
| Branch HTree HTree Int
deriving (Show)


used to decode the data to a file. (HTree is the structure I'm using to store the Huffman Tree).










share|improve this question
















I'm trying to do some Huffman coding in Haskell, but am having a hard time figuring out how to save the resulting 1s and 0s as they are instead of as a string. So far, I've read the following--



https://wiki.haskell.org/Dealing_with_binary_data



https://wiki.haskell.org/Binary_IO



http://hackage.haskell.org/package/binary-0.10.0.0/docs/Data-Binary.html



--but am still pretty confused as to how to deal with my specific situation. Similarly, I'm having difficulty figuring out how to write the tree structure



data HTree = Leaf Char Int
| Branch HTree HTree Int
deriving (Show)


used to decode the data to a file. (HTree is the structure I'm using to store the Huffman Tree).







haskell file-io huffman-code






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 7:16







Cameron Toy

















asked Nov 22 '18 at 5:25









Cameron ToyCameron Toy

11




11








  • 2





    Take a look at Data.ByteString

    – typetetris
    Nov 22 '18 at 6:23











  • See: wiki.haskell.org/Dealing_with_binary_data

    – AJFarmar
    Nov 22 '18 at 9:28














  • 2





    Take a look at Data.ByteString

    – typetetris
    Nov 22 '18 at 6:23











  • See: wiki.haskell.org/Dealing_with_binary_data

    – AJFarmar
    Nov 22 '18 at 9:28








2




2





Take a look at Data.ByteString

– typetetris
Nov 22 '18 at 6:23





Take a look at Data.ByteString

– typetetris
Nov 22 '18 at 6:23













See: wiki.haskell.org/Dealing_with_binary_data

– AJFarmar
Nov 22 '18 at 9:28





See: wiki.haskell.org/Dealing_with_binary_data

– AJFarmar
Nov 22 '18 at 9:28












1 Answer
1






active

oldest

votes


















2














You probably want to take a look at the Put monad in Data.Binary.Put. You didn't provide any code example but something to get you started:



-- just an example with some made up encoding  

huffmanEncode :: HTree -> Put
huffmanEncode (Leaf c val) = do
putWord16be 0xf00
putCharUtf8 c
putWord32be $ fromIntegral val
huffmanEncode (Branch l r x) = do
putWord16be 0xbaf
putWord32be $ fromIntegral x
huffmanEncode l
huffmanEncode r


You would then "run" your encoder thus: runPut $ huffmanEncode myTree






share|improve this answer
























  • obviously this is some made up encoding.. I have no clue how you want to encode this ;-)

    – Erick Gonzalez
    Nov 22 '18 at 12:53











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53424380%2fwriting-raw-binary-to-a-file-in-haskell%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














You probably want to take a look at the Put monad in Data.Binary.Put. You didn't provide any code example but something to get you started:



-- just an example with some made up encoding  

huffmanEncode :: HTree -> Put
huffmanEncode (Leaf c val) = do
putWord16be 0xf00
putCharUtf8 c
putWord32be $ fromIntegral val
huffmanEncode (Branch l r x) = do
putWord16be 0xbaf
putWord32be $ fromIntegral x
huffmanEncode l
huffmanEncode r


You would then "run" your encoder thus: runPut $ huffmanEncode myTree






share|improve this answer
























  • obviously this is some made up encoding.. I have no clue how you want to encode this ;-)

    – Erick Gonzalez
    Nov 22 '18 at 12:53
















2














You probably want to take a look at the Put monad in Data.Binary.Put. You didn't provide any code example but something to get you started:



-- just an example with some made up encoding  

huffmanEncode :: HTree -> Put
huffmanEncode (Leaf c val) = do
putWord16be 0xf00
putCharUtf8 c
putWord32be $ fromIntegral val
huffmanEncode (Branch l r x) = do
putWord16be 0xbaf
putWord32be $ fromIntegral x
huffmanEncode l
huffmanEncode r


You would then "run" your encoder thus: runPut $ huffmanEncode myTree






share|improve this answer
























  • obviously this is some made up encoding.. I have no clue how you want to encode this ;-)

    – Erick Gonzalez
    Nov 22 '18 at 12:53














2












2








2







You probably want to take a look at the Put monad in Data.Binary.Put. You didn't provide any code example but something to get you started:



-- just an example with some made up encoding  

huffmanEncode :: HTree -> Put
huffmanEncode (Leaf c val) = do
putWord16be 0xf00
putCharUtf8 c
putWord32be $ fromIntegral val
huffmanEncode (Branch l r x) = do
putWord16be 0xbaf
putWord32be $ fromIntegral x
huffmanEncode l
huffmanEncode r


You would then "run" your encoder thus: runPut $ huffmanEncode myTree






share|improve this answer













You probably want to take a look at the Put monad in Data.Binary.Put. You didn't provide any code example but something to get you started:



-- just an example with some made up encoding  

huffmanEncode :: HTree -> Put
huffmanEncode (Leaf c val) = do
putWord16be 0xf00
putCharUtf8 c
putWord32be $ fromIntegral val
huffmanEncode (Branch l r x) = do
putWord16be 0xbaf
putWord32be $ fromIntegral x
huffmanEncode l
huffmanEncode r


You would then "run" your encoder thus: runPut $ huffmanEncode myTree







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 22 '18 at 12:48









Erick GonzalezErick Gonzalez

1544




1544













  • obviously this is some made up encoding.. I have no clue how you want to encode this ;-)

    – Erick Gonzalez
    Nov 22 '18 at 12:53



















  • obviously this is some made up encoding.. I have no clue how you want to encode this ;-)

    – Erick Gonzalez
    Nov 22 '18 at 12:53

















obviously this is some made up encoding.. I have no clue how you want to encode this ;-)

– Erick Gonzalez
Nov 22 '18 at 12:53





obviously this is some made up encoding.. I have no clue how you want to encode this ;-)

– Erick Gonzalez
Nov 22 '18 at 12:53




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53424380%2fwriting-raw-binary-to-a-file-in-haskell%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

SQL update select statement

'app-layout' is not a known element: how to share Component with different Modules