string problem

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

On my form i have a message box called txtItemDesc that displays the french
phrase qualité Père Noël. Now then when i run this code on that text box:

Dim chrArr() As Char
chrArr = txtItemDesc.Text.ToCharArray
Dim pos As Integer
While pos < chrArr.Length
Dim c As Char
MsgBox(Asc(chrArr(pos)) & " " & chrArr(pos))
pos = pos + 1
End While

I get this as my message box reply:
113 q (which is right in the ascii char set)
117 u
97 a
108 l
105 i
116 t
233 é (this is wrong in the extend ascii char set it should be 130)

How do i get this message box to print out the write ascii char value?
 
john said:
On my form i have a message box called txtItemDesc that displays the french
phrase qualit? P?re No?l. Now then when i run this code on that text box:

Dim chrArr() As Char
chrArr = txtItemDesc.Text.ToCharArray
Dim pos As Integer
While pos < chrArr.Length
Dim c As Char
MsgBox(Asc(chrArr(pos)) & " " & chrArr(pos))
pos = pos + 1
End While

I get this as my message box reply:
113 q (which is right in the ascii char set)
117 u
97 a
108 l
105 i
116 t
233 ? (this is wrong in the extend ascii char set it should be 130)

How do i get this message box to print out the write ascii char value?

There's no single "extended ASCII" character set, and the plain ASCII
encoding only goes as far as 127.

You should use the *unicode* character you're interested in, and
preferrably avoid Asc which uses (IIRC) the default encoding for the
platform, which could be confusing things.

See http://www.pobox.com/~skeet/csharp/unicode.html
 
The problem i have is that i'm sending this data to a case labeling printer
that requires ascii. Now it does use the extended ascii set i know there are
multiples of them but the most common one of them is used. Here is a page
that has the extend chars that i need: http://www.lookuptables.com/
so is there a way to get those extended char values from the input string?
 
john said:
The problem i have is that i'm sending this data to a case labeling printer
that requires ascii.

No, it requires more than just ASCII by the sounds of it - as I said
before, actual ASCII only goes up as far as 127.
Now it does use the extended ascii set i know there are
multiples of them but the most common one of them is used. Here is a page
that has the extend chars that i need: http://www.lookuptables.com/
so is there a way to get those extended char values from the input string?

That's far from the most common these days (despite what the web page
says) - it's an encoding from the DOS days. I *suspect* it's code page
437, which you can get with Encoding.GetEncoding(437). It's worth
trying, certainly.
 
thanks for the help Jon. You are right. it looks like the 437 char set is
the one I want. example of the set can be found at
http://www.georgehernandez.com/xComputers/CharacterSets/OEM.htm
I double check my label printers on my line i sent the ascii value of
char(130) and i recieved the results that i expected. now then i'm having a
problem with the conversion of the string still maybe you or someone can
point out where i'm going wrong. I'm still getting a code of 233 for char é
when it should be 130
'conversion code for my 437 encoding
' Create two different encodings.
Dim ibm437 As Encoding = Encoding.GetEncoding(437)
Dim [unicode] As Encoding = Encoding.Unicode

' Convert the string into a byte[].
Dim unicodeBytes As Byte() = [unicode].GetBytes(Me.TextBox1.Text)

' Perform the conversion from one encoding to the other.
Dim ibm437Bytes As Byte() = Encoding.Convert([unicode], ibm437,
unicodeBytes)

' Convert the new byte[] into a char[] and then into a string.
Dim imb437Chars(ibm437.GetCharCount(ibm437Bytes, 0,
ibm437Bytes.Length)) As Char
ibm437.GetChars(ibm437Bytes, 0, ibm437Bytes.Length, imb437Chars, 0)
Dim imb437String As New String(imb437Chars)

'checking my results from the conversion in a message box
Dim chrArr() As Char
chrArr = imb437String.ToCharArray

Dim pos As Integer
While pos < chrArr.Length
Dim c As Char
MsgBox(Asc(chrArr(pos)) & " " & chrArr(pos))
pos = pos + 1
End While
 
it seems to me by doing this it is converting it back into a uncode string
because i'm putting the Encoded 437 chars back into a windows char array.

right?
john said:
thanks for the help Jon. You are right. it looks like the 437 char set is
the one I want. example of the set can be found at
http://www.georgehernandez.com/xComputers/CharacterSets/OEM.htm
I double check my label printers on my line i sent the ascii value of
char(130) and i recieved the results that i expected. now then i'm having a
problem with the conversion of the string still maybe you or someone can
point out where i'm going wrong. I'm still getting a code of 233 for char é
when it should be 130
'conversion code for my 437 encoding
' Create two different encodings.
Dim ibm437 As Encoding = Encoding.GetEncoding(437)
Dim [unicode] As Encoding = Encoding.Unicode

' Convert the string into a byte[].
Dim unicodeBytes As Byte() = [unicode].GetBytes(Me.TextBox1.Text)

' Perform the conversion from one encoding to the other.
Dim ibm437Bytes As Byte() = Encoding.Convert([unicode], ibm437,
unicodeBytes)

' Convert the new byte[] into a char[] and then into a string.
Dim imb437Chars(ibm437.GetCharCount(ibm437Bytes, 0,
ibm437Bytes.Length)) As Char
ibm437.GetChars(ibm437Bytes, 0, ibm437Bytes.Length, imb437Chars, 0)
Dim imb437String As New String(imb437Chars)

'checking my results from the conversion in a message box
Dim chrArr() As Char
chrArr = imb437String.ToCharArray

Dim pos As Integer
While pos < chrArr.Length
Dim c As Char
MsgBox(Asc(chrArr(pos)) & " " & chrArr(pos))
pos = pos + 1
End While

Jon Skeet said:
No, it requires more than just ASCII by the sounds of it - as I said
before, actual ASCII only goes up as far as 127.


That's far from the most common these days (despite what the web page
says) - it's an encoding from the DOS days. I *suspect* it's code page
437, which you can get with Encoding.GetEncoding(437). It's worth
trying, certainly.
 
nevermind thanks for the help i started to write my own converter for it.
it's working now.

john said:
it seems to me by doing this it is converting it back into a uncode string
because i'm putting the Encoded 437 chars back into a windows char array.

right?
john said:
thanks for the help Jon. You are right. it looks like the 437 char set is
the one I want. example of the set can be found at
http://www.georgehernandez.com/xComputers/CharacterSets/OEM.htm
I double check my label printers on my line i sent the ascii value of
char(130) and i recieved the results that i expected. now then i'm having a
problem with the conversion of the string still maybe you or someone can
point out where i'm going wrong. I'm still getting a code of 233 for char é
when it should be 130
'conversion code for my 437 encoding
' Create two different encodings.
Dim ibm437 As Encoding = Encoding.GetEncoding(437)
Dim [unicode] As Encoding = Encoding.Unicode

' Convert the string into a byte[].
Dim unicodeBytes As Byte() = [unicode].GetBytes(Me.TextBox1.Text)

' Perform the conversion from one encoding to the other.
Dim ibm437Bytes As Byte() = Encoding.Convert([unicode], ibm437,
unicodeBytes)

' Convert the new byte[] into a char[] and then into a string.
Dim imb437Chars(ibm437.GetCharCount(ibm437Bytes, 0,
ibm437Bytes.Length)) As Char
ibm437.GetChars(ibm437Bytes, 0, ibm437Bytes.Length, imb437Chars, 0)
Dim imb437String As New String(imb437Chars)

'checking my results from the conversion in a message box
Dim chrArr() As Char
chrArr = imb437String.ToCharArray

Dim pos As Integer
While pos < chrArr.Length
Dim c As Char
MsgBox(Asc(chrArr(pos)) & " " & chrArr(pos))
pos = pos + 1
End While

Jon Skeet said:
The problem i have is that i'm sending this data to a case labeling printer
that requires ascii.

No, it requires more than just ASCII by the sounds of it - as I said
before, actual ASCII only goes up as far as 127.

Now it does use the extended ascii set i know there are
multiples of them but the most common one of them is used. Here is a page
that has the extend chars that i need: http://www.lookuptables.com/
so is there a way to get those extended char values from the input string?

That's far from the most common these days (despite what the web page
says) - it's an encoding from the DOS days. I *suspect* it's code page
437, which you can get with Encoding.GetEncoding(437). It's worth
trying, certainly.
 
john said:
thanks for the help Jon. You are right. it looks like the 437 char set is
the one I want. example of the set can be found at
http://www.georgehernandez.com/xComputers/CharacterSets/OEM.htm
I double check my label printers on my line i sent the ascii value of
char(130) and i recieved the results that i expected.

It's not entirely clear *exactly* what you're doing. Where is your
input, what are you doing, etc? What's the flow of your app?

It's very rarely a good idea to take a string, convert it into bytes in
one encoding and then convert it into characters in another encoding.
 
Back
Top