Implementation of the Variable length integer in C Anyone can give me a idea of how implement in C this concept of Variable length integer which encoded the numer of transactions in a block? based on what is described here, I think is something lime that:

if(value=?)
    uint8_t number_transactions = length;
else if(value=?)
    uint16_t number_transactions = 0xFD+length;
else if(value=?)
    uint32_t number_transactions = 0xFE_length;
else
    uint64_t number_transactions = 0xFF_length;


where number_transactions would be the value concatenated on the block. But how implement this? This code does the same described in the bitcoin wiki?

UPDATE

char* varint(unsigned long size) {
  char* number;
  if(size &lt; 252) {
    number = malloc(1);
    number[0] = (uint8_t)size;
  } else if(size&gt;=253 &amp;&amp; size&lt;65535) {
    number = malloc(3);
    number[0] = 0xfd;
    memcpy(&amp;number[1], (uint16_t)&amp;size, sizeof(uint16_t));
  } else if(size&gt;=65536 &amp;&amp; size&lt;4294967295) {
    number = malloc(5);
    number[0] = 0xfe;
    memcpy(&amp;number[1], (uint32_t)&amp;size, sizeof(uint32_t));
  } else if(size&gt;=4294967296) {
    number = malloc(9);
    number[0] = 0xff;
    memcpy(&amp;number[1], (uint64_t)&amp;size, sizeof(uint64_t));
  }
  return NULL;
}


message from compiler:

bitcoin_rpc.c: In function ‘varint’:
bitcoin_rpc.c:230:24: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
     memcpy(&amp;number[1], (uint16_t)&amp;size, sizeof(uint16_t));
                        ^
bitcoin_rpc.c:230:24: warning: passing argument 2 of ‘memcpy’ makes pointer from integer without a cast [-Wint-conversion]
In file included from bitcoin_rpc.c:4:0:
/usr/include/string.h:42:14: note: expected ‘const void * restrict’ but argument is of type ‘short unsigned int’
 extern void *memcpy (void *__restrict __dest, const void *__restrict __src,
              ^~~~~~
bitcoin_rpc.c:234:24: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
     memcpy(&amp;number[1], (uint32_t)&amp;size, sizeof(uint32_t));
                        ^
bitcoin_rpc.c:234:24: warning: passing argument 2 of ‘memcpy’ makes pointer from integer without a cast [-Wint-conversion]
In file included from bitcoin_rpc.c:4:0:
/usr/include/string.h:42:14: note: expected ‘const void * restrict’ but argument is of type ‘unsigned int’
 extern void *memcpy (void *__restrict __dest, const void *__restrict __src,
              ^~~~~~
bitcoin_rpc.c:238:24: warning: passing argument 2 of ‘memcpy’ makes pointer from integer without a cast [-Wint-conversion]
     memcpy(&amp;number[1], (uint64_t)&amp;size, sizeof(uint64_t));
                        ^
In file included from bitcoin_rpc.c:4:0:
/usr/include/string.h:42:14: note: expected ‘const void * restrict’ but argument is of type ‘long unsigned int’
 extern void *memcpy (void *__restrict __dest, const void *__restrict __src,
No. The compact size unsigned integers are not uints (except for the base one of one byte). They are a serialization, so it is actually just an array of bytes. You just compare the number you are serializing to the boundaries and determine from there what the prefix byte is. Bitcoin.org does a better job of describing this: https://bitcoin.org/en/developer-reference#compactsize-unsigned-integers. Note that you will need to understand hex numbers are and how they work in C.