Converting string with letters and numbers to decimal only numbers - c#

I'm working with RFID Reader, and it became with a software demo that has some different types of reading a rfid tag, like:
Hexadecimal,Decimal,Ascii, Abatrack etc...
The Abatrack documentation says:
Shows the CardID converted to decimal with 14 digits.
I have a CardID = 01048CABFB then with this protocol it shows me 00004371295227
where the first four zeroes were added by the software
It converts a string with letters and numbers to decimal with only numbers. how may I do that ?
I've found THIS , but it's in VB.

To convert from hexadecimal to decimal, you can do this:
string hexString = "01048CABFB";
long intVal = Int64.Parse(hexString, System.Globalization.NumberStyles.HexNumber);
// intVal = 4371295227

You can also use Convert.ToInt64() which allows you to specify base 16 (hexadecimal):
string hexFromRFID = "01048CABFB";
Int64 decFromRFID = Convert.ToInt64(hexFromRFID, 16);
Console.WriteLine("Hex: " + hexFromRFID + " = Dec: " + decFromRFID);

Related

Convert string to unicode in C#?

How can I get the unicode values (from the code column) if I have the string?
For example, for passing the empty space " " I would like to get the value U+0020.
I found this approach:
byte[] asciiBytes1 = Encoding.ASCII.GetBytes(" ");
But this returns me the value from the decimal column.
If value is your decimal value:
string code = $"U+{value.ToString ("X4")}";
will give you what you want.
(X means hex, 4 means pad to 4 digits)

Is there a different way to convert to hexadecimal?

I'm trying to write some information to a special device that requires me to encode the string and I quote " an even number of bytes to write (1-32, base 10) "
The example string provided "DE AD BE EF CA FE" (works).
I have converted my string to decimal and from decimal to hexadecimal.
string TextToConvert = "Test Andrei";
TextToConvert=ConvertStringToHex(TextToConvert, Encoding.UTF8);
List<char> Chars = TextToConvert.ToCharArray().ToList();
string CharValue = "";
string secondHexConvert = "";
foreach(char c in Chars)
{
CharValue+=Convert.ToInt32(c);
secondHexConvert+=Convert.ToString(c, 16)+" ";
}
string hexValue = String.Format("{0:X}", CharValue)+" ";
I have found on internet a tool that converts to hexadecimal that works. The problem is that I can't figure what type of encoding is that. The site is this: https://codebeautify.org/decimal-hex-converter
from decimal "841011151163265110100114101105" to hex = "a9d741e82c990000000000000"
To convert such a big integer to a hexadecimal string, use the aptly named BigInteger type:
var num = BigInteger.Parse("841011151163265110100114101105");
string hex = num.ToString("X");
Console.WriteLine(hex);
will output:
0A9D741E82C98FC6A137B75371
but here's a snag, the output you showed in your question is somewhat different, let me show it together with what the code above produces:
0A9D741E82C98FC6A137B75371
a9d741e82c990000000000000
As you can see, the numbers start the same but your example then ends up with lots of zeroes.
The only way I understand this could happen is that they're in fact not using a type that can hold that many significant digits, so you get a rounding error.
Many of the dynamic programming languages allows you to use floating point numbers and integers interchangeably, I guess this is what happened, a floating point type that can only hold 17-18 significant digits or some such was used, and you lost precision. .NET, however, doesn't have built-in support for converting floating point types to hexadecimal.
You can see that .NET produces the exact value by converting back:
Console.WriteLine(BigInteger.Parse(hex, System.Globalization.NumberStyles.HexNumber));
outputs:
841011151163265110100114101105
In other words, I'm not sure you can get the exact same results in .NET.
Corollary: Don't use that site for this kind of conversion!
You can use the following code to convert a string to hexadecimal:
public static string ConvertStringToHex(String input, System.Text.Encoding encoding)
{
Byte[] stringBytes = encoding.GetBytes(input);
StringBuilder sbBytes = new StringBuilder(stringBytes.Length * 2);
foreach (byte b in stringBytes)
{
sbBytes.AppendFormat("{0:X2}", b);
}
return sbBytes.ToString();
}
And you just call it using:
string testString = "11111111";
string hex = ConvertStringToHex(testString, System.Text.Encoding.Unicode);

c# convert double to string keeping trailing 0s [duplicate]

I've tried converting the double value into a string and using the Replace() method
to replace the ',' to '.'.
This works well but only when the trailing digits are not zero, I need zeros in my string, even if the value is 1234.0. This worked well for the decimal values. I have tried to convert the double to decimal but I lose the decimal digits if there are zeros.
I know I'm missing something. I would be grateful for some suggestions.
This would depend on the language. An example in C#
d.ToString("0.00");
Would produce a double with 2 decimal places nomatter the values (zero or otherwise).
If this is in Java, check out the NumberFormat class's setMinimumFractionDigits() method.
Example:
double d1 = 2.5;
double d2 = 5.0;
NumberFormat nf = NumberFormat.getInstance();
nf.setMinimumFractionDigits(2);
String d1s = nf.format(d1);
String d2s = nf.format(d2);
System.out.println("d1s: " + d1s + " and d2s: " + d2s);
produces
d1s: 2.50 and d2s: 5.00
...and in Fortran, you could do something like: :-)
write(*,110) x
110 format (F5.3)
(guess we really have to know what language is being used...)

What does Int32.Parse do exactly?

I am just beginning to learn C#. I am reading a book and one of the examples is this:
using System;
public class Example
{
public static void Main()
{
string myInput;
int myInt;
Console.Write("Please enter a number: ");
myInput = Console.ReadLine();
myInt = Int32.Parse(myInput);
Console.WriteLine(myInt);
Console.ReadLine();
}
}
When i run that and enter say 'five' and hit return, i get 'input string not in correct format' error. The thing i don't understand is, i converted the string myInput to a number didn't i? Microsoft says that In32.Parse 'Converts the string representation of a number to its 32-bit signed integer equivalent.' So how come it doesn't work when i type the word five? It should be converted to an integer shouldn't it... confused. Thanks for advice.
'five' is not a number. It's a 4-character string with no digits in it. What parse32 is looking for is a STRING that contains numeric digit characters. You have to feed it "5" instead.
The string representation that Int32.Parse expects is a sequence of decimal digits (base 10), such as "2011". It doesn't accept natural language.
What is does is essentially this:
return 1000 * ('2' - '0')
+ 100 * ('0' - '0')
+ 10 * ('1' - '0')
+ 1 * ('1' - '0');
You can customize Int32.Parse slightly by passing different NumberStyles. For example, NumberStyles.AllowLeadingWhite allows leading white-space in the input string: " 2011".
The words representing a number aren't converted; it converts the characters that represent numbers into actual numbers.
"5" in a string is stored in memory as the ASCII (or unicode) character representation of a 5. The ASCII for a 5 is 0x35 (hex) or 53 (decimal). An integer with the value '5' is stored in memory as an actual 5, i.e. 0101 binary.

Double to String keeping trailing zero

I've tried converting the double value into a string and using the Replace() method
to replace the ',' to '.'.
This works well but only when the trailing digits are not zero, I need zeros in my string, even if the value is 1234.0. This worked well for the decimal values. I have tried to convert the double to decimal but I lose the decimal digits if there are zeros.
I know I'm missing something. I would be grateful for some suggestions.
This would depend on the language. An example in C#
d.ToString("0.00");
Would produce a double with 2 decimal places nomatter the values (zero or otherwise).
If this is in Java, check out the NumberFormat class's setMinimumFractionDigits() method.
Example:
double d1 = 2.5;
double d2 = 5.0;
NumberFormat nf = NumberFormat.getInstance();
nf.setMinimumFractionDigits(2);
String d1s = nf.format(d1);
String d2s = nf.format(d2);
System.out.println("d1s: " + d1s + " and d2s: " + d2s);
produces
d1s: 2.50 and d2s: 5.00
...and in Fortran, you could do something like: :-)
write(*,110) x
110 format (F5.3)
(guess we really have to know what language is being used...)

Categories