Tuesday, July 28, 2009

Working with ASCII values in C++?

I'm trying to put interleaved 2 of 5 barcodes on a .pdf report that I'm generating, but am running into trouble. The barcode font expects me to give it ASCII values rather than CString values. Specifically, the ASCII value has to be the CString value plus 33: i.e. to represent the number 1, I have to give the font the ASCII value 34, to represent the number 10, I have to give the font the ASCII value 43, and so on.





I'm so confused. I have to take a CString, turn it into an ASCII value, add 33 to that ASCII value, and then print this new ASCII value in a particular barcode font. On top of that, the virtual print engine I'm using to generate the .pdf report (Ideal Software) expects the value that I print to be a LPCSTR.





Can anyone give me any tips on working with ASCII characters and converting them to and from the various formats that I need?

Working with ASCII values in C++?
CStrings nominally store ASCII values. "A", "1" are ASCII values.





In this case, you're not really storing ASCII's in the CString, but the N+33 values the barcode wants. Just focus on putting those values into the right places in the CString. (Hint: since you have only a handful of possible values, a simple subscript into a character array will be very fast.) Make sure the CString has a zero-byte terminator to be safe.





And I think referencing the CString in a function call will automatically convert it to LPCTSTR.





Hope that helps.
Reply:If you cast "1" to int you'll have a value of 49 (not 33) - the digits 0-9 have values from 48-57. So you'll have to use the cast value - 16, then cast it back to a string to print it. (Or get a more sand algorithm - like print the actual character using a 2 of 5 font. Then you just have to create the lead-in and lead-our characters [if 2 of 5 works that way - it's a barcode I'm not familiar with] and the checksum.).


No comments:

Post a Comment