问题
Introduction
Hey folks, i am currently working on a JavaScript-based 3d slicing tool (SLAcer.js - awesome work btw.), that can generate convenient print files for my cheap dlp-printer (SparkMaker). The printing files consist of some G-Code and simple binary layers/images (see here).
I am already able to generate the G-Code and a Uint8Array that contains the relevant information of the binary layer. All G-Code and binary layers currently get concatenated as a String (there may be the first Problem) and in the end can be downloaded as print file (.wow) as part of a zip File. (projects default JSZip Framework) (see here)
The point where i am struggling is:
I am not able to get the right TextEncoding for the binary layer to match the original binary layer.
var array = new Uint8Array(width*height/8);
var binary_layer;
Already tried several things, including:
binary_layer=(new TextDecoder("utf-8")).decode(array) /*with different text encodes*/
Also tried:
binary_layer=bin2string(array)
function bin2string(array){
var result = "";
for(var i = 0; i < array.length; ++i){
result+= (String.fromCharCode(array[i]));
}
console.log(result);
return result;
}
What amazes me is that, when exporting each Uint8Array as a separate binary txt file they nearly perfectly match the wanted/original pattern
Concatenation:
file_contents = "[some gecode]";
file_contents += binary_layer;
Summary
var staring = "hello";
var array = new Uint8Array(2);
array[0]=255;
array[1]=0;
Wanted file contents (both text and raw binary - utf8):
hello(xFF)(NUL)
This may sound easy, but in terms of right encoding it's not:
- file sliced by SLAcer (binary layers differ)
- single layer file (does nearly match the original first layer pattern)
- file sliced by original software Hope someone can help me :)
回答1:
Issue could be fixed (still further testing is needed)
Problem could be solved by saving all data with {binary: true}
option and using simple hex to string decoder method:
function bin2string(array){
var result = "";
for(var i = 0; i < array.length; ++i){
result+= (String.fromCharCode(array[i]));
}
return result;
}
Annoyingly (in my case):
var array = new Uint8Array(2);
array[1]=255;
(new TextDecoder("utf-8")).decode(array)
Did output: (NUL)(xFD)
But should have output: (NUL)(xFF)
This need to be discussed further more! (see also here)
来源:https://stackoverflow.com/questions/49073758/binary-handling-for-3d-printing-js-slicer-slacer-js