Coding ... kinda?

Forgive me if this was asked earlier, but I assure you that I browsed the Internet and did not understand anything, perhaps because I do not have the correct terminology.

I would like to take an integer and convert it to a little-endian (?) Hexadecimal representation as follows:

303 β†’ 0x2f010000

I see that bytes are packed so that 16 and 1 places are in one byte and that places 4096 and 256 places have a common byte. If someone could just tell me the correct terminology for such coding, I am sure that I can find my answer on how to do this. Thanks!

+5
source share
3 answers

AND OR...

, 32- :

int value = 303;
int result = 0x00000000;

for (int i = 0; i < 4; i++)
{
    result = result | ((value & (0xFF << (i * 8))) << (24 - (i * 8)));
}
+2

-endian little-endian . , 0x2f100000, , endianness .

32- , , Demi.

( ), - . . BSD htonl(), 32- .

, htonl (303) == 0x2f100000. , htonl (303) == 303. [0x00, 0x00, 0x01, 0x2f] .

+2

If someone can point out a specific term to what I'm trying to do, I will still listen to it. However, I found a way to do what I needed, and I will post it here so that if someone looks after me, they can find it. There may be (maybe there is) a simpler, more direct way to do this, but here is what I did in VB.Net to return the bytecode I wanted:

Private Function Encode(ByVal original As Integer) as Byte()    
    Dim twofiftysixes As Integer = CInt(Math.Floor(original / 256))
    Dim sixteens As Integer = CInt(Math.Floor((original - (256 * twofiftysixes)) / 16))
    Dim ones As Integer = original Mod 16
    Dim bytecode As Byte() = {CByte((16 * sixteens) + ones), CByte(twofiftysixes), 0, 0}
    Return bytecode
End Function

Effectively decompose an integer into its hexadecimal components, and then convert the corresponding pairs to cBytes.

0
source

All Articles