Does anyone have a nice code snippet for a Guid to Base34 encoder/decoder, I've googled around for it previously and never really found any good sources.
This Number base conversion class in C# could be fairly easily extended to do base34 (or others if you think people will confuse S and 5 or b and 6 or i and j or B and 8 or 9 and g or whatever)
Here's a simplified version... It basically takes a string, calculates the MD5 hash, extracts the first four bytes as an unsigned long (effective mapping the string to a 4-byte number), converts that to base36 and then swaps out the "oh" and "zero" chars for "X" and "Y". Then, it ensures the final string is only six chars, padding with "Z" chars if needed.
require 'digest/md5'
# create an easy-to-read 6-digit unique idno
idno = original # starting string
idno = Digest::MD5.digest(idno).unpack("N").first # digest as unsigned long
idno = idno.to_s(36).upcase.tr("0O","XY") # convert to base34 (no "oh" or "zero")
idno = idno[0,6].ljust(6,"Z") # final 6-digit unique idno (pad with "Z" chars)
The key methods here are ToByteArray and this particular constructor.
Encode:
string encodedGuid = Convert.ToBase64String(guid.ToByteArray());
Decode:
Guid guid = new Guid(Convert.FromBase64String(encodedGuid));
Related
I don't quite get what {1:X} is in this piece of code:
ushort secretKey = 0x0088; // The cyphering key.
char character = 'A'; // The initial key to be cyphered.
Console.WriteLine("Initial symbol: {0}, its code in the symbols' table: {1:X}", character, (byte)character);
I mean, I realize that {0:X} means a lowercase hexadecimal, but does that mean that {1:X} is a decimal? Thanks for explaining.
It is known as composite formatting. 1 means second argument and X means hexadecimal. Here is the list.
So, i need to calculate byte arrays in my program and i noticed weird thing:
string aaa = "F8F9FAFBFCFD";
string aaaah = "10101010101";
BigInteger dsa = BigInteger.Parse(aaa, NumberStyles.HexNumber) + BigInteger.Parse(aaaah, NumberStyles.HexNumber);
MessageBox.Show(dsa.ToString("X"));
When i add aaa + aaah, it displays me 9FAFBFCFDFE, but it should display F9FAFBFCFDFE, but when i subtract it does it right, aaa - aaah, displays F7F8F9FAFBFC, everything should be right in my code.
BigInteger.Parse interprets "F8F9FAFBFCFD" as the negative number -7,722,435,347,203 (using two's complement) and not 273,752,541,363,453 as you were probably expecting.
From the documentation for BigInteger.Parse:
If value is a hexadecimal string, the Parse(String, NumberStyles)
method interprets value as a negative number stored by using two's
complement representation if its first two hexadecimal digits are
greater than or equal to 0x80. In other words, the method interprets
the highest-order bit of the first byte in value as the sign bit.
To get the result you are expecting, prefix aaa with a 0 to force it to be interpreted as a positive value:
string aaa = "0F8F9FAFBFCFD";
string aaaah = "10101010101";
BigInteger dsa = BigInteger.Parse(aaa, NumberStyles.HexNumber)
+ BigInteger.Parse(aaaah, NumberStyles.HexNumber);
MessageBox.Show(dsa.ToString("X")); // outputs 0F9FAFBFCFDFE
Hi i have this int (example starts with 10)and i have an incremental function which increments the int so the next number would be 11. I would like to change the int to 010 and 011 etc etc.
I found this code online which helps to pad the number but i am not sure how to display put it to a variable...
int intValue = 10;
Console.WriteLine("{0,22} {1,22}", intValue.ToString("D3"), intValue.ToString("X3"));
Console.WriteLine("{0,22:D8} {0,22:X8}", byteValue);
i would like to change it so that i can create a new intvalue2(which is not created yet) to be 010.
Thanks in advance...
The .ToString() method is creating a string not another int. So the number 010 is not actually a number but a string.
string pInt = intValue.ToString("D3");
Now pInt = 010 represented as a string.
So I have an integer number 208 I don't expect many to understand why I am doing this, but the end result of what I am trying to do is get the base-10 representation of octal number 208 (two-zero-eight). I expect that the confusing thing (for people that will try and answer this question) is that while 208 is an integer, I am using it more like a string containing the characters two, zero, and eight. Please let me know if there are any more questions on this, as I think it will cause some confusion.
Anyway, to get the base-10 representation of "208" here is what I do:
Convert int 208 into string "208".
Take the string "208", and parse from octal to decimal.
Then, here is the corresponding source code:
public byte OctalToDecimal(int octalDigits)
{
byte decimalValue = 0;
string octalString = string.Empty;
// first, get a string representation of the integer number
octalString = octalDigits.ToString();
// now, get the decimal value of the octal string
decimalValue = Convert.ToByte(octalString, 8);
// set the decimal-value as the label
return decimalValue;
}
I get a format exception when octalDigits = 208. I get a message about there being additional characters in the octalString's value. Why would that be? All I do is convert from int to string it's very short/simple, and not like I append anything on there. What is going on?
You should know that the digits for octal numbers are in the range 0 to 7
Here, some helpful links
the octal representations of bytes range from 000 to 377?
http://www.asciitable.com/
Octal numbers can not contain digit 8, like base-10 representation can't contain "digit" 10 and binary can't contain digit 2.
I have a byte array containing value like this:
byte[] data={0x04,0x00};
I need to convert it to a string a print it as str_data=0x400
But when i convert this to string the data is printed as 40 where last 0x00 is considered as only 0.
I am new to C# and I am struggling to solve this. Please help.
Your question is a bit unclear, but I think what you want is the X2 format specifier for bytes, which will print your bytes as two hex digits, e.g.:
byte b = 0x40;
Console.WriteLine( b.ToString( "X2" ) ); // Prints '40'
Convert each of your bytes into a string (with e.g. LINQ's Select method), then join them and add the "0x" prefix.