1

i need to convert some char to int value but bigger than 256. This is my function to convert int to char. I need reverse it

public static string chr(int number)
{
    return ((char)number).ToString();
}

This function doesnt work - its returning only 0-256, ord(chr(i))==i

public static int ord(string str)
{
    return Encoding.Unicode.GetBytes(str)[0];
}
Magnus Hoff
  • 21,529
  • 9
  • 63
  • 82
  • Use a different encoding, maybe? – Tim Apr 13 '16 at 20:03
  • What coding? I tried utf-8 and ascii and its not working, i am making encrypting function to make bot and i was converting it from php, i practically need php ord function. –  Apr 13 '16 at 20:06
  • `Encoding.Unicode.GetBytes` returns a byte array. A byte is unsigned: https://msdn.microsoft.com/en-us/library/5bdb6693.aspx – 001 Apr 13 '16 at 20:06
  • Chr returns a single character concealed as a string. But in turn, GetBytes() returns an array of bytes. A byte in C# is from 0 upto 255. A byte is not a character. A character can span over many bytes in a string. Why didn't you just take a `string str` and take a first character from it? – quetzalcoatl Apr 13 '16 at 20:06
  • Probably you should be using `int.Parse(str)` instead of `Encoding.Unicode.GetBytes(str)[0]` – kfazi Apr 13 '16 at 20:07
  • 1
    Maybe I am seeing it wrong, but if you only return the first position of the array of bytes, the value will always be between 0 and 255... – Manuel Reis Apr 13 '16 at 20:07
  • I need string str as int value. so for example "2" is chr(50) and i need make from "2" 50 –  Apr 13 '16 at 20:08
  • [This](http://stackoverflow.com/questions/6141255/parse-a-non-ascii-unicode-number-string-as-integer-in-net) looks like an answer. – BobRodes Apr 13 '16 at 20:09
  • 2
    `int i = (int)'ğ';` would give 287. `char c = (char)287;` is the reverse op. – Eser Apr 13 '16 at 20:11

4 Answers4

2

The problem is that your ord function truncates the character of the string to the first byte, as interpreted by UNICODE encoding. This expression

Encoding.Unicode.GetBytes(str)[0]
//                            ^^^

returns the initial element of a byte array, so it is bound to stay within the 0..255 range.

You can fix your ord method as follows:

public static int Ord(string str) {
    var bytes = Encoding.Unicode.GetBytes(str);
    return BitConverter.ToChar(bytes, 0);
}

Demo

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • 1
    Why did you use an encoding? Wouldn't a simple integer cast be enough? – usr Apr 13 '16 at 20:20
  • @usr No, not without loss of generality. It appears that OP would like to make something encoding-dependent - perhaps he plans to pass different encodings to his `Ord` in a separate parameter. I cannot guess his intentions, so I kept as much of his code as I could. – Sergey Kalinichenko Apr 13 '16 at 20:22
  • @usr Perhaps you're right, but I think it's better to give new OPs a benefit of the doubt. – Sergey Kalinichenko Apr 13 '16 at 20:25
1

Since you don't care much about encodings and you directly cast an int to a char in your chr() function, then why dont you simply try the other way around?

    Console.WriteLine((int)'\x1033');
    Console.WriteLine((char)(int)("\x1033"[0]) == '\x1033');
    Console.WriteLine(((char)0x1033) == '\x1033');
quetzalcoatl
  • 32,194
  • 8
  • 68
  • 107
0

char is 2 bytes long (UTF-16 encoding) in C#

char c1; // TODO initialize me
int i = System.Convert.ToInt32(c1); // could be greater than 255
char c2 = System.Convert.ToChar(i); // c2 == c1

System.Convert on MSDN : https://msdn.microsoft.com/en-us/library/system.convert(v=vs.110).aspx

RCYR
  • 1,452
  • 13
  • 28
0

Characters and bytes are not the same thing in C#. The conversion between char and int is a simple one: (char)intValue or (int)myString[x].

hoodaticus
  • 3,772
  • 1
  • 18
  • 28