Click here to Skip to main content
15,895,740 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
Hi
I have a question to ask. How or is it possible to take a volume serial number convert it into decimal from hex and then some how modify it to generate a portion of a serial number. I have and example as follows:

Example 1:
2350214222		-HDD Serial Number converted from Hex

000A0C3B2EE30087	-Activation Code
   A   B   3   7	-Expiry Date
      3   e   8		-Emp amount 
000 0C  2E  00		-Corresponds to HDD Volume(2350214222) ?


Example 2:
142598462		-HDD Serial Number converted from Hex 87FE13E

000A0E0BA6C30087	-Activation Code
   A   B   3   7	-Expiry Date
      0   C   8		-Emp amount 
000 0E  A6  00		-Corresponds to HDD Volume(142598462) ?

Any comments appreciated.
Posted
Updated 19-Feb-11 4:45am
v2

1 solution

You problem is thinking as hex and decimal as of data types, hence "convert". You cannot "convert" because there is nothing to convert. There is only one integer type your working with. Hex and decimal are only string presentations of it.

It looks like you need to work with separate bytes or semi-bytes in your integer.
For bytes, this is easy. Instead of your 64-bit integer, use 64-bit structure structure:

C#
using System.Runtime.InteropServices;

//...

[StructLayout(LayoutKind.Explicit)]
struct VolumeSerialNumberDecoded {
    [FieldOffset(0)] byte ExpirationDay;
    [FieldOffset(1)] byte Amount; //1-byte shift
    //...
    //you can define intercepting memory areas: this is like union
}


Address separate bytes in this structure.
You you need semi-bytes, its a bit more:

C#
//this will work correctly only for lo or hi in 0..F:
static byte ByteFromSemibytes(byte lo, byte hi) {
    return (byte)(hi << 4 + lo);
}

static byte GetLoSemibyte(byte value) {
    return (byte)(value & 0x0F);
}

static byte GetHiSemibyte(byte value) {
    return (byte)((value & 0xF0) >> 4);
}


You still work with bytes and manipulate with semi-bytes withing the byte.

—SA
 
Share this answer
 
v2
Comments
jarrydpro 25-Feb-11 15:20pm    
Sorry for the complete noob question but how will i run these commands- will i run them in visual studio?
Sergey Alexandrovich Kryukov 25-Feb-11 19:32pm    
Your question is almost like "How can I develop software using VS?" :-)
I modified my code; renamed a structure. You're using something like ulong (64-bit) for your serial number, but actually need access to separate bytes/bits if this 64-bit word. Instead of ulong you should use my structure (modify it according to you documentation); my structure provides convenient access to separate bytes using FieldOffset (see help on this attribute).
--SA
Espen Harlinn 26-Feb-11 10:22am    
Good effort - my 5
Sergey Alexandrovich Kryukov 26-Feb-11 13:37pm    
Thank you.
--SA
Sergey Alexandrovich Kryukov 26-Feb-11 11:05am    
Thank you for accepting my Answer.
Good luck, call again,
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900