barnhill / barcodelib

C# Barcode Image Generation Library
Apache License 2.0
736 stars 239 forks source link

string to code128 string (not binary) #122

Closed HelloNewW closed 3 years ago

HelloNewW commented 3 years ago

Hi,

I be able to load your project on VS 2019 :-) When I encode the value 123456 with code 128, I getting that long bin value 1101001110010110011100100010110001110001011011000010100100011101101100011101011

Is is possible to get the (short) encode string ? (The ASCII value, I think, something like that ÍKLÈ3.323LÎ ) I want to use that encode string with code 128 font to get the barcode image

binki commented 3 years ago

@HelloNewW

It would be a bit odd to pack those as ASCII characters, but you can certainly use various string manipulation to reencode the string any way you want. As there is no standard way of doing this or use-case for it, it’s up to you to do what you want with it.

Are you certain you want to do this, though? The return value of Barcode.GenerateBarcode() is a description of where bars would go in the barcode, not itself an image. If you want to produce an image, you might be more interested in the Barcode.Encode() method. Also, simply being able to pack the data into a string type in this way will result in nonsensical results because the sequences may not be valid characters, especially if you encode 8 bits at a time instead of 7 bits at a time (which seems to be what you’re trying to do).

Also, the length of the string returned by Barcode.GenerateBarcode() is not guaranteed to be divisible by either 7 or 8. So, depending on how you do this, you need to be aware that the result will have trailing 0s which shouldn’t be there upon decoding.

Here is an example chunk of code which can accomplish something like that:

using System;
using System.Text;

var barString = "11010011100101100111001000101100011100010110100011011101100011101011";

string RepackAsString(string s, int stride = 8) {
    var result = new StringBuilder();
    for (var i = 0; i < s.Length; i += stride) {
        var c = '\0';
        for (var j = 0; j < stride; j++) {
            c <<= 1;
            var index = i + j;
            if (index < s.Length) {
                var currentChar = s[index];
                switch (currentChar) {
                    case '0': break;
                    case '1': c |= (char)1; break;
                    default: throw new InvalidOperationException($"Unexpected character ‘{currentChar}’ at index {index}.");
                }
            }
        }
        result.Append(c);
    }
    return result.ToString();
}

Console.WriteLine(RepackAsString(barString)); // Ӗr,qhݎ°
HelloNewW commented 3 years ago

Hi,

My goal is to encode integer(max 7 digits) to small string\int values . Write that value to a txt file and mssqldb cell.

Load that file from machine that connected to a label printer. Configure label that contain that value with code 128 font .

What is the best way to do it ? maybe it better use another barcode format and not code128
Where can I find font 128 that working ... ?

Thanks

binki commented 3 years ago

Hi @HelloNewW

I thought that things like CODE128 fonts worked differently than that. The idea with such a font would be that you write the letter “a” and then the font renders “a” as one of the bar sequences used to represent “a”. To use such a font, you do need to be somewhat aware of how the barcode works so that you can use a character to represent the bar sequence that occurs at the beginning and end of the code and provide any check digits appropriately.

This is not something that BarcodeLib was really designed to help with. It outputs the bar sequence itself which you could turn into a graphic by drawing onto a bitmap or building an SVG. It would not be easy without a lookup table and understanding how the barcode works to take that bar sequence and turn it back into a sequence of characters that could be used with a barcode font. However, you might look at BarcodeLib’s source code to help with understanding how to use barcode font (if that is how you prefer to learn things)—though you might be better off reading such a font’s user manual and the WIkipedia article on Code 128.

However, again, this project does not really provide anything that helps with using such a font and it seems to be out of scope for this project (for now at least…? ;-)).