34

Consider the string:

string str="A C# string";

What would be most efficient way to printout the ASCII value of each character in str using C#.

Shamim Hafiz - MSFT
  • 21,454
  • 43
  • 116
  • 176
  • 1
    Define "most efficient". :) Also, possible duplicate of [How to get ASCII value of string in C#](http://stackoverflow.com/questions/400733/how-to-get-ascii-value-of-string-in-c). Also, do you really mean ASCII here? – bzlm Feb 15 '11 at 11:23
  • 6
    Well, technically, C# characters use UNICODE, not ASCII. – Frédéric Hamidi Feb 15 '11 at 11:24

4 Answers4

38

Just cast each character to an int:

for (int i = 0; i < str.length; i++)  
  Console.Write(((int)str[i]).ToString());
Jorge Córdoba
  • 51,063
  • 11
  • 80
  • 130
  • 1
    That won't limit to ASCII, will it? – bzlm Feb 15 '11 at 11:23
  • 1
    +1 - Can also be iterated using `foreach(char c in str.ToCharArray())` – Joel Etherton Feb 15 '11 at 11:26
  • As long as the characters are ASCII you'll get ASCII, if you use unicode characters in your string you'll get their values too. What's wrong with casting? Is the fastest way, after all the character representation itself is an integer anyway (4 bytes if I recall correctly) – Jorge Córdoba Feb 15 '11 at 11:31
  • No... characters are 2 bytes, so short. But yes, full unicode requires 32 bits, and some characters are deconstructed as two 16-bits chars (surrogates). – xanatos Feb 15 '11 at 12:27
  • 2
    @Joel: Or you can just iterate using `foreach (char c in str)`. No need even for the `ToCharArray`. – LukeH Feb 15 '11 at 15:39
  • @LukeH - Tru dat. Old habits die hard I guess :) – Joel Etherton Feb 15 '11 at 16:18
  • @JoelEtherton, for efficiency, which the op wants, the for loop is 2x faster than using a foreach iterator. String's indexer[] impl uses native code to return the array item. – noctonura Jul 30 '17 at 22:45
21

Here's an alternative since you don't like the cast to int:

foreach(byte b in System.Text.Encoding.UTF8.GetBytes(str.ToCharArray()))
    Console.Write(b.ToString());
Joel Etherton
  • 37,325
  • 10
  • 89
  • 104
  • Well this should be correct if you use ASCII instead of UTF8.. or else you wont get the ascii values you will get the UTF8 values... – Peter Feb 15 '11 at 13:09
  • 2
    @Petoj - This is pulled straight from the MSDN docs on converting to ascii. The "correct" way would be to call `ConvertToUTF32()` since that is the true encoding of the characters themselves. – Joel Etherton Feb 15 '11 at 13:43
  • 1
    It might be pulled form MSDN but i don't agree with you that its correct, first of ASCII only contains 7bit chars and UTF8 contains a lot more and secondly some UTF8 chars are saved as 2 bytes (or more) so you are not able to determine what char is what byte any more... – Peter Feb 15 '11 at 14:16
  • @Petoj - Again, please direct complaints about the inadequacies of UTF8 vs ASCII to Microsoft. – Joel Etherton Feb 15 '11 at 14:17
  • 1
    @Joel Etherton - so you just copy and paste a answer and we are not supposed to point out when it does not work as supposed? – Peter Feb 15 '11 at 14:24
  • 1
    @Petoj - I offered an alternative to the answers the OP did not like. I agree with the previous answers and recommend that the one offered by @Jorge Córdoba. However, OP indicated he did not like that method, so I offered him another. And FYI, I did not copy/paste. As for pointing things out, you did that with your first comment, and you don't have to agree with me. You are welcome to your opinion. I look forward to reading your alternative answer. – Joel Etherton Feb 15 '11 at 14:34
  • 3
    @Petoj: Unicode codes from 0 through 127 are **identical** to the seven-bit ASCII codes. If the original poster wants "ASCII" data for strings that contain characters that have no ASCII encoding then the OP is going to have to clarify the question, because as it stands it is unclear. – Eric Lippert Feb 15 '11 at 15:08
  • 7
    @Joel: Please direct complaints about UTF8 to the Unicode Consortium. Microsoft is just one member of the consortium; you might as well say "direct your complaints to Apple, Oracle, Google, IBM, Microsoft, SAP, Yahoo! and the federal government of India". Microsoft is just one of many important players in the Unicode standardization process. – Eric Lippert Feb 15 '11 at 15:15
  • @Eric Lippert - I don't have any complaints about it. I just wanted his complaints to go somewhere else. He can ask Martha Steward about it if it makes him feel better. I'm no Unicode expert, so complaining to me about it isn't going to get him very far :) – Joel Etherton Feb 15 '11 at 15:17
  • you could use `Encoding.ASCII` if that is what the OP wants. After all that has totally different semantics for any character > 7bit – Sebastian Dec 08 '13 at 22:14
4

This example might help you. by using simple casting you can get code behind urdu character.

string str = "عثمان";
        char ch = ' ';
        int number = 0;
        for (int i = 0; i < str.Length; i++)
        {
            ch = str[i];
            number = (int)ch;
            Console.WriteLine(number);
        }
Ammar Shaukat
  • 345
  • 6
  • 17
  • Because a `string` is a counted sequence of UTF-16 code units (one or two of which encode a Unicode codepoint), this will get the UTF-16 code units. Codepoints are more human readable. To get those, convert to UTF-32 because UTF-32 code units and Unicode codepoints are one-to-one and have the same values. – Tom Blodget Feb 26 '16 at 08:36
0

Here is another alternative. It will of course give you a bad result if the input char is not ascii. I've not perf tested it but I think it would be pretty fast:

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static int GetAsciiVal(string s, int index) {
    return GetAsciiVal(s[index]);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static int GetAsciiVal(char c) {
    return unchecked(c & 0xFF);
}
noctonura
  • 12,763
  • 10
  • 52
  • 85