Results 1 to 16 of 16

Thread: [RESOLVED] Can't concatenate to string

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2005
    Posts
    334

    Resolved [RESOLVED] Can't concatenate to string

    Code:
            Dim a As String = ""
            Dim x As Integer = 0
            Dim y As String = ""
            Dim z As Integer = 0
            Dim w As String = ""
            For x = 0 To 9
                z = CInt(Val("&H" & x.ToString("00")))
                w = Chr(z).ToString
                a = a & w
                w = Int(x / 10).ToString
                a = a & w
                w = (x Mod 10).ToString
                a = a & w
            Next
    In debugging I can see that w gets the expected values, but they aren't concatenated to a.

    I can maybe see the 1st concatenation being a problem as w=Chr(0) is Nul, and other values are non-printable/control codes-but why does that stop the other concatenations?

    If I remove the 1st concatenation then I get the expected values-but if I include it then I get no concatenation from any of the lines.

    Of course I would like to know why this is happening, but even if you can't explain that maybe you can suggest another approach.

    What I'm trying to do is build a translation table for COBOL packed fields. Those store digits in nybbles so I'm trying to build a table that includes the character represented by each combination of digits followed by the string containing those two digits (e.g. &H00 = the two digit string "00", &H01 = the two digit string "01", etc.). I'd then use IndexOf to find the character that represents the hex code, and Substring to return the next two characters-the 'decoded' hex.

    Thanks.

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

    Re: Can't concatenate to string

    Bcd?
    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

  3. #3
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: Can't concatenate to string

    A string should end with null value but it seems that w = Chr(z).ToString returns a character that overwrites the ending value so “a” has no ending Null value.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    May 2005
    Posts
    334

    Re: Can't concatenate to string

    Hmm, I can see why that would cause a to have no value-but I can't see why Chr(z).ToString doesn't return a string ending in a Null value? (Except possibly for chr(0) which is the Null value. But I've tried it starting with x=1 also.)

    Of course why's often have no discernible answers-so I thank you for explaining what is happening. Thanks.

    And yes, COBOL packed fields are a form of BCD.

  5. #5
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Can't concatenate to string

    what is the point of this line?

    Code:
    z = CInt(Val("&H" & x.ToString("00")))
    it produces the same result as

    Code:
    z = x

  6. #6
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: Can't concatenate to string

    One question though, why use Chr(z).ToString instead of this z.ToString?

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

    Re: Can't concatenate to string

    if you are getting Packed Decimals then you must be reading strings. this should help.

    Code:
            Dim b As Byte
            Dim bcd As New List(Of Byte)
            '
            'Decimal:    0     1     2     3     4     5     6     7     8     9
            'BCD:     0000  0001  0010  0011  0100  0101  0110  0111  1000  1001
            'Packed:  00000001  00100011  01000101  01100111  10001001
            'generate packed bytes for test
            Dim x As Integer = 0
            'the Do Loop generates this
            'Packed:  00000001  00100011  01000101  01100111  10001001
            Do While x <= 9
                b = CByte(x)
                x += 1
                b = b << 4
                b = b Or CByte(x)
                Debug.WriteLine(Convert.ToString(b, 2).PadLeft(8, "0"c))
                bcd.Add(b)
                x += 1
            Loop
            '
            'convert packed bytes to a string
            Dim s As String = System.Text.Encoding.GetEncoding(1252).GetChars(bcd.ToArray)
            ' s = #Eg‰
            'at this point s should look like a string you are receiving
            Dim nib1, nib2 As Byte
            'convert packed bytes to nibbles
            For Each ch As Char In s
                b = CByte(Asc(ch))
                nib1 = CByte(b And &HF0)
                nib1 = nib1 >> 4
                nib2 = CByte(b And &HF)
                Debug.WriteLine(nib1.ToString & " " & nib2.ToString)
            Next
    Last edited by dbasnett; Feb 4th, 2009 at 02:21 PM.
    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

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    May 2005
    Posts
    334

    Re: Can't concatenate to string

    In both cases those are remnants of failed approaches. Should have cleaned up the code a little better before posting it. Sorry.

    I found a snippet that displays, as a string, the hex code of a byte-so I'm now trying an approach that directly decodes the bytes vs. constructing a string/table I can look them up in.

    OTOH DBasnett's snippet might work too. Thanks. (Didn't see his reply until after I'd posted mine.)

  9. #9
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: Can't concatenate to string

    why don't you use x.ToString("X2") to convert it to hex value?

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    May 2005
    Posts
    334

    Re: Can't concatenate to string

    Yes, I'm doing that now. I didn't do it before because I hadn't run across that format for the ToString method. The following works to dump the hex values for the characters loaded into a byte array.

    Dim s As String
    Dim b As Byte
    For x As Integer = 0 To 9
    b = ByteArray(x)
    s = b.ToString("X2")
    Console.WriteLine(s)
    Next

    Thanks.

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

    Re: Can't concatenate to string

    Quote Originally Posted by VBDT
    why don't you use x.ToString("X2") to convert it to hex value?
    how does that help?
    he has bytes that has 2 - 4 bit values in it. the four bit values will be one of these
    Code:
            'BCD:     0000  0001  0010  0011  0100  0101  0110  0111  1000  1001
            'Decimal:    0     1     2     3     4     5     6     7     8     9
    i guess what i posted didn't work.
    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

  12. #12
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: Can't concatenate to string

    Quote Originally Posted by dbasnett
    how does that help?
    he has bytes that has 2 - 4 bit values in it. the four bit values will be one of these
    Code:
            'BCD:     0000  0001  0010  0011  0100  0101  0110  0111  1000  1001
            'Decimal:    0     1     2     3     4     5     6     7     8     9
    i guess what i posted didn't work.
    We are talking about hex value and one byte. One byte is from 0 to 255 and represents two hex chars. Thus 255 in hex will be FF. so he is converting byte to byte.

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

    Re: Can't concatenate to string

    Quote Originally Posted by VBDT
    We are talking about hex value and one byte. One byte is from 0 to 255 and represents two hex chars. Thus 255 in hex will be FF. so he is converting byte to byte.
    i am lost. if this is about Packed BCD FF is not possible (1111 is not a BCD number). the largest numeric value for a byte would be Hex 99 (1001 1001).

    i converted a byte to two nibbles, but it looks like he wanted a table, not actual data converted.
    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

  14. #14
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: Can't concatenate to string

    Quote Originally Posted by dbasnett
    i am lost. if this is about Packed BCD FF is not possible (1111 is not a BCD number). the largest numeric value for a byte would be Hex 99 (1001 1001).

    i converted a byte to two nibbles, but it looks like he wanted a table, not actual data converted.
    Well I am not sure what exactly he is using for but he wants to convert the bites to hex values. The largest byte vale is 255 in hex FF. here is it from MSDN: “Byte Data Type holds unsigned 8 bit (1 byte) integers that range in value from 0 though 255.” And SByte “Holds signed 8-bit (1-byte) integers that range in value from -128 through 127” which may have the leading digit 1 that means negative value. As I see you are talking about binary values not hex values.

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

    Re: [RESOLVED] Can't concatenate to string

    maybe the OP will clear up the mystery, he must have the answer as this is resolved.

    and "As I see you are talking about binary values not hex values." HEX is a string representation of a binary value.
    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

  16. #16
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: [RESOLVED] Can't concatenate to string

    Quote Originally Posted by dbasnett
    maybe the OP will clear up the mystery, he must have the answer as this is resolved.

    and "As I see you are talking about binary values not hex values." HEX is a string representation of a binary value.
    The hex value is not only a string representation of binary values but it is the compressed form of it. So what is wrong about representing the binary value as it is in the memory such 0100 0101 string? This is a string representation of a binary value that has eight chars.
    The hex value has a base of 16 and one character can represent four chars of binary string. So let say an image in RTF format (which is a string) is saved in hex and they use binary string representation of it instead. The image would have occupy at list four times more memory that the hex values. Every four bits of binary string can be converted to on hex value. It is simply a compression.

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