Results 1 to 5 of 5

Thread: [RESOLVED] [2005] Byte array loses data when converted to ascii string

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jun 1999
    Location
    California, USA
    Posts
    662

    Resolved [RESOLVED] [2005] Byte array loses data when converted to ascii string

    I'm writing a piece of software that will print barcodes using a font. (My method of manually drawing the barcode didn't yield the results I was looking for and I'm on a deadline.) So now I'm using a font. Unfortunatly, because it's code 128 (types b and c, intermixed) I have to pre-encode the strings, calculate a checksum, etc. No big problem.

    Because code 128 only has 156 different symbols, the font is ASCII (i.e. it uses character codes from 0 to 255 and is only 1 byte per character.) It should be noted that several of these symbols are control codes and must be displayed correctly for the barcode to work at all.

    The encoding I'm doing works with a byte array to make individual changes easier.

    The problem arises when I need to draw the string. I have to convert the byte array to a string first. For some reason, VB likes to replace certain character values with a question mark. This is not some on screen representation of a character that can't be displayed. It's actually substituting one character for another in the variable.

    This should have nothing to do with unicode to ascii and having ascii unable to represent the character.

    vb Code:
    1. Dim bc1Text As String = "+H822023450022406"
    2.             Dim bc1EncodedBytes As Byte() = Encode128(bc1Text)
    3.  
    4.             Dim TextEncoder As New System.Text.ASCIIEncoding
    5.             Dim bc1EncodedText As String = TextEncoder.GetString(bc1EncodedBytes)

    The last line is where the data is changing. bc1EncodedText now has different data than bc1EncodedBytes.

    An example (and I've checked this by converting the resultant string back into a byte array and reading the value) is that bc1EncodedBytes(0) and bc1EncodedBytes(1) have a value of 155 and 150 respectively, but the characters in the string and a byte array made from the string have the values of 63 and 63 respectively.

    Since I know that Ascii can represent characters 155 and 150, even if there's no visual representation, I'm not entirely sure why they are being converted to a question mark.

    Is there any way to prevent it from being converted to a question mark so that I can properly use the control codes in my barcode font?

    This is being used with Graphics.DrawString, so keeping it as a byte array won't work either.
    Last edited by agent; Sep 1st, 2008 at 04:31 AM.

  2. #2
    Frenzied Member
    Join Date
    Mar 2005
    Location
    Sector 001
    Posts
    1,577

    Re: [2005] Byte array loses data when converted to ascii string

    What is Encode128?
    Try the last line with
    Code:
    Dim bc1EncodedText As String = Encoding.Default.GetString(bc1EncodedBytes)
    or
    Code:
    Dim bc1EncodedText As String = Encoding.ASCII.GetString(bc1EncodedBytes)
    VB 2005, Win Xp Pro sp2

  3. #3
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: [2005] Byte array loses data when converted to ascii string

    Code:
            Dim buffer() As Byte = New Byte() {10, 20, 30, 40, 50, 64, 65, 127, 128, 150, 254, 255}
            Dim buffAsStr As String
            Dim AllByteEnc As System.Text.Encoding = System.Text.Encoding.GetEncoding("Windows-1252")
            buffAsStr = AllByteEnc.GetChars(buffer)
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Jun 1999
    Location
    California, USA
    Posts
    662

    Re: [2005] Byte array loses data when converted to ascii string

    Quote Originally Posted by Half
    What is Encode128?
    Try the last line with
    Code:
    Dim bc1EncodedText As String = Encoding.Default.GetString(bc1EncodedBytes)
    or
    Code:
    Dim bc1EncodedText As String = Encoding.ASCII.GetString(bc1EncodedBytes)
    The first one works. Thanks. Encode128 is the function that encodes the string for the Code128 font.

  5. #5
    Frenzied Member
    Join Date
    Mar 2005
    Location
    Sector 001
    Posts
    1,577

    Re: [RESOLVED] [2005] Byte array loses data when converted to ascii string

    'ASCII' here is so misleading. I (and, as I often see, others) assumed it is the extended ascii. Well, it's not - ASCII in .net means the 7 bit stuff, things above the 127 get's broken. If you look at what intellisense is saying it makes sense. They should have bolded the relevant 7 and included 'use "default" if you are after the 256 bit magic'.
    VB 2005, Win Xp Pro sp2

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width