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

  • Home
  • SEARCH
  • 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 6802397
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:13:06+00:00 2026-05-26T19:13:06+00:00

I’m not very fluent in C++, to tell the truth. I have some binary

  • 0

I’m not very fluent in C++, to tell the truth.

I have some binary data in memory under a type void* (which means, i think, a pointer to some unrepresentable something/nothing). The data are first taken from the file by fread.

int readfile FILE *file, void **data_return) {
    //some code...

    fread((void *)data, length, 1, file);

    //some code...
}

There’s a complex algorithm behind reading the binary data, but I think I don’t need to understand it for this task.

char *t = ((char *)loc->mo_data) + string_offset;
return t;

This code reads the void* type (loc->mo_data) as string. Still understandable for me I guess.

The problem is that this data contains russian, spanish, czech and all sort of international characters representable in UTF8.

I’m not even sure, what encoding the “char” represents, probably win1250, because the strings returned are just bad. The function returns Организация instead of Организация. The first string is the UTF8 but represented in ASCII.

The bigger picture: I’m playing with this C++ library which has already been written by someone else – the library exposes just two functions, open file (returns pointer) and get string from this file by string key (returns string). This library is being used in a C# project.

At first, I thought that there might be something wrong with passing UTF8 strings between an C# app and DLL library

    [DllImport("MoReader.dll", CallingConvention = CallingConvention.Cdecl)]
    public static extern IntPtr OpenFile(string path);

    [DllImport("MoReader.dll", CallingConvention = CallingConvention.Cdecl)]
    public static extern string FindString(IntPtr filePointer, string key);

C++ code:

    extern "C" __declspec(dllexport) BinaryFileType* OpenFile(char *filePath);
    extern "C" __declspec(dllexport) char *FindString(BinaryFileType *locText, char *key);

FindString returns the string but in a wrong encoding. And I don’t know, how one could read ASCII represented in C# strings which are Unicode as UTF8…I tried some conversion methods but to no avail.

Though I think the problem is in the C++ code, i’d love the char type to be in the UTF8 encoding, I’ve noticed there’s something called a codepage and there are some conversion functions and utf8 stream readers but because of my weak C++ knowledge, I don’t really know the solution.

=== UPDATE ===

I’ve found a property in the Encoding class…
When I read the output string like this:

Encoding.UTF8.GetString(Encoding.Default.GetBytes(e))

…the result is right. I’m just getting the bytes from the string via some “Default” encoding and then I read the bytes again with the UTF8. The Default encoding here on my computer is ISO-8859-2 but it would be just plain stupid to rely on this property.

So…the question remains. I still need to know, how to read the void* type with a particular encoding. But at least, I know now that the string is being returned in the default encoding used by Windows.

** === ANSWER === **

Thanks everyone for answers.

As James pointed out, char * are just numbers. So I avoided all encoding troubles by just getting the numbers and not trying to interpret them as a string at all.
There was another problem…how to get an array of bytes in C# out of a char* in the C++ library? There is a Marshal.Copy method but I need to know the size of the string.
Every char* in C++ must end with a null character ‘\0’.
So in the end, I just read a byte after a byte until i find this null character. The code then looks like this.

IntPtr charPointer = ExternDll.FindString(fileIntPtr, "someString");
List<byte> bytes = new List<byte>();
for (int i=0; ;i++)
{
    byte b = Marshal.ReadByte(charPointer, i);
    if (b == '\0')
        break;

     bytes.Add(b);
}

string theResultStringInTheUTF8 = Encoding.UTF8.ToString(bytes.ToArray());
  • 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-26T19:13:07+00:00Added an answer on May 26, 2026 at 7:13 pm

    C++ is agnostic about character encoding. For that matter, if you’re
    getting the characters through some sort of hacky type conversions, any
    language will be; there’s no way for the language to know what the
    encoding is.

    In C++, char is really just a small integer; it’s only by convention
    that it contains some character encoding. But which encoding depends on
    you. If your input is really UTF-8, then the char’s pointed to by a
    char* will contain UTF-8; if it’s something else, then they’ll contain
    something else.

    When you output the char’s to the screen, C++ just passes them on (at
    least by default). It’s up to the terminal window to decide how to
    interpret them; i.e. break the sequence into code points, then map each
    code point to a graphic image. Under Unix (xterm), this is defined by
    the display font; under Windows, formally at least by the code page (but
    you can certainly install miscoded fonts which will screw it up). C++
    has nothing to do with this. The code page for UTF-8 is 65001; if you
    set the terminal to use this code page (chcp 5001 on the command
    line), then output UTF-8, it should work.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have just tried to save a simple *.rtf file with some websites and
I have an autohotkey script which looks up a word in a bilingual dictionary
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have an array which has BIG numbers and small numbers in it. I
I have a text area in my form which accepts all possible characters from
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
For some reason, after submitting a string like this Jack’s Spindle from a text
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is

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.