6

I have a c++ application. In that application one of the function is returning unsigned char value. I want to convert that function into C# code. But, I don't have enough knowledge about c++ programming. I want to know what datatype would be placed against c++ unsigned char in C#.

The c++ function is look like this

unsigned char getValue(string s)
{
    //SOME CODE HERE
}
Shell
  • 6,818
  • 11
  • 39
  • 70

4 Answers4

12

The equivalent of unsigned char in C# is byte.

byte getValue(string s)
{

}

Second option: if you use unsigned char as a character storage, you should use char:

char getValue(string s)
{

}

You have to remember, that C++ treats characters and 8-byte values equally. So, for instance in C++:

'A' + 'B' == 65 + 66 == 65 + 'B' == 'A' + 66

You have to check from the context, whether the unsigned char is a character or a number. From string being passed as a parameter, one can guess, that the function processes characters so mostly probably you want a char C# type.

Spook
  • 25,318
  • 18
  • 90
  • 167
3

You are looking for the byte type. Also, this question has additional mappings if you need those.

If your C# Code is supposed to treat the value as a character, then the char type is what you want. The reason why we have been suggesting byte is because we are assuming you want to treat the value as an 8-bit integer.

Community
  • 1
  • 1
merlin2011
  • 71,677
  • 44
  • 195
  • 329
2
C++ unsigned char = C# byte
C++ char = C# sbyte 
Arsalan Qaiser
  • 426
  • 2
  • 7
  • 26
1

Use a char. A char in C# is neither signed or unsigned. It is 16 bit value.

If you do not want to retain the character, but the actual binary number, then you should choose the byte, that is a unsigned integer number, just like the unsigned char in c++.

See also: What is an unsigned char?

Community
  • 1
  • 1
Martin Mulder
  • 12,642
  • 3
  • 25
  • 54
  • so, is my function returning ascii byte or any alphabetic character? – Shell Apr 11 '14 at 06:45
  • @Nimesh, Your function is returning an 8-bit value, which your program can choose to interpret however it chooses to. – merlin2011 Apr 11 '14 at 06:49
  • Both. So to make a choise, we need to find out the actual needed datatype, so we need the body of the method. If the method returns, for example, the first character from the given string, then the return type is `char`. if it will return the number of elements in the string, `byte` should be better (besides `int`). – Martin Mulder Apr 11 '14 at 06:51