More Group Sites
Education Books
School Rankings
Jobless Net
Better Home
Enviro++
更好教育论坛


Help | Subscribe/Unsubscribe | Rules | Other Group Sites: Better Education | Better Education Forum
Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

Share
Options
View
Go to last post Go to first unread
hong  
#1 Posted : Thursday, 2 February 2017 5:53:09 AM(UTC)
hong

Rank: Administration

Reputation:

Groups: AcademicCoachingSchool, admin, Administration, BookSeller, CatholicSchool, CoachingAdult, CoachingProfessional, CoachingSports, ExtraCurriculumCoaching, IndependentSchool, Moderator, MusicTeacher, PrivateSchool, PublicSchool, SelectiveSchool, tutor
Joined: 23/11/2008(UTC)
Posts: 519

Concatenate two integers into hex with specified lengths with leading zeros

The easiest way:

Code:
static ulong ConcatenateInt2Hex(ulong ul1, ulong ul2, int bitLen1, int bitLen2)
{
    var byteSize1 = (int)Math.Ceiling(bitLen1/ 8.0);
    var byteSize2 = (int)Math.Ceiling(bitLen2/ 8.0);
    ulong result = Convert.ToUInt64($"{ul1.ToString("X" + byteSize1 * 2)}{ul2.ToString("X" + byteSize12* 2)}", 16);
    return result;
}


The following two methods use Array.Copy() and Array.Reverse().

Method 2

static ulong ConcatenateInt2Hex(ulong ul1, ulong ul2, int bitLen1, int bitLen2)
{
byte[] bytes1 = null;
byte[] bytes2 = null;
var byteSize1 = (int) Math.Ceiling(bitLen1 / 8.0);
var byteSize2 = (int)Math.Ceiling(bitLen2 / 8.0);
if (bitLen1 != 0)
{
bytes1 = new byte[byteSize1];
Array.Copy(GetBytes(ul1), bytes1, byteSize1);
}
if (bitLen2 != 0)
{
byteSize2 = new byte[byteSize2];
Array.Copy(GetBytes(ul2), bytes2, byteSize2);
}

ulong result = 0;
//reverse order
Array.Reverse(bytes2);
if (bytes1 != null)
{
Array.Reverse(bytes1);
result = Convert.ToUInt64($"{BitConverter.ToString(bytes1).Replace("-", string.Empty)}{BitConverter.ToString(bytes2).Replace("-", string.Empty)}",16);
}
else
{
result = Convert.ToUInt64($"{BitConverter.ToString(bytes2).Replace("-", string.Empty)}", 16);
}

return result;
}

Method 3

static ulong ConcatenateInt2Hex(ulong ul1, ulong ul2, int bitLen1, int bitLen2)
{
byte[] bytes1 = null;
byte[] bytes2 = null;
var byteSize1 = (int) Math.Ceiling(bitLen1 / 8.0);
var byteSize2 = (int)Math.Ceiling(bitLen2 / 8.0);
if (bitLen1 != 0)
{
bytes1 = new byte[byteSize1];
Array.Copy(GetBytes(ul1), bytes1, byteSize1);
}
if (bitLen2 != 0)
{
byteSize2 = new byte[byteSize2];
Array.Copy(GetBytes(ul2), bytes2, byteSize2);
}

byte[] allBytes = null;
ulong result = 0;
allData = new byte[8];
Array.Copy(byte2, allBytes, byteSize2);
if (byteSize1 > 0)
Buffer.BlockCopy(bytes1, 0, allBytes, byteSize2, byteSize1);

//alternative way 1
result = BitConverter.ToUInt64(allBytes, 0);

//alternative way 2
Array.Reverse(allBytes);
result = Convert.ToUInt64($"{BitConverter.ToString(allBytes).Replace("-", string.Empty)}", 16);

return result;
}


BitConverter.ToUInt64()
Returns a 64-bit unsigned integer converted from eight bytes at a specified position in a byte array.
if less than bytes, ArgumentException "destination not long enough"

More resources are abailable from internet:
Byte array to hex string
http://stackoverflow.com...23104/byte-to-hex-string

How do you convert Byte Array to Hexadecimal String, and vice versa?
http://stackoverflow.com...al-string-and-vice-versa

How to get little endian data from big endian in c# using bitConverter.ToInt32 method?
http://stackoverflow.com...-using-bitconverter-toin

Edited by user Sunday, 5 February 2017 10:26:23 AM(UTC)  | Reason: Not specified

Sponsor
Rss Feed  Atom Feed
Users browsing this topic
Guest
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.