Results 1 to 16 of 16

Thread: ascii code

  1. #1

    Thread Starter
    Fanatic Member daimous's Avatar
    Join Date
    Aug 2005
    Posts
    657

    ascii code

    hi guys! how can i get the character value of this ascii code: 252,253 and 254? thanks in advance!!!

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: ascii code

    Convert.ToChar(UnicodeValue)

    For ASCII characters the Unicode value and ASCII value match, but Convert.ToChar can be used for non-ASCII characters too.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Fanatic Member daimous's Avatar
    Join Date
    Aug 2005
    Posts
    657

    Re: ascii code

    why i wont get the character for this ascii value. i have the snippet of my code below.
    Code:
    MessageBox.Show(Convert.ToString(Convert.ToChar(130)));

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: ascii code

    Hmmm... I just did a bit of testing and it seems that there are some characters that Unicode and ASCII don't agree on. I used Convert.ToChar, ChrW and Chr to convert values from 1 to 255 in VB and here's the output:
    Code:
    Value: ToChar ChrW Chr
    1:   
    2:   
    3:   
    4:   
    5:   
    6:   
    7:   
    8:   
    9: 	 	 	
    10: 
     
     
    
    11:   
    12:   
    13: 
     
     
    
    14:   
    15:   
    16:   
    17:   
    18:   
    19:   
    20:   
    21:   
    22:   
    23:   
    24:   
    25:   
    26:   
    27:   
    28:   
    29:   
    30:   
    31:   
    32:      
    33: ! ! !
    34: " " "
    35: # # #
    36: $ $ $
    37: % % %
    38: & & &
    39: ' ' '
    40: ( ( (
    41: ) ) )
    42: * * *
    43: + + +
    44: , , ,
    45: - - -
    46: . . .
    47: / / /
    48: 0 0 0
    49: 1 1 1
    50: 2 2 2
    51: 3 3 3
    52: 4 4 4
    53: 5 5 5
    54: 6 6 6
    55: 7 7 7
    56: 8 8 8
    57: 9 9 9
    58: : : :
    59: ; ; ;
    60: < < <
    61: = = =
    62: > > >
    63: ? ? ?
    64: @ @ @
    65: A A A
    66: B B B
    67: C C C
    68: D D D
    69: E E E
    70: F F F
    71: G G G
    72: H H H
    73: I I I
    74: J J J
    75: K K K
    76: L L L
    77: M M M
    78: N N N
    79: O O O
    80: P P P
    81: Q Q Q
    82: R R R
    83: S S S
    84: T T T
    85: U U U
    86: V V V
    87: W W W
    88: X X X
    89: Y Y Y
    90: Z Z Z
    91: [ [ [
    92: \ \ \
    93: ] ] ]
    94: ^ ^ ^
    95: _ _ _
    96: ` ` `
    97: a a a
    98: b b b
    99: c c c
    100: d d d
    101: e e e
    102: f f f
    103: g g g
    104: h h h
    105: i i i
    106: j j j
    107: k k k
    108: l l l
    109: m m m
    110: n n n
    111: o o o
    112: p p p
    113: q q q
    114: r r r
    115: s s s
    116: t t t
    117: u u u
    118: v v v
    119: w w w
    120: x x x
    121: y y y
    122: z z z
    123: { { {
    124: | | |
    125: } } }
    126: ~ ~ ~
    127:   
    128: ? ? €
    129:   
    130: ? ? ‚
    131: ? ? ƒ
    132: ? ? „
    133: ? ? …
    134: ? ? †
    135: ? ? ‡
    136: ? ? ˆ
    137: ? ? ‰
    138: ? ? Š
    139: ? ? ‹
    140: ? ? Œ
    141:   
    142: ? ? Ž
    143:   
    144:   
    145: ? ? ‘
    146: ? ? ’
    147: ? ? “
    148: ? ? ”
    149: ? ? •
    150: ? ? –
    151: ? ? —
    152: ? ? ˜
    153: ? ? ™
    154: ? ? š
    155: ? ? ›
    156: ? ? œ
    157:   
    158: ? ? ž
    159: ? ? Ÿ
    160:      
    161: ¡ ¡ ¡
    162: ¢ ¢ ¢
    163: £ £ £
    164: ¤ ¤ ¤
    165: ¥ ¥ ¥
    166: ¦ ¦ ¦
    167: § § §
    168: ¨ ¨ ¨
    169: © © ©
    170: ª ª ª
    171: « « «
    172: ¬ ¬ ¬
    173: * * *
    174: ® ® ®
    175: ¯ ¯ ¯
    176: ° ° °
    177: ± ± ±
    178: ² ² ²
    179: ³ ³ ³
    180: ´ ´ ´
    181: µ µ µ
    182: ¶ ¶ ¶
    183: · · ·
    184: ¸ ¸ ¸
    185: ¹ ¹ ¹
    186: º º º
    187: » » »
    188: ¼ ¼ ¼
    189: ½ ½ ½
    190: ¾ ¾ ¾
    191: ¿ ¿ ¿
    192: À À À
    193: Á Á Á
    194: Â Â Â
    195: Ã Ã Ã
    196: Ä Ä Ä
    197: Å Å Å
    198: Æ Æ Æ
    199: Ç Ç Ç
    200: È È È
    201: É É É
    202: Ê Ê Ê
    203: Ë Ë Ë
    204: Ì Ì Ì
    205: Í Í Í
    206: Î Î Î
    207: Ï Ï Ï
    208: Ð Ð Ð
    209: Ñ Ñ Ñ
    210: Ò Ò Ò
    211: Ó Ó Ó
    212: Ô Ô Ô
    213: Õ Õ Õ
    214: Ö Ö Ö
    215: × × ×
    216: Ø Ø Ø
    217: Ù Ù Ù
    218: Ú Ú Ú
    219: Û Û Û
    220: Ü Ü Ü
    221: Ý Ý Ý
    222: Þ Þ Þ
    223: ß ß ß
    224: à à à
    225: á á á
    226: â â â
    227: ã ã ã
    228: ä ä ä
    229: å å å
    230: æ æ æ
    231: ç ç ç
    232: è è è
    233: é é é
    234: ê ê ê
    235: ë ë ë
    236: ì ì ì
    237: í í í
    238: î î î
    239: ï ï ï
    240: ð ð ð
    241: ñ ñ ñ
    242: ò ò ò
    243: ó ó ó
    244: ô ô ô
    245: õ õ õ
    246: ö ö ö
    247: ÷ ÷ ÷
    248: ø ø ø
    249: ù ù ù
    250: ú ú ú
    251: û û û
    252: ü ü ü
    253: ý ý ý
    254: þ þ þ
    255: ÿ ÿ ÿ
    Note the section from 128 to 159. I then used Reflector to have a look at the implementation of Chr and here it is:
    VB Code:
    1. Public Shared Function [Chr](ByVal CharCode As Integer) As Char
    2.       Dim ch1 As Char
    3.       If ((CharCode < -32768) OrElse (CharCode > 65535)) Then
    4.             Throw New ArgumentException(Utils.GetResourceString("Argument_RangeTwoBytes1", New String() { "CharCode" }))
    5.       End If
    6.       If ((CharCode >= 0) AndAlso (CharCode <= 127)) Then
    7.             Return Convert.ToChar(CharCode)
    8.       End If
    9.       Try
    10.             Dim num1 As Integer
    11.             Dim encoding1 As Encoding = Encoding.GetEncoding(Utils.GetLocaleCodePage)
    12.             If (encoding1.IsSingleByte AndAlso ((CharCode < 0) OrElse (CharCode > 255))) Then
    13.                   Throw ExceptionUtils.VbMakeException(5)
    14.             End If
    15.             Dim chArray1 As Char() = New Char(2  - 1) {}
    16.             Dim buffer1 As Byte() = New Byte(2  - 1) {}
    17.             Dim decoder1 As Decoder = encoding1.GetDecoder
    18.             If ((CharCode >= 0) AndAlso (CharCode <= 255)) Then
    19.                   buffer1(0) = CByte((CharCode And 255))
    20.                   num1 = decoder1.GetChars(buffer1, 0, 1, chArray1, 0)
    21.             Else
    22.                   buffer1(0) = CByte(((CharCode And 65280) >> 8))
    23.                   buffer1(1) = CByte((CharCode And 255))
    24.                   num1 = decoder1.GetChars(buffer1, 0, 2, chArray1, 0)
    25.             End If
    26.             ch1 = chArray1(0)
    27.       Catch exception1 As  Exception
    28.             Throw exception1
    29.       End Try
    30.       Return ch1
    31. End Function
    Icky! I then thought that maybe an Encoding object could do the job, but this:
    VB Code:
    1. MessageBox.Show(System.Text.Encoding.ASCII.GetString(New Byte() {130}))
    didn't work either. I then tried this:
    VB Code:
    1. MessageBox.Show(System.Text.Encoding.Default.GetString(New Byte() {130}))
    and it displayed the correct character. I don't know if that may change for different cultures or not but you can check that out if you like.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Fanatic Member daimous's Avatar
    Join Date
    Aug 2005
    Posts
    657

    Re: ascii code

    How can i run the code below to C#.net? 'cuase I'm trying to run it in c#.net but it has an error.
    Code:
    MessageBox.Show(System.Text.Encoding.Default.GetString(New Byte() {130}))
    By the way, Thanks a bunch!

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: ascii code

    Firstly you should look at the code to see what it is doing and then ask yourself how you would do that in C#. If you aren't able to make it work yourself then the next step is to use one of the code converters in my signature, or another if you have a favourite.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Fanatic Member daimous's Avatar
    Join Date
    Aug 2005
    Posts
    657

    Re: ascii code

    Thanks..I used your converters and its great!! how helpful it is! thanks soo much!!!

  8. #8

    Thread Starter
    Fanatic Member daimous's Avatar
    Join Date
    Aug 2005
    Posts
    657

    Re: ascii code

    is there any better way than the code below to get the ascii code for a particular character?
    Code:
    MessageBox.Show(Convert.ToInt16('ÿ').ToString());
    Thanks in advance!

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: ascii code

    That has the same issue as going the other way. If you use the code from before to create a Char object and then use that code to convert back to a number, you'll find that it doesn't round trip for that same range of characters while it does for the others. I guess you could use an Encoding object but I'll leave it up to you to experiment with that if you want.
    Last edited by jmcilhinney; Aug 16th, 2006 at 03:55 AM.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10

    Thread Starter
    Fanatic Member daimous's Avatar
    Join Date
    Aug 2005
    Posts
    657

    Re: ascii code

    Hi! i have i question. Since, the GetBytes encodes an array of character is there any way for me to just encode a single character? becuase i just want to encode a single character like '€' but GetBytes just wont allow me to do that or is just that i dont know yet. So, can anybody please help me. Thanks!

    VB Code:
    1. Encoding.Default.GetBytes(char[] chars)

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: ascii code

    Just create an array with one character in it. A string with one character in it will do the same job as GetBytes is overloaded.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  12. #12

    Thread Starter
    Fanatic Member daimous's Avatar
    Join Date
    Aug 2005
    Posts
    657

    Re: ascii code

    hi! i really need help...I can already get the ascii code for some characters like 'A','-','=', etc. using the code below, but when i try to get the ascii for this 'š' character there is always an error saying "Value was either too large or too small for an unsigned byte." can anybody please help me on how to solve this problem.

    Code:
    MessageBox.Show(Convert.ToString(Convert.ToByte('A'))); //OK
    MessageBox.Show(Convert.ToString(Convert.ToByte('š'))); //ERROR

  13. #13
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: ascii code

    Only ASCII characters have ASCII codes. If the value you get from a character is to big for a Byte then that means that it is not an ASCII character, because ASCII characters have values fro 0 to 255. This character set is quite limited, which is basically why the Unicode character set was created. Unicode characters have a two-byte value, so from 0 to 65535, which allows an enormous number of characters from many languages and other symbols as well. The first 256 Unicode characters have the same value as the equivalent ASCII characters, so your first line is OK. The second line is trying to convert a Unicode character with a value greater than 255, which is obviously not going to fit in a variable with a maximum value of 255. If you're using Unicode characters then you should be using Short variables at least. Note that a .NET Char object is two bytes in size. Also, you can use the Encoding.Unicode object to get a byte array that represents a string of Unicode characters, where each character is converted to two bytes.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  14. #14

    Thread Starter
    Fanatic Member daimous's Avatar
    Join Date
    Aug 2005
    Posts
    657

    Re: ascii code

    I've tried to get the the ascii code of this 'š' character using VB and i found out that it has a value of 154 which is in range (0-256) and is a ASCII character. Why is it can't fit to a byte?
    Code:
        Dim i As Integer
        For i = 0 To 255
            txtaddressee.Text = txtaddressee.Text & i & ": " & Chr(i) & Chr(13)
        Next
    Result:
    0: 1: 
    2: 
    3: 
    4: 
    5: 
    6: 
    7:
    8: 
    9:
    10:

    11:

    12:
    13:

    14: 
    15: 
    16: 
    17: 
    18: 
    19: 
    20: 
    21: 
    22: 
    23: 
    24: 
    25: 
    26: 
    27: 
    28: 
    29: 
    30: 
    31: 
    32:
    33: !
    34: "
    35: #
    36: $
    37: %
    38: &
    39: '
    40: (
    41: )
    42: *
    43: +
    44: ,
    45: -
    46: .
    47: /
    48: 0
    49: 1
    50: 2
    51: 3
    52: 4
    53: 5
    54: 6
    55: 7
    56: 8
    57: 9
    58: :
    59: ;
    60: <
    61: =
    62: >
    63: ?
    64: @
    65: A
    66: B
    67: C
    68: D
    69: E
    70: F
    71: G
    72: H
    73: I
    74: J
    75: K
    76: L
    77: M
    78: N
    79: O
    80: P
    81: Q
    82: R
    83: S
    84: T
    85: U
    86: V
    87: W
    88: X
    89: Y
    90: Z
    91: [
    92: \
    93: ]
    94: ^
    95: _
    96: `
    97: a
    98: b
    99: c
    100: d
    101: e
    102: f
    103: g
    104: h
    105: i
    106: j
    107: k
    108: l
    109: m
    110: n
    111: o
    112: p
    113: q
    114: r
    115: s
    116: t
    117: u
    118: v
    119: w
    120: x
    121: y
    122: z
    123: {
    124: |
    125: }
    126: ~
    127: 
    128: €
    129: 
    130: ‚
    131: ƒ
    132: „
    133: …
    134: †
    135: ‡
    136: ˆ
    137: ‰
    138: Š
    139: ‹
    140: Œ
    141: 
    142: Ž
    143: 
    144: 
    145: ‘
    146: ’
    147: “
    148: ”
    149: •
    150: –
    151: —
    152: ˜
    153: ™
    154: š
    155: ›
    156: œ
    157: 
    158: ž
    159: Ÿ
    160: *
    161: ¡
    162: ¢
    163: £
    164: ¤
    165: ¥
    166: ¦
    167: §
    168: ¨
    169: ©
    170: ª
    171: «
    172: ¬
    173: *
    174: ®
    175: ¯
    176: °
    177: ±
    178: ²
    179: ³
    180: ´
    181: µ
    182: ¶
    183: ·
    184: ¸
    185: ¹
    186: º
    187: »
    188: ¼
    189: ½
    190: ¾
    191: ¿
    192: À
    193: Á
    194: Â
    195: Ã
    196: Ä
    197: Å
    198: Æ
    199: Ç
    200: È
    201: É
    202: Ê
    203: Ë
    204: Ì
    205: Í
    206: Î
    207: Ï
    208: Ð
    209: Ñ
    210: Ò
    211: Ó
    212: Ô
    213: Õ
    214: Ö
    215: ×
    216: Ø
    217: Ù
    218: Ú
    219: Û
    220: Ü
    221: Ý
    222: Þ
    223: ß
    224: à
    225: á
    226: â
    227: ã
    228: ä
    229: å
    230: æ
    231: ç
    232: è
    233: é
    234: ê
    235: ë
    236: ì
    237: í
    238: î
    239: ï
    240: ð
    241: ñ
    242: ò
    243: ó
    244: ô
    245: õ
    246: ö
    247: ÷
    248: ø
    249: ù
    250: ú
    251: û
    252: ü
    253: ý
    254: þ
    255: ÿ

  15. #15
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: ascii code

    You're not learning from history. Remember a few posts ago when we determined that there was a lack of correspondence between ASCII and Unicode in the range 128 to 159? I'm pretty sure that 154 falls in that range. The Unicode value for that character is 353, which won't fit into a byte. I already alluded to this issue in post #9. You need to make a decision as to whether you're working with ASCII or Unicode and stick to it.
    MessageBox.Show(Convert.ToString(System.Text.Encoding.Default.GetBytes(new char[] { 'š' })[0]));
    That's what I implied in post #9.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  16. #16

    Thread Starter
    Fanatic Member daimous's Avatar
    Join Date
    Aug 2005
    Posts
    657

    Re: ascii code

    Thanks a bunch!!!I've already solved that problem with your help. But I still have a problem. Actually, im working with a simple encryption/decryption algorithm the could work both in C#.net and VB6, currently, i have the code below that try to ecnrypt whatever the text i pass to it. Now, my problem is i can't get the solution to decrypt it. can anybody please help me.

    Code:
            private void button1_Click(object sender, EventArgs e)
            {
                string str, password, Encrypt_code, Decrypt_code;
                str = @"abcdefghijklmnopqrstuvwxyz";
                password = "BCMD";
                Encrypt_code = RndCrypt(str, password, true);
                txtSubject.Text = Encrypt_code;
                Decrypt_code = RndCrypt(Encrypt_code, password, false);
                txtAddressee.Text = Decrypt_code;
             }
    
            public string RndCrypt(string str, string password, Boolean Encrypt)
            {
                byte tmp_str, tmp;
                byte[] string_bytes;
                string encStr,tmp_encStr;
                double key;
                int counter;
                key = 0;
                counter = 1;
                encStr = "";
                Encoding ascii = Encoding.Default;
                foreach (char i in password)
                {
                    key += Convert.ToDouble(Convert.ToByte(i) * (counter * 2));
                    counter++;
    
                }
                key = key % 255;
                counter = 1;
    
                string_bytes = ascii.GetBytes(str);
                foreach (byte b in string_bytes)
                {
                    tmp = Convert.ToByte((key * counter) % 255);
                    if (Encrypt)
                    {
                        //encryption happens here
                        tmp_encStr = ascii.GetString(new byte[] { (Convert.ToByte(((tmp ^ b) % 222) + 33)) }).ToString();
      
                    }
                    else
                    {
                        //decryption happens here
                        //but i dont have yet the solution
                    }
                    encStr += tmp_encStr;
                    counter++;
                }
                return encStr;
            }

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