Converting char array to double in C?

Converting hex to uintvar

  • hi vijayan, I'm trying to write code in c++ language using Dev-C++ program. I'm trying to write a program which takes in hexadecimal numbers from the command-line and converts it to uintvar byte values. I am fairly new to C++ but i have made a start. The problem is that i am unsure of how i would go about the conversion part. I have found some information relating on how the Uintvar conversion works. "A unitvar representation uses variable number of bytes to represent an unsigned integer. Only the lower 7 bits of a byte are used for the number. This means the maximum value a single byte can house is 127. Values greater than 127 bytes will need more than a byte to represent them. The most-significant bit (MSB) of the byte is used as a continuation marker. If MSB is 0, that means no further bytes to follow, and an MSB of 1 means there is a byte to follow. This allows chaining an arbitrary number of bytes. Representing a value as a uintvar: Write down the binary representation of the given value. Group them into 7-bit groups starting with the lowest significant bit. For example, 137 is 10001001. Grouped into 7-bit groups, this will be 1:0001001. For each group, except the last group, add an eighth bit at MSB and set it to 1. For the last group, add the eighth bit at MSB as before but set it to 0. Our 137 example, then becomes 10000001:00001001. In other words, the uintvar corresponding to 137 (89 in hex) has two bytes: 81 and 9, both in hex. Note that a uintvar is related to base-128 representation of an integer. For example, 137 in base-128 is 19. The only addition we have for uintvar is the presence of the continuation bit. You can use the techniques you learnt earlier in this course to represent values in, for example, hex to represent values in base-128. Once the value is in base-128, then the only addition required for uintvar is to set the continuation bits appropriately. You may find this observation useful. When we have 7-bit values in a byte, setting the MSB will amount to adding 80 (hex) to the byte value. Examples: The value 128 requires two bytes: 1000 0001 0000 0000. And value 137 will be: 1000 0001 0000 1001. The second bytes for the values 128 and 137 have their first bits as 0, to indicate there are no more bytes to follow. " So for example the typical run of the application would be: /a.out 80 3ffe which should print to the standard output the lines 80: 81 0 3ffe: ff 7e (and nothing else). Here byte values (81, 0) is the uintvar representation of 80 (hex). And byte values (ff, 7e) is the uintvar representation of 3ffe (hex). This is what i have so far: using namespace std; //print all arguments void convert(int number) { printf("Converting %0X\n", number); unsigned char result[16]; /* storing bytes of the result */ int resultIx = 0; /* index into the result array */ /* zero out the result before starting */ memset(result, 0, sizeof(result)); while (number != 0) { /* get the bit */ int bit = number & 1; number = number >> 1; cout } /* print out the result (backwards!) */ for(int ix = resultIx; ix >= 0; --ix) { printf("%0X ", result[ix]); } puts("\n"); } int main(void) { convert(0x80); convert(0x3FFE); } As you can probably see it doesn't compile properly and i'm having trouble with the conversion part. If you could help in anyway ill be really thankfull.

  • Answer:

    The algorithm to perform the conversion is already given: "Representing a value as a uintvar: Write down the binary representation of the given value. Group them into 7-bit groups starting with the lowest significant bit. For example, 137 is 10001001. Grouped into 7-bit groups, this will be 1:0001001. For each group, except the last group, add an eighth bit at MSB and set it to 1. For the last group, add the eighth bit at MSB as before but set it to 0." The easy way to implement is to use std::bitset<> and std::string. void print_as_unitvar( unsigned long n ) { // print the number in hex format std::cout // Take care of number being zero if( n == 0 ) { std::cout enum { MAXBITS = std::numeric_limits ::digits / 7 * 7, MAXGROUPS = MAXBITS/7 }; // Representing a value as a uintvar: // Write down the binary representation of the given value. std::string bitstr = std::bitset (n).to_string() ; // Group them into 7-bit groups std::string sevenbit_groups[MAXGROUPS] ; for( int i=0 ; i // skip over leading zeroes int first_group = 0 ; for( int i=0 ; i // For each group, except the last group, add an eighth bit at MSB and set it to 1 for( int i = first_group ; i // For the last group, add the eighth bit at MSB as before but set it to 0 sevenbit_groups[ (MAXGROUPS-1) ] = '0' + sevenbit_groups[ (MAXGROUPS-1) ] ; // print the groups out in hex format for( int i = first_group ; i ( sevenbit_groups[i] ).to_ulong() ; std::cout }

Miningco.com Visit the source

Was this solution helpful to you?

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.