3

Is there a way to format an integer as the equivalent ASCII/Unicode character in C#? For example, if I have the following code:

int n = 65;
string format = ""; // what?

string s = string.Format(format, n);

what do I need to put in the string format to result a single character, 'A', being written to s - basically I'm looking for the equivalent of doing the following in C:

int n = 65;
char s[2];
char format = "%c";

sprintf(s, format, n); /* s <- 'A' */

EDIT

I should probably explain a bit more about what I'm trying to do, as the obvious answer "cast it to char", doesn't help.

I have a situation where I have an integer value that represents a bank account check digit, but which needs to be output as a character for some countries and a (0-padded) digit string for others. I'm wondering if there's a way of switching between the two just by changing the format string, so I can hold a dictionary of the appropriate format strings, keyed on the country code.

EDIT 2

(For Oded) something like this ...

IDictionary<string, string> ccFormat = new Dictionary<string, string>()
{
  { "GB", "{0:D}" },  // 0-9
  { "PT", "{0:D2}" }, // 00-99
  { "US", "{0:D3}" }, // 000-999
  { "IT", ???? }      // A-Z -- What should ???? be?
};


string FormatCheckDigits(int digits, string country)
{
  return string.Format(ccFormat[country], digits);
}

Currently I've got ???? as null and some special case code in the method:

string FormatCheckDigits(int digits, string country)
{
  string format = ccFormat[country];

  if (string.IsNullOrEmpty(format)) 
  {
    // special case: format as A-Z
    return ((char) digits).ToString();
  }
  else 
  {
    // Use retrieved format string
    return string.Format(format, digits);
  }
}
Rob Gilliam
  • 2,680
  • 4
  • 26
  • 29
  • Isn't it a duplicate question? http://stackoverflow.com/questions/4648781/how-to-get-character-for-a-given-ascii-value – user1859587 Nov 29 '12 at 14:33
  • Why not just have a `Dictionary` for the conversion? One per country? – Oded Nov 29 '12 at 15:59
  • @Oded I'm not quite sure what you're envisaging, but the check digit isn't per-country; the _format_ is per-country, but the check digit differs based on the bank account, hence the need to format and print it. – Rob Gilliam Nov 30 '12 at 06:22
  • Can you give a few examples? It is not clear to me at all. – Oded Nov 30 '12 at 09:31

4 Answers4

6

You can cast the int to a char:

string s = ((char)n).ToString();

There is no way to tell string.Format to format an integer as a character. There are not custom or standard numeric format strings that will help with this.

Your current solution is simple and works, so you should keep it, unless you are starting to get into more involved options. If that happens, you may need to introduce an abstraction that will return the correct representation for country (say a CountryCheckDigitFormatter that will hold the country and a custom ToString method per country).

Oded
  • 489,969
  • 99
  • 883
  • 1,009
2

Is this acceptable?

string a = Char.ConvertFromUtf32(65);

Console.WriteLine(a); //prints 'A'
Esailija
  • 138,174
  • 23
  • 272
  • 326
  • Not sure this is entirely correct. Is ConvertFromUtf32 equivalent to (char)x? I don't think so. – usr Nov 29 '12 at 14:35
  • @usr Well it's a horribly named method, it should be something like `String.fromCodePoint`. It gives a string with character from an unicode integer code point, that seemed like what the OP wanted. Also, `(char)x` cannot handle beyond BMP, because they require 2 chars to encode in UTF16. – Esailija Nov 29 '12 at 14:44
1

Well to get a character, you can just cast:

char c = (char) n;

If you then need to get that as a string, you can just call ToString:

string x = c.ToString();
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
1

easy:

int i = 65;
char c = Convert.ToChar(i);
Kamber
  • 89
  • 3
  • See my edit - casting to char isn't the solution for my particular problem. – Rob Gilliam Nov 29 '12 at 14:57
  • Down-vote wasn't me - that would, indeed, have been quite unfair!(Also my comment isn't 100% applicable to your answer - I added it to two others, but it's only stuck to yours; anti-spamming, I guess) – Rob Gilliam Nov 29 '12 at 15:09