Solved

# Convert byte to hex, binary, octal, quanternary

Posted on 2004-11-09
482 Views
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?
0
Question by:Kevin_P23

LVL 55

Expert Comment

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

Author Comment

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

LVL 16

Expert Comment

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

LVL 50

Expert Comment

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

Author Comment

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)
{
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

LVL 50

Expert Comment

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

LVL 50

Expert Comment

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

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)
{
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

LVL 16

Assisted Solution

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

LVL 50

Accepted Solution

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

LVL 12

Expert Comment

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

Author Comment

Got it guys! Thanks for all your help.

Kevin
0

## Join & Write a Comment Already a member? Login.

### Suggested Solutions

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

#### 746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

#### Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!