How to convert binary back to normal string? - c#

So I have this:
char x = 'a';
int number = (int)x;
textBox.Text = number.ToString(); // actual "97", wanted "a"
It will display "97" if I print it.
How can I convert this back to char 'a'?
I tried a lot of things but it always displays 97.

Convert first to char (you'll get 'a' from ascii code 97) and only then to string ("a"):
textBox.Text = ((char)number).ToString();
Same idea but with implicit ToString() which will be called by string interpolation:
textBox.Text = $"{(char)number}";

You can use System.Convert:
char x = 'a';
int number = (int)x;
char chrNumber = System.Convert.ToChar(number);
textBox.text = chrNumber.ToString();

Related

Convert emoji to hex number

I'm trying to convert an emoji to an hex number or a string.
there is any way to convert this 👱 in this : 0x00000000D83DDC71L or D83DDC71
Edit
my code is this:
var bytes = Encoding.UTF8.GetBytes(emoji.ToString()); //emoji is 👱
var number = BitConverter.ToUInt32(bytes, 0); //number is 2610470896
var emojiCode = unicode.ToString("X"); // emojiCode is 9B989FF0
the problem is that i need my emojiCode to be D83DDC71
i hope is more clear now.
You have to do something like:
var str = "\uD83D\uDC71";
string res = BitConverter.ToString(Encoding.BigEndianUnicode.GetBytes(str)).Replace("-", "");
Note that you want your Unicode string to be in "big endian" mode (so Encoding.BigEndianUnicode)
Probably easier without going through the Encoding conversion:
string res = string.Concat(str.Select(x => ((ushort)x).ToString("X4")));
(ushort and char are nearly the same thing, but ushort is built to be formatted as a number, while char is built to be formatted as a character)
Emoji Unicode is not a single hex number, and it only encoding by UTF32.
So you could split it, like this:
byte[] utfBytes = System.Text.Encoding.UTF32.GetBytes("👱");
print(utfBytes.Length);
for (int i = 0; i < utfBytes.Length; i += 4)
{
if (i != 0) result += '-';
result += System.BitConverter.ToInt32(utfBytes, i).ToString("x2").ToUpper();
}

Is there built-in method to add character multiple times to a string?

Is there a built-in function or more efficient way to add character to a string X number of times?
for example the following code will add '0' character 5 times to the string:
int count = 5;
char someChar = '0';
string myString = "SomeString";
for(int i=0;i<count;i++)
{
myString = someChar + myString;
}
Use PadLeft() or PadRight()
An example for PadRight():
int count = 5;
char someChar = '0';
string myString = "SomeString";
myString = myString.PadRight(count + myString.Length, someChar);
// output -> "SomeString00000"
Remember the first parameter of either method is the total string length required hence why I am adding count to the original string length.
Likewise if you want to append the character at the start of the string use PadLeft()
myString = myString.PadLeft(count + myString.Length, someChar);
// output -> "00000SomeString"
string.Concat(Enumerable.Repeat("0", 5));
will return
"00000"
Refered from :Is there a built-in function to repeat string or char in .net?
You can also do it as:
string line = "abc";
line = "abc" + new String('X', 5);
//line == abcXXXXX
Take a look here, You can use PadRight() / PadLeft();
int count = 5;
char someChar = '0';
string myString = "SomeString";
var stringLength = myString.Length;
var newPaddedStringRight = myString.PadRight(stringLength + count, '0');
//will give SomeString00000
var newPaddedStringLeft = myString.PadLeft(stringLength + count, '0');
//will give 00000SomeString
Remember, a string is Immutable, so you'll need to assign the result to a new string.
You could also use StringBuilder. As the string size increases the += incurs a cost on array copy.

How can I convert any string to an integer?

How can i convert from example "1.234.567.890 VNĐ" or any string not in a correct number format.
output: 1234567890
I try: int.Parse, convert.ToInt32 or int.tryParse, double,.... But not working.
If all you want is the integer numbers that are contained within a string, you can just loop through the string and take all numbers.
string yourString = "1.234.567.890 VNĐ";
string tmpString = String.Empty;
for (int i = 0; i < yourString.Length; i++)
{
if (char.IsDigit(yourString, i))
{
tmpString += yourString[i];
}
}
int finalInt = int.Parse(tmpString);
char.IsDigit(string, int) (see documentation) checks if the char at position i in the string is a digit (not only 0..9, but also other numbers). If that's the case, add it to your string. At the end, you have all your numbers and can cast them to int.

Convert letter from textBox to an integer

How do I convert alphabet letter from textBox to an integer?
For example, if the user inputs "B" in the textBox, I am trying to convert B to the number 1
string a = textBox1.Text
int number = 1;
number = int.Parsed (a);
messageBox.Show(number.ToString());
Your first task is to capture only the first character from the input:
string str = textBox1.Text;
char chr = str[0]; // get first character
Now the character can be directly converted to an integer, for example:
int number = (int)chr;
However, the character 'A' is actually represented by the number 65, 'B' 66, and so on (see ASCII), so you'll have to subtract that from your input to get the intended value:
int value = number - 65;
Or if you prefer:
int value = number - (int)'A';
Now you might want to normalize input so a character 'a' will be treated as 'A', so in the end it would look a bit like this:
string str = textBox1.Text.ToUpperCase();
char chr = str[0];
int number = (int)chr;
int value = number - 65;
MessageBox.Show(value.ToString());

Convert a word into character array

How do I convert a word into a character array?
Lets say i have the word "Pneumonoultramicroscopicsilicovolcanoconiosis" yes this is a word ! I would like to take this word and assign a numerical value to it.
a = 1
b = 2
... z = 26
int alpha = 1;
int Bravo = 2;
basic code
if (testvalue == "a")
{
Debug.WriteLine("TRUE A was found in the string"); // true
FinalNumber = Alpha + FinalNumber;
Debug.WriteLine(FinalNumber);
}
if (testvalue == "b")
{
Debug.WriteLine("TRUE B was found in the string"); // true
FinalNumber = Bravo + FinalNumber;
Debug.WriteLine(FinalNumber);
}
My question is how do i get the the word "Pneumonoultramicroscopicsilicovolcanoconiosis" into a char string so that I can loop the letters one by one ?
thanks in advance
what about
char[] myArray = myString.ToCharArray();
But you don't actually need to do this if you want to iterate the string. You can simply do
for( int i = 0; i < myString.Length; i++ ){
if( myString[i] ... ){
//do what you want here
}
}
This works since the string class implements it's own indexer.
string word = "Pneumonoultramicroscopicsilicovolcanoconiosis";
char[] characters = word.ToCharArray();
Voilá!
you can use simple for loop.
string word = "Pneumonoultramicroscopicsilicovolcanoconiosis";
int wordCount = word.Length;
for(int wordIndex=0;wordIndex<wordCount; wordIndex++)
{
char c = word[wordIndex];
// your code
}
You can use the Linq Aggregate function to do this:
"wordsto".ToLower().Aggregate(0, (running, c) => running + c - 97);
(This particular example assumes you want to treat upper- and lower-case identically.)
The subtraction of 97 translates the ASCII value of the letters such that 'a' is zero. (Obviously subtract 96 if you want 'a' to be 1.)
you can use ToCharArray() method of string class
string strWord = "Pneumonoultramicroscopicsilicovolcanoconiosis";
char[] characters = strWord.ToCharArray();

Categories

Resources