Hex

Share your ZGE-development tips and techniques here!

Moderator: Moderators

Post Reply
User avatar
Kjell
Posts: 1787
Joined: Sat Feb 23, 2008 11:15 pm

Hex

Post by Kjell » Tue Mar 16, 2021 2:13 pm

:idea:

Couple of ( unrolled ) functions to convert integers to hex-strings.

Code: Select all

private string getHex(int n)
{
  return n < 10 ? chr(48+n) : chr(55+n);
}

string hex8(int n)
{
  int n1, n2;

  n1 = (n>>00)&0xF;
  n2 = (n>>04)&0xF;

  return "0x"+getHex(n2)+getHex(n1);
}

string hex16(int n)
{
  int n1, n2, n3, n4;

  n1 = (n>>00)&0xF;
  n2 = (n>>04)&0xF;
  n3 = (n>>08)&0xF;
  n4 = (n>>12)&0xF;

  return "0x"+getHex(n4)+getHex(n3)
             +getHex(n2)+getHex(n1);
}

string hex24(int n)
{
  int n1, n2, n3, n4, n5, n6;

  n1 = (n>>00)&0xF;
  n2 = (n>>04)&0xF;
  n3 = (n>>08)&0xF;
  n4 = (n>>12)&0xF;
  n5 = (n>>16)&0xF;
  n6 = (n>>20)&0xF;

  return "0x"+getHex(n6)+getHex(n5)
             +getHex(n4)+getHex(n3)
             +getHex(n2)+getHex(n1);
}

string hex32(int n)
{
  int n1, n2, n3, n4, n5, n6, n7, n8;

  n1 = (n>>00)&0xF;
  n2 = (n>>04)&0xF;
  n3 = (n>>08)&0xF;
  n4 = (n>>12)&0xF;
  n5 = (n>>16)&0xF;
  n6 = (n>>20)&0xF;
  n7 = (n>>24)&0xF;
  n8 = (n>>28)&0xF;

  return "0x"+getHex(n8)+getHex(n7)
             +getHex(n6)+getHex(n5)
             +getHex(n4)+getHex(n3)
             +getHex(n2)+getHex(n1);
}
K

Post Reply