Padding so always 12 digits long - c#

I have an string which needs to be always 12 digits long
Its need starts with 'PSS1'
but need there to be always 12 characters and pad the difference with zero's
so if input is string1 = '300'
I would need the result = 'PSS100000300'
when the input length increase the number of zero padding decreases so the total of characters remains 12.
I've tried using .padleft or .ToString("D12")

This should work
string result = "PSS1".PadRight(12 - string1.Length,'0') + string1;

I realize that your original question said that you had a string, but if it were an int, you could do this with the ToString method on the int object.
int input = 300;
input.ToString("PSS100000000");
Which returns PSS100000300
Perhaps doesn't answer this specific question, but may potentially be useful for others.

Related

3 digit hexadecimal generator for Access Database

I am trying to figure out how to create a hexadecimal generator that always spits out 3 digits in C# and sends it to my access database. Here is the code that I found an example of on here and changed a little bit, but the generator sometimes only gives me 2 digits. What am I doing wrong?
Thank you,
var r = new Random();
int A = r.Next(100, 500);
string hexValue1 = A.ToString("X");
MessageBox.Show(hexValue1);
As described in
The Hexadecimal ("X") Format Specifier
The precision specifier indicates the minimum number of digits desired in the resulting string. If required, the number is padded with zeros to its left to produce the number of digits given by the precision specifier.
So you can simply use
string hexValue1 = A.ToString("X3");
to always give you three digits (provided that A <= 0xFFF)..
The minimum possible value for a 3 digit hex value would be 0x100 (256 decimal) and the maximum value would be 0xFFF (4095 decimal). So in order to generate this string you need to use something like:
var r = new Random();
int A = r.Next(256, 4096); //Using the exclusive maximum (required max + 1)
string hexValue1 = A.ToString("X");
MessageBox.Show(hexValue1);

Converting string to sortable number

Is there any way of converting a string value (any content) to a number such that they both sort in the same way? I don't need to be able to convert the number back to a string. In fact it would be an advantage if it were impossible to convert it back.
I don't need to be able to sort any length of string. If a 64-bit long integer is used as the sort-value then I could trim the texts to a value that fits this number range.
I don't think there can be 100% correct way since sorting a string depends on the culture. For ex
int c1 = String.Compare("AA", "BB", false, CultureInfo.GetCultureInfo("en-US")); //return -1
int c2 = String.Compare("AA", "BB", false, CultureInfo.GetCultureInfo("da-DK")); //return 1
The closest thing I can think of is:
ulong l = BitConverter.ToUInt64(Encoding.UTF8.GetBytes(str), 0);
PS: pad str if its len is shorter than 8
You could take the first 8 bytes from the string, the 8 bytes would make up a ulong. It would only be 4 characters of the string with unicode, or 8 characters if you limit the strings to ASCII.

System.Double value with max characters

I'm testing net xml serialization of double[] arrays so I'm interested to know whats the double value that has most characters int it's serialized for so I can test whats the max output size of serialized array.
It should be 24.
double.MinValue.ToString("R").Length
From double.ToString(string)
or "R", which returns 15 digits if the number can be represented with that precision or 17 digits if the number can only be represented with maximum precision.
you have that there are at max 17 digits, plus 1 for sign, plus 1 for the decimal separator, plus 5 for the E+xxx (double.MaxValue is 1.7976931348623157E+308 and double.Epsilon, the smallest value > 0, is 4.94065645841247E-324, so both in the form E[+-][0-9]{1,3}).
Note that technically, in some strange languages,
var str2 = double.PositiveInfinity.ToString("R");
could be longer (because the string is localized), but I hope you'll serialize your numbers with CultureInfo.InvariantCulture!
But remember that users could have changed their culture from the control panel... something like:
var culture = (CultureInfo)CultureInfo.CurrentCulture.Clone();
culture.NumberFormat.NegativeSign = "Negative";
culture.NumberFormat.NumberDecimalSeparator = "DecimalSeparator";
var str4 = double.MinValue.ToString("R", culture);
Result: Negative1DecimalSeparator7976931348623157E+308
For this reason it's better to use the CultureInfo.InvariantCulture :-)
But if you want to know the truth, in the Control Panel the decimal separator can be long up to 3 characters, and the negative sign up to 4 (you can try it, or you can check the LOCALE_SDECIMAL and LOCALE_SNEGATIVESIGN, clearly the terminating null character can be ignored in .NET)
You can try -1.0 / 3.0, it will have many decimals.

substitute the number after decimal with 0

How do I replace the decimal part of currency with 0's
Here's my cuurency: 166.7
This is to be formatted as 000000016670
The length of this field is 12.
s.padright(12,0); This is will the second part I believe.
The first part will involve replace the number after the decimal with 000..
Thanks
You can multiply by 100 then format the number.
var num = 166.7;
var numString = (num * 100).ToString("000000000000");
Multiplying by 100 turns 166.7 to 16670. Next you need to pad the left part of the number, which is what the ToString does. Each 0 represents a digit. It means, write the number that belongs to that digit, and if no number is present print 0.

How to show only part of an int in c# (like cutting off a part of a credit card number)

If I have a credit number that is an int and I just want to display the last 4 numbers with a * on the left, how would I do this in C#?
For example, 4838382023831234 would shown as *1234
If it's an integer type?
Where i is the int
string maskedNumber = string.Format("*{0}", i % 10000)
This will get the modulus of 10,000 which will return the last four digits of the int
// assumes that ccNumber is actually a string
string hidden = "*" + ccNumber.Substring(ccNumber.Length - 4);
string myCc = myCc.ToString().Substring(12).PadLeft(1, '*');
A credit card number will overflow an int32 and just like phone numbers it doesn't make any sense to think about adding, subtractings, or multiplying credit card numbers. Also string inputs can handle formatting because some users will write in the hyphens. For those reasons, its a lot better to store these objects as strings and reserve numeric value types for data that you actually intend to perform arithmetic on.
I'm not satisfied.
binaryworrier: Mind that if you use modulo, you will get fewer digits for numbers such as
1234123412340001
sshow: mind that, if you use substring(12), you will get fewer digits for numbers such as
0000123412341234
solution would be:
UInt64 ccNumber;
string s = ccNumber.ToString().Text.PadLeft(15, 'myString');
string last = "*"+s.Substring(s.Length-4);
But on a more abstract note, is a credit card number actually a number?
I think not; much more likely that you are going to want to manipulate it digit by digit than perform arithmetic on it. Your advantage of converting char[16] to UInt64 cuts storage space by 50%. No wait, 75% - stupid two-byte-chars!
If the number is stored as a string then this will do it
string ccNumber = "4242424242424242";
string modifiedCCNumber = "*" + ccNumber.Substring(ccNumber.Length - 4);
string cardNo = "1234567890123456";
string maskedNo = "*" + cardNo.Substring(12,4);

Categories