How to get character for a given ascii value

后端 未结 9 2554
醉酒成梦
醉酒成梦 2020-11-29 05:51

How can I get the ascii character of a given ascii code.

e.g. I\'m looking for a method that given the code 65 would return \"A\".

Thanks

相关标签:
9条回答
  • 2020-11-29 06:16

    Sorry I dont know Java, but I was faced with the same problem tonight, so I wrote this (it's in c#)

    public string IncrementString(string inboundString)    {
    byte[] bytes = System.Text.Encoding.ASCII.GetBytes(inboundString.ToArray);
    bool incrementNext = false;
    
    for (l = -(bytes.Count - 1); l <= 0; l++) {
        incrementNext = false;
    
        int bIndex = Math.Abs(l);
        int asciiVal = Conversion.Val(bytes(bIndex).ToString);
    
        asciiVal += 1;
    
        if (asciiVal > 57 & asciiVal < 65)
            asciiVal = 65;
        if (asciiVal > 90) {
            asciiVal = 48;
            incrementNext = true;
        }
    
        bytes(bIndex) = System.Text.Encoding.ASCII.GetBytes({ Strings.Chr(asciiVal) })(0);
    
        if (incrementNext == false)
            break; // TODO: might not be correct. Was : Exit For
    }
    
    inboundString = System.Text.Encoding.ASCII.GetString(bytes);
    
    return inboundString;
    }
    
    0 讨论(0)
  • 2020-11-29 06:22

    Simply Try this:

    int n = Convert.ToInt32(Console.ReadLine());
    Console.WriteLine("data is: {0}", Convert.ToChar(n));
    
    0 讨论(0)
  • 2020-11-29 06:23

    Do you mean "A" (a string) or 'A' (a char)?

    int unicode = 65;
    char character = (char) unicode;
    string text = character.ToString();
    

    Note that I've referred to Unicode rather than ASCII as that's C#'s native character encoding; essentially each char is a UTF-16 code point.

    0 讨论(0)
提交回复
热议问题