How to get character for a given ascii value

后端 未结 9 2679
醉酒成梦
醉酒成梦 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;
    }
    

提交回复
热议问题