ADVERTISEMENT
ADVERTISEMENT

Home » Code Snippets » C/C++ Code Snippets

C program to extract bytes from a long integer hexadecimal number

By: IncludeHelp On 08 DEC 2016

Here, we will learn how to extract bytes from a long integer number (or convert hexadecimal long integer value into byte array)?

In this program we are taking a long integer hexadecimal number of 8 bytes and converting it into byte array i.e. extracting bytes (one- one byte hexadecimal values) from long integer hexadecimal number.

ADVERTISEMENT

Extract bytes from long int hexadecimal number in c language

//Extracted bytes from long int hexadecimal values

#include <stdio.h>
typedef unsigned char BYTE;
int main()
{
	int i,j;
	long int hexValue=0x1122334455667788;
	BYTE byteVal[8];
	
	for(j=0,i=7; i>=0; i--,j++){
		byteVal[j]= (hexValue>>(i*8))&0xff;
	}		
	
	printf("Hexadecimal bytes are:\n");
	for(j=0;j<8;j++){
		printf("%02X ",byteVal[j]);
	}
 
	return 0;
}

Output

    Hexadecimal bytes are:
    11 22 33 44 55 66 77 88

COMMENTS

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.