I would like to convert some huge hex string to int string.
my hex string can be somthing like that:
666E7A427931676468633533394553764B38483240384A4B615241333455455A3369386F366048745A333932367A6A664142462F57574273
this number is too long to be store in an int, and I need to get it in a string as integer
and how to convert my new int string back in hex ?
have you some idea to do that?
You can use BigInteger (.NET 4.0+) to obtain the base 10 representation.
var hexString = "666E7A427931676468633533394553764B38483240384A4B615241333455455A3369386F366048745A333932367A6A664142462F57574273"
var bigNumber = BigInteger.Parse(hexString, NumberStyles.AllowHexSpecifier)
The result is
290825075527865440850840162776336047300722068695844686969687688283360481878315042200208855384521898951434440464937388090234036230242931
You can store it as a string from there if it's more convenient for you.
Related
Working on a project where I need to do a bitwise & between two similar 168 character bitarrays. I am working in C# and when I attempt to convert from String to BigInteger, the leading zero's are being truncated. Is there anything I can do to save these characters?
Basically:
string bits = "000000000000000000000000000000000000000000000000000000000000000000001111111000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
BigInteger bigIntBits = BigInteger.Parse(bits);
// I am being returned 1111111000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
Anything will help. Thank you in advance.
You can simply do this:
string bits = "000000000000000000000000000000000000000000000000000000000000000000001111111000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000";
// convert to big integer
var bigIntBits = BigInteger.Parse(bits);
int index = bits.IndexOf('1');
string backToString = new string('0', index) + bigIntBits.ToString();
I was wondering if there were any good methods to change a string into an integer representation and then be able to change the integer representation back to a string? There are a lot of ways to change a string to an integer representation by changing each character to ASCII, etc. but I was confused by how to change the ascii integer back into the fully formed string.
I am trying to represent my object of 3 strings into an integer so that I do not have to group by 3 string but just 1 integer.
For example an object of :
("hey",
"hello",
"goodbye")
would have an int representation of lets say 123423425 with some algorithm.
Then if possible I would like to be able convert that int from another algorithm back into
("hey",
"hello",
"goodbye")
Any help would be appreciated
int number = Int32.Parse("45");
string formatted = number.ToString()//add formatting if needed
You can go back and forth
Reference for format strings: http://msdn.microsoft.com/en-us/library/dwhawy9k(v=vs.110).aspx
Convert char array to string
char[] charArray = {'1', '2', '3'};
String str = String.valueOf(charArray);
I need to write a hexadecimal value into a string.
I'm using this:
int value;
long item = Convert.ToInt64(value); // this convert value to a hexadecimal
So I need to write this hexadecimal value into a string...
so, when I use ToString(), it's convert again into integer, I need to write hexadecimal value.
Not sure if I get your question, but you may want to try this:
int value;
string s = value.ToString("X"); // int to hexadecimal string
If you want to reparse it back to a long:
ulong ul = ulong.Parse(s, System.Globalization.NumberStyles.HexNumber); // hexadecimal string to unsigned int
int myInt = 1243;
string myHex = myInt.ToString("X"); // gives you hex
int myNewInt = Convert.ToInt32(myHex, 16); // back to int again.
Is it possible to convert alphabetical string into int in C#? For example
string str = "xyz";
int i = Convert.ToInt32(str);
I know it throws an error on the second line, but this is what I want to do.
So how can I convert an alphabetical string to integer?
Thanks in advance
System.Text.Encoding ascii = System.Text.Encoding.ASCII;
string str = "xyz";
Byte[] encodedBytes = ascii.GetBytes(str);
foreach (Byte b in encodedBytes)
{
return b;
}
this will return each characters ascii value... its up to you what you want to do with them
To answer the literal questions that you have asked
Is it possible to convert alphabetical string into int in C#?
Simply put... no
So how can I convert an alphabetical string to integer?
You cannot. You can simply TryParse to see if it will parse, but unless you calculate as ASCII value from the characters, there is no built in method in c# (or .NET for that matter) that will do this.
You can check whether a string contains a valid number using Int32.TryParse (if your questions is about avoiding an exception to be thrown):
int parsed;
if (!Int32.TryParse(str, out parsed))
//Do Something
I need to convert an int to hex string.
When converting 1400 => 578 using ToString("X") or ToString("X2") but I need it like 0578.
Can anyone provide me the IFormatter to ensure that the string is 4 chars long?
Use ToString("X4").
The 4 means that the string will be 4 digits long.
Reference: The Hexadecimal ("X") Format Specifier on MSDN.
Try the following:
ToString("X4")
See The X format specifier on MSDN.
Try C# string interpolation introduced in C# 6:
var id = 100;
var hexid = $"0x{id:X}";
hexid value:
"0x64"
Previous answer is not good for negative numbers. Use a short type instead of int
short iValue = -1400;
string sResult = iValue.ToString("X2");
Console.WriteLine("Value={0} Result={1}", iValue, sResult);
Now result is FA88
Convert int to hex string
int num = 1366;
string hexNum = num.ToString("X");