Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5944331
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:31:22+00:00 2026-05-22T16:31:22+00:00

I want to convert strict ByteStrings from Haskell into C++’s std::string to pass it

  • 0

I want to convert strict ByteStrings from Haskell into C++’s std::string to pass it to a C++ library via the FFI. As the ByteString may contain NULL characters, converting into a CString as an intermediate step is not viable. What is the right approach here?

current solution

Thanks for the answers so far. I hoped for a canonical solution for that task, but maybe it does not exist yet 🙂

Some c++ library documentation says following:

string ( const char * s, size_t n );

Content is initialized to a copy of the string formed by the first n characters in the array of characters pointed by s.

Therefore one can write such a function which copies once from the ByteString to construct a std::string

foreign import ccall unsafe toCCString_ :: CString -> CUInt -> IO (Ptr CCString)
toCCString :: ByteString -> IO (Ptr CCString)
toCCString bs =
    unsafeUseAsCStringLen bs $ \(cstring,len) ->
    toCCString_ cstring (fromIntegral len)

The C++ code accompanying toCCString_ then would just look like Neil and Alan pointed out.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T16:31:22+00:00Added an answer on May 22, 2026 at 4:31 pm

    The documentation is great!

    type CString = Ptr CChar

    A C string is a reference to an array of C characters terminated by NUL.

    type CStringLen = (Ptr CChar, Int)

    A string with explicit length information in bytes instead of a terminating NUL (allowing NUL characters in the middle of the string).

    If you use a CStringLen, you should have no problems. (In fact, I recommend this because interfacing C++ and Haskell is a nightmare.)

    NULL characters in the middle of char buffers is only problematic when you don’t know how long the data contained therein should be (and thus have to traverse it looking for a NULL, hoping that that’s the intended end of the data).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to convert a string into a double and after doing some math
I want to convert a std::string to lowercase. I am aware of the function
I want to convert a string into a series of Keycodes, so that I
I want to convert a primitive to a string, and I tried: myInt.toString(); This
I want to convert from char representing a hexadecimal value (in upper or lower
For some complicated reason, I want to convert any supported type T (coming from
I want to convert -123.456 into a C double for network transmission in python.
I want to convert the source & destination IP addresses from a packet captured
I want to convert a given time into epoch(time_t) and vice versa. can anyone
How can I convert a string of bytes into an int in python? Say

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.