473,396 Members | 2,033 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Converting 8 bytes to integer

I am working with a binary file and I have 8 bytes that I want to convert to an integer.

This way works:
Expand|Select|Wrap|Line Numbers
  1. unsigned char buf[1024];
  2. long long value;
  3.  
  4. value = ((long long)buf[0]<<56) | ((long long)buf[1]<<48) | ((long long)buf[2]<<40) | ((long long)buf[3]<<32) | (buf[4]<<24) | (buf[5]<<16) | (buf[6]<<8) | buf[7];
  5.  
  6. printf("%lld\n", value);
But it's not very elegant. Is there any elegant way to convert 8 bytes to an integer.
Aug 1 '10 #1

✓ answered by Banfa

That is the way to do it, white space can make it more readable but generally all portable methods will involve doing something similar to what you have done.

2 6558
Banfa
9,065 Expert Mod 8TB
That is the way to do it, white space can make it more readable but generally all portable methods will involve doing something similar to what you have done.
Aug 1 '10 #2
unsigned long long value;

if( 1==fread((unsigned char*)&value,sizeof value,1,FILEptr) )
...
writing the file e.g.
if( 1==fwrite((unsigned char*)&value,sizeof value,1,FILEptr) )
...
Aug 8 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Ken Fettig | last post by:
I have a quick simple question. How do you convert an integer to a string in Python? Thanks Ken Fettig
1
by: Girish | last post by:
HERE IS SOME ASP.NET CODE NEED HELP on transfering this to classic ASP, all I need is valid XML doc so that I can read data from it and display it on the browser // Make sure we have something...
0
by: claire.bell1 | last post by:
Hi, I have an int and a char array in my program, the int is multiple digits. How can I append the int to the char array/string? This is not for use in cout or cin so the << and >> commands...
15
by: jaks.maths | last post by:
How to convert negative integer to hexadecimal or octal number? Ex: -568 What is the equivalent hexadecimal and octal number??
3
by: news.microsoft.com | last post by:
Hello, I'm a VB.NET guy converting a C# app to VB.NET and ran into an issue. Steping through both the C# and VB.NET apps i get identical results but for one section. Notes: bit_buffer = 360...
9
by: hapa | last post by:
I have a problem in this C++ program That i have written. help me with the problem that i have mentioned below I have written this program to convert a integer to binary digit problem one is that...
1
by: sake | last post by:
This problem seems to have a fairly simple solution in C, and google seems to love to pull up solutions for C. However, C++ just seems have the exact opposite luck. So pretty much, I want to...
1
by: freeurmind | last post by:
hello, i'm writing a function that translate Bytes of type unsigned char to integer array of 1's and 0's, i'm taking a message of 33 Bytes and translating them in an int array of 264 integers, here's...
7
by: tragic54 | last post by:
Alright so i've done this in some of my recent programs with option strict on and for some reason When i debug and select the option from the combo box, it crashes and gives me a 'Input String was...
4
by: geeta719 | last post by:
void main() { char ch=""; char c; int k=0,l,i; printf("Enter a number:"); scanf("%d",&i); while(i!=0) //reversing a numbr { k=k*10+i%10; i=i/10;}
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.