Asked by:
How to convert ulong number
Question

Hello,
Please suggest how can we implement the below requirement.
050203060404> This is hexa decimal number which is 48 bits
I need to convert it to 523644. Basically I have removed zero in front of each 16 bits data.
Suppose If I have across 050203000404, then I need to display as 523044.
Please suggest.
click "Proposed As Answer by" if this post solves your problem or "Vote As Helpful" if a post has been useful to you Happy Programming! Hari
All replies

Please suggest how can we implement the below requirement.
050203060404> This is hexa decimal number which is 48 bits
I need to convert it to 523644. Basically I have removed zero in front of each 16 bits data.
Suppose If I have across 050203000404, then I need to display as 523044.
with here. Hexadecimal is just one of several ways to *represent* a number.
So typically we are talking about a string  or text at least  when referring
to values such as 050203060404 or 050203000404 as hex values. A ulong contains
a value in binary which can be *represented* as hex or decimal or binary or
octal etc.
So are you asking how to remove the leading zero from each hex character in a
string representing in hex format a value from a ulong?
While you are pondering your reply, play with this code:
static void Main(string[] args) { //ulong u = 0x050203060404; ulong u = 0x050203000404; string s = string.Format("{0:x}", u); string s2 = ""; for(int n = s.Length  1; n >= 0; n) { s2 += s[n]; } var ca = s2.ToCharArray(); var car = ca.Reverse().ToArray(); string s3 = new string(car); Console.WriteLine(s3); Console.ReadLine(); }
 Wayne


We are dealing with ulong datatype here.
Yes, but when you talk about hex then you are talking about how the value in
the ulong is presented to a human examining its contents. A ulomg contains a
sequence of binary digits  zeros and ones. Whether you view those binary
digits as a sequence of zeros and ones (binary), or as a base ten number
(decimal) or as a base 16 number (hexadecimal) or as a base 8 number (octal)
has no bearing on the value in the variable or constant.
All of the following represent the exact same value:
050203060404 hex
5506198799364 dec
120100301402004 oct
101 0000 0010 0000 0011 0000 0110 0000 0100 0000 0100 bin
So when you say the ulong contains a specific hex value, and you want to
remove the leading zero from each hex character, what *exactly* are you
trying to do? Removing the leading zeros from 050203060404 to give 523644
*where* EXACTLY? If you remove them from the *contents* of the ulong then
you will have an entirely different value.
To put the hex value 523644 into a ulong from a string "523644" you can do
something like the following. Add these lines to my prior example, before
the Console.ReadLine();
ulong ul2; ulong.TryParse(s3, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out ul2); Console.WriteLine("{0:x}", ul2);
 Wayne

Hello Wayne,
Is there any way to deal the above problem with bit operator?
I would like to use bit operator. Please suggest me.
Best Regards,
Hari
click "Proposed As Answer by" if this post solves your problem or "Vote As Helpful" if a post has been useful to you Happy Programming! Hari

Is there any way to deal the above problem with bit operator?
ulong u = 0x050203060404; ulong ch0 = (u << 56) >> 56; ulong ch1 = ((u << 48) >> 56) << 4; ulong ch2 = ((u << 40) >> 56) << 8; ulong ch3 = ((u << 32) >> 56) << 12; ulong ch4 = ((u << 24) >> 56) << 16; ulong ch5 = ((u << 16) >> 56) << 20; ulong res = ch0  ch1  ch2  ch3  ch4  ch5; Console.WriteLine("{0:x}", res);
Note that you still haven't explained what you are trying to accomplish or why.
I don't ask questions in my replies just to exercise my fingers. It's to get
sufficient details to be able to address your issue(s) accurately.
 Wayne