|
-
Feb 4th, 2009, 01:12 PM
#1
Thread Starter
Hyperactive Member
[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.
-
Feb 4th, 2009, 01:19 PM
#2
Re: Can't concatenate to string
-
Feb 4th, 2009, 01:22 PM
#3
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.
-
Feb 4th, 2009, 01:26 PM
#4
Thread Starter
Hyperactive Member
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.
-
Feb 4th, 2009, 01:34 PM
#5
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
-
Feb 4th, 2009, 01:36 PM
#6
Re: Can't concatenate to string
One question though, why use Chr(z).ToString instead of this z.ToString?
-
Feb 4th, 2009, 01:50 PM
#7
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.
-
Feb 4th, 2009, 01:50 PM
#8
Thread Starter
Hyperactive Member
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.)
-
Feb 4th, 2009, 02:01 PM
#9
Re: Can't concatenate to string
why don't you use x.ToString("X2") to convert it to hex value?
-
Feb 4th, 2009, 02:18 PM
#10
Thread Starter
Hyperactive Member
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.
-
Feb 4th, 2009, 02:20 PM
#11
Re: Can't concatenate to string
 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.
-
Feb 4th, 2009, 02:43 PM
#12
Re: Can't concatenate to string
 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.
-
Feb 4th, 2009, 02:51 PM
#13
Re: Can't concatenate to string
 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.
-
Feb 4th, 2009, 03:06 PM
#14
Re: Can't concatenate to string
 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.
-
Feb 4th, 2009, 03:14 PM
#15
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.
-
Feb 4th, 2009, 07:09 PM
#16
Re: [RESOLVED] Can't concatenate to string
 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|