1

first a depressing fact: https://www.base64decode.org/ can do what i want to do.

i´m trying to encode and decode (to and from base64) a model file (.shm) generated by the image processing tool MVTec Halcon because i want to store it in a xml file.

If i open it, it has this strange form:

HSTF ÿÿÿÿ¿€          Q¿ÙG®záH?Üä4©±w?­Eè}‰@?ð ................

I´m using this methods to encode and decode it:

    public static string Base64Encode(string text)
    {
        Byte[] textBytes = Encoding.Default.GetBytes(text);
        return Convert.ToBase64String(textBytes);
    }

    public static string Base64Decode(string base64EncodedData)
    {
        Byte[] base64EncodedBytes = Convert.FromBase64String(base64EncodedData);
        return Encoding.Default.GetString(base64EncodedBytes);
    }

and calling the methods from a gui like this:

    var model = File.ReadAllText(@"C:\Users\\Desktop\model_region_nut.txt");
    var base64 = ImageConverter.Base64Encode(model);
    File.WriteAllText(@"C:\Users\\Desktop\base64.txt", base64);

    var modelneu = ImageConverter.Base64Decode(File.ReadAllText(@"C:\Users\\Desktop\base64.txt"));
    File.WriteAllText(@"C:\Users\\Desktop\modelneu.txt", modelneu);

my result for modelneu is:

HSTF ??????          Q??G?z?H???4??w??E?}??@??

so you can see that there are lots of missing characters.. I guess the problem is caused by using .Default.

Thanks for your help, Michel

Michel
  • 25
  • 4
  • 1
    It seems likely to be an encoding issue. What kind of data are you saving? – ProgrammingLlama Mar 13 '19 at 08:24
  • they call it a .shm file. I store it as a .txt file. – Michel Mar 13 '19 at 08:26
  • @Michel and what is a .shm file? What you posted isn't Base64. Using your locale's codepage with Encoding.Default won't help either - if you can't read the text, it means it's not in your language. It probably means that `.shm` file doesn't contain text at all – Panagiotis Kanavos Mar 13 '19 at 08:39
  • @Panagiotis Kanavos the .shm file is generated by an intern halcon serialize method. I was afraid too that the file maybe doesnt contain text at all but the problem was just the way i read in (ReadAllText()) the file. I had to overload the method like yekanchi said. Now im able to reuse this model-files. For your information: .shm Files are "model files" which contain only the edges of an object. I use them for Image Processing. the model files are the templates to search for in an image. – Michel Mar 13 '19 at 10:38

2 Answers2

0

The problem is with reading file with unspecified encoding, check this question.

As mentioned there you can go with overload for ReadAllText to specify encoding and also for writing you must specofy encoding for WriteAllText I suggest using UTF-8 encoding so:

var model = File.ReadAllText(@"C:\Users\pichlerm\Desktop\model_region_nut.txt",Encoding.UTF8);
var base64 = ImageConverter.Base64Encode(model);
File.WriteAllText(@"C:\Users\\Desktop\base64.txt", base64,Encoding.UTF8);

var modelneu = ImageConverter.Base64Decode(File.ReadAllText(@"C:\Users\\Desktop\base64.txt"));
File.WriteAllText(@"C:\Users\pichlerm\Desktop\modelneu.txt", modelneu);
dev-masih
  • 4,188
  • 3
  • 33
  • 55
yekanchi
  • 813
  • 1
  • 12
  • 30
0

If you're working with binary data, there is no reason at all to go through text decoding and encoding. Doing so only risks corrupting the data in various ways, even if you're using a consistent character encoding.

Just use File.ReadAllBytes() instead of File.ReadAllText() and skip the unnecessary Encoding step.

kalimag
  • 1,139
  • 2
  • 6
  • 11
  • i tried this first but unfortunately i have to put it in an xml with encoding = "utf-16". so there are missing characters.. – Michel Mar 13 '19 at 10:28
  • You can still Base64 encode the bytes you get from `File.ReadAllBytes()`, you just don't need to read the file as text and then convert the text back to bytes before you do that. – kalimag Mar 13 '19 at 11:44
  • aaah, now i understand what you mean. Thank you. – Michel Mar 13 '19 at 11:56