473,395 Members | 1,726 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,395 software developers and data experts.

Converting hexa to decimal

Hello,
I have a hexa file which I need to convert to decimal.
I use memcpy into variables (char for one octet, short for 2 octets
and int for 4 octets) and then print the value into the file by using
fprintf.
The problem is that I don't know how to convert a field of 6 octets?
Should I use a long variable?

Thanks
Nov 14 '05 #1
3 6865
Golan wrote:
Hello,
I have a hexa file which I need to convert to decimal.
I don't know what that means.
I use memcpy into variables
What are you using as the source?
(char for one octet, short for 2 octets
and int for 4 octets)
If you are assuming that these types have exactly those sizes, your code
is not very portable. Any of those types may be larger than you specify,
and int may be either larger or smaller.
and then print the value into the file by using
fprintf.
The problem is that I don't know how to convert a field of 6 octets?
In C99 the type 'long long' is large enough to contain an integer that
size, but there is no type guaranteed to be large enough in earlier C
versions. One of the floating point types may be able to represent
integers that large, if you are willing to do the work to build up the
value from the bytes. Beyond that, all you can do is come up with an
algorithm for converting the bytes to text without storing the entire
value in an intermediate object of built-in type.
Should I use a long variable?


long is not guaranteed to be large enough, and typically isn't.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Nov 14 '05 #2
Let me guess it

union _tagchange {
int a;
char b[4];
} change;

main()
{
int i,j;
int buf[1024];
/*
Read some hexa file,and something to buf,and i
*/
memecpy(change,buf[i],sizeof(int));
for(j=0;j<4;j++)
printf(change.b[i]);
}
"Kevin Goodsell" <us*********************@neverbox.com> дÈëÏûÏ¢ÐÂÎÅ
:w6*******************@newsread2.news.pas.earthlin k.net...
Golan wrote:
Hello,
I have a hexa file which I need to convert to decimal.


I don't know what that means.
I use memcpy into variables


What are you using as the source?
(char for one octet, short for 2 octets
and int for 4 octets)


If you are assuming that these types have exactly those sizes, your code
is not very portable. Any of those types may be larger than you specify,
and int may be either larger or smaller.
and then print the value into the file by using
fprintf.
The problem is that I don't know how to convert a field of 6 octets?


In C99 the type 'long long' is large enough to contain an integer that
size, but there is no type guaranteed to be large enough in earlier C
versions. One of the floating point types may be able to represent
integers that large, if you are willing to do the work to build up the
value from the bytes. Beyond that, all you can do is come up with an
algorithm for converting the bytes to text without storing the entire
value in an intermediate object of built-in type.
Should I use a long variable?


long is not guaranteed to be large enough, and typically isn't.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Nov 14 '05 #3
gabriel wrote:
Let me guess it
OK, but please don't top-post when doing so. Top-posting is rude.

union _tagchange {
Until you know the language much better than you do now, don't use
identifiers beginning with an underscore for any purpose. This
particular case violates the implementation's namespace. From section
7.1.3, paragraph 1:

All identifiers that begin with an underscore are always
reserved for use as identifiers with file scope in both the
ordinary and tag name spaces.
int a;
char b[4];
} change;

main()
Prefer

int main(void)

"Implicit int" has been removed from the C language. Empty parameter
lists on function declarations are a bad idea unless you are sure you
know exactly what you are doing.
{
int i,j;
int buf[1024];
/*
Read some hexa file,and something to buf,and i
*/
memecpy(change,buf[i],sizeof(int));
There is no function memecpy in the C standard library, and you haven't
declared it anyway. This is an error in C99 and a bad idea in any C version.

Assuming you meant memcpy(), you need to #include <string.h>. Also,
there's no implicit conversion to void* from a union or int type, so the
first two arguments are wrong. Besides that, 'i' seems to be uninitialized.

If you meant to copy an int from buf[i] (for some valid value of i in
[0, 1024) ) into the int member of change, there's no reason to use
memcpy at all. Simple assignment works just fine:

change.a = buf[i];
for(j=0;j<4;j++)
printf(change.b[i]);
Using printf here is an error requiring a diagnostic in C99, and a
(possibly) silent invocation of undefined behavior in earlier versions
because there is no prototype in scope. #include <stdio.h> to fix this.

You've also severely screwed up the printf call. What happened to the
format string?

Even with those fixed, however, the code is still questionable. In
general, reading a member of a union other than the most recent member
that had a value stored in it is undefined behavior. unsigned char is
special - it has no trap representations, so inspecting the value of an
unsigned char is always defined. Whether or not this is also true for
plain char is uncertain. I'm not aware of any guarantee (and I can't
locate one) that signed char has no trap representations, so I believe
that the behavior of accessing change.b[i] can be undefined.

Returning a value from main() would be a good idea.
}


-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Nov 14 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Newbee Adam | last post by:
I have been reading in help how I need to use decimal becuase currency does not exist like I used in vb6. I had a difficult time on google and msdn finding how or if I can take the value of text...
1
by: Adam | last post by:
Hello, I'm trying to decifer the data in the table that stores the data in the binary format. All numbers are placed in varbinary fields. All I know is the MS SQL 2000 database useing collation...
2
by: Nadav | last post by:
Hi, How can I use C# to print a number in hexadecimal e.g. printf(" %x "); or print 15 ( decimal ) as 'F' (hexa) Can a number be printed as binary as-well? Thanks in advance, Nadav.
2
by: sarchanakumar | last post by:
Hi friends, I want to write a program to add two hexa-decimal numbers. I know there is no way of direction addtion. My logic is, first convert two hexa-numbers into decimal after that we add and...
1
by: Chandra | last post by:
Hi, I have a problem related to File Reading. I have a file which contains data which is in Hexa format. I have to read that data and push it to a function which takes a parameter of type char....
2
by: mshahzadali | last post by:
is there any expert who could guide me making a C++ program. I want to convert a Decimal number into a Binary, Octal and a Hexadecimal Number using a C++ Built-in Function(if there exist any).
5
by: lokeshrajoria | last post by:
hello, how to convert binary to hexa decimal..... thanks & regards lokiiiii
4
by: Jeff | last post by:
Hey ..NET 2.0 In my code am I trying to convert an int value into a decimal: decimal d = 0; int k = 87664; d = Convert.ToDecimal(k/100);
2
by: clintonb | last post by:
Victor said: The double value that I'm trying to convert to GCSMoney (which is implemented as cents) was produced by multiplying a dollar amount by an interest rate to get interest. double...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.