Uniswap Sniper Bot  1.0
utils.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <cstdint>
4 #include <cstring>
5 #include <stdexcept>
6 
10 namespace Utils {
11  using Byte = std::uint8_t;
12  using Buffer = Byte*;
13 
22  inline Byte hexCharToByte(char x) {
23  if(x >= '0' && x <= '9') return x - '0';
24  if(x >= 'A' && x <= 'F') return x - 'A' + 10;
25  if(x >= 'a' && x <= 'f') return x - 'a' + 10;
26  throw std::invalid_argument("Invalid argument");
27  }
28 
37  inline char byteToHexChar(Byte x) {
38  if(x <= 9) return x + '0';
39  if(x >= 10 && x <= 15) return (x - 10) + 'a';
40  throw std::invalid_argument("Invalid argument");
41  }
42 
52  inline std::size_t hexStringToBuffer(const char *input, std::size_t inputLength, Buffer output, bool stripZeroes = false) {
53  if(stripZeroes) {
54  while(*input == '0') {
55  ++input;
56  --inputLength;
57  }
58  }
59  if(inputLength == 0) return 0;
60 
61  std::size_t outputLength = (inputLength + 1) / 2;
62 
63  if(inputLength % 2 == 1) {
64  *(output++) = hexCharToByte(*(input++));
65  --inputLength;
66  }
67 
68  while(inputLength > 0) {
69  *(output++) = 16 * hexCharToByte(*input) + hexCharToByte(*(input + 1));
70  input += 2;
71  inputLength -= 2;
72  }
73 
74  return outputLength;
75  }
76 
85  inline std::size_t hexStringToBuffer(const char *input, Buffer output, bool stripZeroes = false) {
86  return hexStringToBuffer(input, strlen(input), output, stripZeroes);
87  }
88 
98  inline std::size_t bufferToHexString(Buffer input, std::size_t inputLength, char *output, bool nullTerminated = false) {
99  if(inputLength == 0) return 0;
100 
101  Buffer inputEnd = input + inputLength;
102  while(input != inputEnd) {
103  *output = byteToHexChar((*input / 16) % 16);
104  *(output + 1) = byteToHexChar(*input % 16);
105 
106  ++input;
107  output += 2;
108  }
109 
110  if(nullTerminated) *output = '\0';
111 
112  return inputLength * 2;
113  }
114 
122  inline std::size_t intToBuffer(std::uint64_t x, Buffer output) {
123  std::size_t length = 0;
124  if(x > 0xFFFFFFFFFFFFFF) length = 8;
125  else if(x > 0xFFFFFFFFFFFF) length = 7;
126  else if(x > 0xFFFFFFFFFF) length = 6;
127  else if(x > 0xFFFFFFFF) length = 5;
128  else if(x > 0xFFFFFF) length = 4;
129  else if(x > 0xFFFF) length = 3;
130  else if(x > 0xFF) length = 2;
131  else if(x > 0) length = 1;
132  else {
133  *output = 0;
134  return 1;
135  }
136 
137  Buffer outputStart = output + length;
138  for(; x != 0; x >>= 8) *(--outputStart) = x & 0xFF;
139  return length;
140  }
141 }
Namespace holding all converters and other utilities.
Definition: utils.hpp:10
std::size_t intToBuffer(std::uint64_t x, Buffer output)
Converts integer to buffer.
Definition: utils.hpp:122
std::uint8_t Byte
Definition: utils.hpp:11
std::size_t bufferToHexString(Buffer input, std::size_t inputLength, char *output, bool nullTerminated=false)
Converts buffer to hexadecimal string.
Definition: utils.hpp:98
Byte * Buffer
Definition: utils.hpp:12
std::size_t hexStringToBuffer(const char *input, std::size_t inputLength, Buffer output, bool stripZeroes=false)
Converts hexadecimal string to buffer.
Definition: utils.hpp:52
char byteToHexChar(Byte x)
Converts byte to hexadecimal char.
Definition: utils.hpp:37
Byte hexCharToByte(char x)
Converts hexadecimal char to byte.
Definition: utils.hpp:22