Why is my ASCII char to int conversion failing?

2019-08-29 03:45发布

问题:

According to the chart here:

http://www.idautomation.com/barcode-faq/code-128/

This character:

Ë

equates to the value 103.

Yet this code:

string barcode = textBoxRawCode128.Text.Trim(); 
. . .
int runningTotal = ConvertToASCIIInt(barCode[0]);
. . .

private int ConvertToASCIIInt(char valToConvertToASCII)
{
    const int ASCII_ADJUSTMENT_VAL = 32;
    return valToConvertToASCII - ASCII_ADJUSTMENT_VAL;
}

...when the value in the textbox and thus of barcode is "ËTry another string.", thus where barcode[0] is "Ë", returns a value of 171 instead of 103...???

And according to this chart: http://www.adams1.com/128table.html, the value corresponding to 103 is ‡, but when I set barCode to "‡Try another string.", the returned value is 8193...??? Curiouser and curiouser...

Note: A related/preliminary post is Is this code for calculating Code128 barcode check digits correct?

回答1:

Keep in mind that to find the correct number for the Code 128 symbol you have to subtract 32, therefore to get the ASCII value of the Code 128 symbol 103, you'll have to add 32, giving you 135 which is not 7-bit ASCII. Adams has it right, but since it's 'high-ASCII' you get into the mess of code pages. So, depending on the language of the PC and if the applications that touch the string are using DBCS or Unicode or 8-bit ASCII, you may find different characters because the international standards differ. The character given by Russ Adams can be found if you bring up the Small Fonts typefont in the Windows Character Map application. Look at the character under "0x87" which is 135 in decimal.

The IDAutomation people are using their own algorithm to arrive at a barcode character based on the letters you feed it, so if they say they need a 'Ë' to get a 103, then that's what they need. 'Ë' doesn't equate to a 103, it's just what gets their software to cough up a 103. This is all to work around the conversion from a numeric 7-bit standard to the vendor's method for producing bars.

Sadly, different symbologies do not use the same algorithms for encoding data or deriving checksums, so each barcode type has to have its own software.



回答2:

Actually the character you have shown has the value of Ë = 203. Small g is the character which has the ascii value 103.

Hence 203 - 32 = 171.

Reference