• C

Convert byte to hex, binary, octal, quanternary

Hi.

Is there a simple way to take an array of single bytes (type char) and convert them to these different bases while only outputting, say, the right most 3 digits?
Kevin_P23Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jaime OlivaresSoftware ArchitectCommented:
There are many aproaches to acomplish this, but your question is not complete, I think.
How many bytes is your array, if it is short, you can convert the array into a single 32 or 64 bit number.
If it is 32 bits maximum, then you can use itoa() function to convert to any base (hex, binary, decimal, octal, etc)
If it is 64 bits maximum, then you can use i64toa() function.
Have a look to:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt__itoa.2c_._i64toa.2c_._ui64toa.2c_._itow.2c_._i64tow.2c_._ui64tow.asp

If array is greater than 8 bytes, then you will need some specific algorithm.
0
Kevin_P23Author Commented:
The array will most likely be greater than 8 bytes. It is a dynamic array consisting of data read from a file using fread. The file is supposed to be treated as a series of byte, so I am storing the data in a dynamic char array. The size of the array depends on which section of the file is asked for at execution (could technically be as much as a couple thousand). Now each byte, upon output, is supposed to displayed as a series of 8 bits for binary, 4 for quarternary, 3 for octal, and 2 for Hex. Would this reqiure an algorithm using bitwise operations?
0
PaulCaswellCommented:
The general algorithm for conversion would be:

void baseConvert(char * str, int l, int n, int b)
{
 // Calculate the highest value + 1 that will fit into a string of length 'l' in base 'b'.
 // E.G. l = 3, b = 10 must produce 1000 (3 digits in base 10).
 // Chop the number 'n' to that many digits.
 // E.G. n = 123456 must become 456.
 // Peel off each digit from the top down into the string.
 // E.G str[0] = '4' str[1] = '5' str[2] = '6';
}

Try coding this as far as you can and post what you get. We'll help some more then.

Paul
0
The Firewall Audit Checklist

Preparing for a firewall audit today is almost impossible.
AlgoSec, together with some of the largest global organizations and auditors, has created a checklist to follow when preparing for your firewall audit. Simplify risk mitigation while staying compliant all of the time!

Julian HansenCommented:
I must admit I am a bit confused here

Kevin are you asking the following. Given an array of bytes

{0x31,0x20,0x19,0xDA,....}

You want for each byte to display its value in diffent base forms.

If so would the following not be the solution?

for ( i = start_byte ; i < last_byte ;i++)
{
   printf ( "%d", array_element[i] ) ; // decimal
   printf ( "%x", array_element[i] ) ; // Hex
   printf ( "%o", arrah_element[i] ) ; // Octal
   printf ( "%s", GetBinaryRep ( array_element[i] ) ; // binary
   printf ( "%s", GetQuarternaryRep ( array_element[i]) ; // quarternary
}

Is this what you are after - if so let us know and we can look at the code for the Binary and Quarternary representation?
0
Kevin_P23Author Commented:
Sorry if my question is confusing.

When I do this:
printf("%o, %o for octal\n", array[0], array[1]);
printf("%X, %X for hex\n", array[0], array[1]);

the output will look like this:
37777777777, 377777777777 for octal
FFFFFFFF, FFFFFFFF for hex

but the way it is supposed to look (according example outputs given) is:
377, 377 for octal
FF, FF for for hex

I'm not sure I'm going to go about outputting binary and quaternay just yet since there is no % format for those. Perhaps there is a problem in the way I am storing the data?

/* s and e represent the start and and bytes from the file to store in the array,
    respectively. byte_n is the total number of bytes in the file (8000 in this case).
    File infile may be formatted or unformatted, but in either case must be treated
    as a sequence of bytes. */

char * store_in_array(int s, int e, int byte_n)
{
  int i = 0;                  
  char *byteptr;
  size_t size;    
   
  size = ((size_t)e - (size_t)s);
   
    /* allocate a char array (since char = 1 byte) of length size */
    if (( byteptr = (char*)malloc(1 * size)) == NULL)
      {
        printf("Storage allocation failed.\n");
      exit(1);
      }
   
    /* set offset to first byte to be read */
     fseek(infile, s, SEEK_SET);
 
 /*read each byte into the array */
 if ( fread(byteptr, 1, size, infile) == 0)
 {
   printf("Problem in fread().\n");
   exit(1);
 }
 
 /*print each byte in octal and hex (binary and quarternary yet to be implemented) */
  for (i; i < size; ++i)
   {    
      printf("byteptr[%d]: %o\n", i, byteptr[i]);
      printf("byteptr[%d]: %X\n", i, byteptr[i]);  
   }
 
 
 return byteptr;
 
}

I came up with a solution for writing hex in the way it should look, but it doesn't seem to be a good programming approach and won't work for octal. I feel like there is something simple that I should, but am overlooking.

in above function:

char buffer[11];

 for(i = 0; i < size; ++i)
   {  
    sprintf(buffer, "%X", byteptr[i]);
    printf("%c%c\n", buffer[6], buffer[7]);
   }

Any comments are greatly appreciated. Thanks for the help.

0
Julian HansenCommented:
I am confused - not sure how a byte value can output

37777777777, 377777777777 for octal
FFFFFFFF, FFFFFFFF for hex

This code produced the right output for me

#include "stdio.h"

int main ()
{
      char x[] = {0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x3a,0x3b,0x3c,0x3d,0x3e,0x3f} ;

      for ( int i = 0; i < 15; i++ )
      {
            printf ( "x[%d] = %x hex\n", i, x[i] ) ;
            printf ( "x[%d] = %o octal\n", i, x[i] ) ;
      }

     return 0 ;
}

I will try your code a little later - gotta run now.

0
Julian HansenCommented:
Ok problem solved

I ran your code - and saw what you were getting.

Change your char * to unsigned char * as per code below - look for lines with (<-- Change ). That should do the trick

// Function header
unsigned char * store_in_array(int s, int e, int byte_n)   <-- Change
{
  int i = 0;                  
  unsigned char *byteptr; <-- Change
  size_t size;    
   
  size = ((size_t)e - (size_t)s);
   
    /* allocate a char array (since char = 1 byte) of length size */
    if (( byteptr = (unsigned char*)malloc(1 * size)) == NULL) <-- Change
      {
        printf("Storage allocation failed.\n");
     exit(1);
      }
   
    /* set offset to first byte to be read */
     fseek(infile, s, SEEK_SET);
 
 /*read each byte into the array */
 if ( fread(byteptr, 1, size, infile) == 0)
 {
   printf("Problem in fread().\n");
   exit(1);
 }
 
 /*print each byte in octal and hex (binary and quarternary yet to be implemented) */
  for (i; i < size; ++i)
   {    
      printf("byteptr[%d]: %o\n", i, byteptr[i]);
      printf("byteptr[%d]: %X\n", i, byteptr[i]);  
   }
 
 
 return byteptr;
 
}


0
PaulCaswellCommented:
You can also use:

     printf("byteptr[%d]: %03.3X\n", i, byteptr[i]);  
 
To limit yourself to 3 digits but binary and quaternary are going to be a problem using this method.

Have a go at 'baseConvert' and post an attempt. We'll have a look and see how we an help.

Paul
0
Julian HansenCommented:
Damn I am slow this week.

You don't need a base convert function - the itoa function does that for you

char szValue[10] ;
iVal = 59 ;
printf ( "%d = %s quarternary\n" ) , iVal, itoa ( iVal, szValue, 4 ) ) ;
printf ( "%d = %s binary\n" ) , iVal, itoa ( iVal, szValue, 2 ) ) ;

// You can choose whether or not to use this method for Octal and Hex or the %x and %o methods shown above.


0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
stefan73Commented:
Hi Kevin_P23,
The fastest way is to use a lookup table:

char* lookup_binary[256]={"00000000", "00000001", "00000010", ....};

Don't forget to cast a char to unsigned char before using it as an array index for the table.

Cheers!

Stefan
0
Kevin_P23Author Commented:
Got it guys! Thanks for all your help.

Kevin
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.