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

conversion into hexadecimal values

hello
im programming in c.i have to write about 100 hexadecimal values in to
a file. how do i do it?pls help me

Aug 30 '07 #1
11 3333
<sw****************@yahoo.comwrote:
im programming in c.i have to write about 100 hexadecimal values in to
a file. how do i do it?pls help me
Computer files consist of 1's and 0's. Hexadecimal is a *representation* of
a number. I suggest you read the article in the link,reformulate your
question if you still have one, and post again.

http://en.wikipedia.org/wiki/Hexadecimal
Aug 30 '07 #2
Chris Dollin said:

<snip>
There are no "hexadecimal values";
You could, however, be forgiven for thinking there are, if you were
reading the C Standard. It is a little loose in its terminology at
times.

<snip>

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Aug 30 '07 #3
On Aug 30, 8:03 pm, "osmium" <r124c4u...@comcast.netwrote:
<sweeet_addictio...@yahoo.comwrote:
im programming in c.i have to write about 100 hexadecimal values in to
a file. how do i do it?pls help me

Computer files consist of 1's and 0's. Hexadecimal is a *representation* of
a number. I suggest you read the article in the link,reformulate your
question if you still have one, and post again.

http://en.wikipedia.org/wiki/Hexadecimal

im sorry ..may be my question wasnt tht clear.i have to write the
following values into a file(say using fwrite)

4d 54 68 64 00 00 00 06 00 00 00 01 00 80 4d 54 72 6b 00 00 00 8c 00
ff 58
04 04 02 30 08 00 ff 59 02 00 00 00 90 3c 28 81 00 90 3c 00 00 90 3c
1e 81
00 90 3c 00 00 90 43 2d 81 00 90 43 00 00 90 43 32 81 00 90 43 00 00
90 45
2d 81 00 90 45 00 00 90 45 32 81 00 90 45 00 00 90 43 23 82 00 90 43
00 00
90 41 32 81 00 90 41 00 00 90 41 2d 81 00 90 41 00 00 90 40 32 40 90
40 00
40 90 40 28 40 90 40 00 40 90 3e 2d 40 90 3e 00 40 90 3e 32 40 90 3e
00 40
90 3c 1e 82 00 90 3c 00 00 ff 2f 00
all these values are to be written into a file
how do i do it?

Aug 30 '07 #4
In article <11*********************@m37g2000prh.googlegroups. com>,
<sw****************@yahoo.comwrote:
>im sorry ..may be my question wasnt tht clear.i have to write the
following values into a file(say using fwrite)
4d 54 68 64 00 00 00 06 00 00 00 01 00 80 4d 54 72 6b 00 00 00 8c 00
ff 58
[...]
>all these values are to be written into a file
how do i do it?
Code fragment:

unsigned char outputbuffer[] = {
0x4d, 0x54, 0x68, 0x64, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00,
0x01, 0x00, 0x80, 0x4d, 0x54, 0x72, 0x6b, 0x00, 0x00, 0x00, 0x8c, [etc]
};

filestream = fopen('filename', 'w');
fwrite(outputbuffer, sizeof outputbuffer, 1, filestream);
--
All is vanity. -- Ecclesiastes
Aug 30 '07 #5
ro******@ibd.nrc-cnrc.gc.ca (Walter Roberson) writes:
filestream = fopen('filename', 'w');
Both of these should be "string literals", not 'character
constants'.
--
char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[]
={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa6 7f6aaa,0xaa9aa9f6,0x11f6},*p
=b,i=24;for(;p+=!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+
2:{i++;if(i)break;else default:continue;if(0)case 1:putchar(a[i&15]);break;}}}
Aug 30 '07 #6
In article <87************@blp.benpfaff.org>,
Ben Pfaff <bl*@cs.stanford.eduwrote:
>ro******@ibd.nrc-cnrc.gc.ca (Walter Roberson) writes:
>filestream = fopen('filename', 'w');

Both of these should be "string literals", not 'character
constants'.
Opps, you are right. I've been programming in Matlab a lot lately,
which uses '' for string literals.
--
There are some ideas so wrong that only a very intelligent person
could believe in them. -- George Orwell
Aug 30 '07 #7
sw****************@yahoo.com wrote:
>
im programming in c.i have to write about 100 hexadecimal values
in to a file. how do i do it?pls help me
This may be helpful:

/* Routines to display values in various bases */
/* with some useful helper routines. */
/* by C.B. Falconer, 19 Sept. 2001 */
/* Released to public domain. Attribution appreciated */

#include <stdio.h>
#include <string.h>
#include <limits.h /* ULONG_MAX etc. */

/* ======================= */
/* reverse string in place */
size_t revstring(char *stg)
{
char *last, temp;
size_t lgh;

lgh = strlen(stg);
if (lgh 1) {
last = stg + lgh; /* points to '\0' */
while (last-- stg) {
temp = *stg; *stg++ = *last; *last = temp;
}
}
return lgh;
} /* revstring */

/* ============================================ */
/* Mask and convert digit to hex representation */
/* Output range is 0..9 and a..f only */
int hexify(unsigned int value)
{
static char hexchars[] = "0123456789abcdef";

return (hexchars[value & 0xf]);
} /* hexify */

/* ================================================== */
/* convert unsigned number to string in various bases */
/* 2 <= base <= 16, controlled by hexify() */
/* Returns actual output string length */
size_t basedisplay(unsigned long number, unsigned int base,
char *stg, size_t maxlgh)
{
char *s;

/* assert (stg[maxlgh]) is valid storage */
s = stg;
if (maxlgh && base)
do {
*s = hexify(number % base);
s++;
} while (--maxlgh && (number = number / base) );
*s = '\0';
revstring(stg);
return (s - stg);
} /* basedisplay */

/* ================================================ */
/* convert signed number to string in various bases */
/* 2 <= base <= 16, controlled by hexify() */
/* Returns actual output string length */
size_t signbasedisplay(long number, unsigned int base,
char * stg, size_t maxlgh)
{
char *s;
size_t lgh;
unsigned long n;

s = stg; lgh = 0;
n = (unsigned long)number;
if (maxlgh && (number < 0L)) {
*s++ = '-';
maxlgh--;
n = -(unsigned long)number;
lgh = 1;
}
lgh = lgh + basedisplay(n, base, s, maxlgh);
return lgh;
} /* signbaseddisplay */
/* ==================== */
/* flush to end-of-line */
int flushln(FILE *f)
{
int ch;

while ('\n' != (ch = fgetc(f)) && (EOF != ch)) /* more */;
return ch;
} /* flushln */

/* ========== END of generically useful routines ============ */

/* ========================= */
/* Prompt and await <return*/
static void nexttest(char *prompt)
{
static char empty[] = "";

if (NULL == prompt) prompt = empty;
printf("\nHit return for next test: %s", prompt);
fflush(stdout);
flushln(stdin);
} /* nexttest */

/* ============================== */
/* Display a value and its length */
static void show(char *caption, int sz, char *stg)
{

if ((unsigned)sz != strlen(stg))
printf("Something is wrong with the sz value\n");
printf("%s: sz = %2d \"%s\"\n", caption, sz, stg);
} /* show */

/* =========== */
/* exercise it */
int main(void)
{
#define LGH 40
#define VALUE 1234567890

char stg[LGH];
unsigned int base;
int sz;

printf("\nExercising basedisplay routine\n");
printf("\nbase sz value\n");
for (base = 2; base <= 16; base++) {
sz = (int)basedisplay(VALUE, base, stg, LGH - 1);
printf("%2d %2d %s\n", base, sz, stg);
}

nexttest("ULONG_MAX");
for (base = 8; base <= 16; base++) {
sz = (int)basedisplay(ULONG_MAX, base, stg, LGH - 1);
printf("%2d %2d %s\n", base, sz, stg);
}

basedisplay(0, 10, stg, 3);
printf("\nzero %s\n", stg);

basedisplay(VALUE, 10, stg, 3);
printf("3 lsdigits only, base 10 %s\n", stg);

printf("\nBad calls:\n");

sz = (int)basedisplay(VALUE, 10, stg, 0);
show("0 length field", sz, stg);

sz = (int)basedisplay(VALUE, 1, stg, 20);
show("base 1, lgh 20", sz, stg);

sz = (int)basedisplay(VALUE, 0, stg, 20);
show("base 0, lgh 20", sz, stg);

sz = (int)signbasedisplay(-1234, 10, stg, 0);
show("0 lgh fld, -ve", sz, stg);

sz = (int)signbasedisplay(-1234, 10, stg, 2);
show("truncate -1234", sz, stg);

nexttest("System limits");

sz = (int)signbasedisplay(SCHAR_MIN, 10, stg, 20);
show("SCHAR_MIN ", sz, stg);

sz = (int)signbasedisplay(SCHAR_MAX, 10, stg, 20);
show("SCHAR_MAX ", sz, stg);

sz = (int)signbasedisplay(UCHAR_MAX, 10, stg, 20);
show("UCHAR_MAX ", sz, stg);

sz = (int)signbasedisplay(CHAR_MIN, 10, stg, 20);
show("CHAR_MIN ", sz, stg);

sz = (int)signbasedisplay(CHAR_MAX, 10, stg, 20);
show("CHAR_MAX ", sz, stg);

sz = (int)signbasedisplay(MB_LEN_MAX, 10, stg, 20);
show("MB_LEN_MAX ", sz, stg);

sz = (int)signbasedisplay(SHRT_MIN, 10, stg, 20);
show("SHRT_MIN ", sz, stg);

sz = (int)signbasedisplay(SHRT_MAX, 10, stg, 20);
show("SHRT_MAX ", sz, stg);

sz = (int)signbasedisplay(USHRT_MAX, 10, stg, 20);
show("USHRT_MAX ", sz, stg);

sz = (int)signbasedisplay(INT_MIN, 10, stg, 20);
show("INT_MIN ", sz, stg);

sz = (int)signbasedisplay(INT_MAX, 10, stg, 20);
show("INT_MAX ", sz, stg);

sz = (int)signbasedisplay(INT_MAX, 10, stg, 20);
show("INT_MAX ", sz, stg);

sz = (int) basedisplay(UINT_MAX, 10, stg, 20);
show("UINT_MAX ", sz, stg);

sz = (int)signbasedisplay(LONG_MIN, 10, stg, 20);
show("LONG_MIN ", sz, stg);

sz = (int)signbasedisplay(LONG_MAX, 10, stg, 20);
show("LONG_MAX ", sz, stg);

sz = (int) basedisplay(ULONG_MAX, 10, stg, 20);
show("ULONG_MAX ", sz, stg);

nexttest("DONE");
return 0;
} /* main */

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Aug 30 '07 #8
sw****************@yahoo.com wrote:
hello
im programming in c.i have to write about 100 hexadecimal values in to
a file. how do i do it?pls help me
I guess you want to write binary data as a hex string...
void write_hex(FILE *out, unsigned char *binary, size_t binary_len)
{
size_t i;

for (i=0; i<binary_len; i++)
fprintf(out, "%02X", (int)binary[i]);
}

--
Tor <torust [at] online [dot] no>
Aug 31 '07 #9
On Fri, 31 Aug 2007 17:23:10 +0200, Tor Rustad
<to********@hotmail.comwrote:
>sw****************@yahoo.com wrote:
>hello
im programming in c.i have to write about 100 hexadecimal values in to
a file. how do i do it?pls help me

I guess you want to write binary data as a hex string...
void write_hex(FILE *out, unsigned char *binary, size_t binary_len)
{
size_t i;

for (i=0; i<binary_len; i++)
fprintf(out, "%02X", (int)binary[i]);
The cast should not be necessary.
>}

Remove del for email
Sep 1 '07 #10
Barry Schwarz <sc******@doezl.netwrites:
On Fri, 31 Aug 2007 17:23:10 +0200, Tor Rustad
<to********@hotmail.comwrote:
>>sw****************@yahoo.com wrote:
>>hello
im programming in c.i have to write about 100 hexadecimal values in to
a file. how do i do it?pls help me

I guess you want to write binary data as a hex string...
void write_hex(FILE *out, unsigned char *binary, size_t binary_len)
{
size_t i;

for (i=0; i<binary_len; i++)
fprintf(out, "%02X", (int)binary[i]);

The cast should not be necessary.
>>}
Personally, I'd cast it to unsigned rather than to int.

The "%02X" format requires an unsigned arguments. 'binary[i]' is of
type unsigned char, which will normally promote to signed int (but it
could promote to unsigned int if sizeof(int)==1, which implies
CHAR_BIT>=16).

An int argument (as long as it's non-negative) *should* be acceptable
when an unsigned int is expected. I don't remember off the top of my
head whether the standard explicitly guarantees this or just says in a
footnote that it's intended to work.

This is one of the rare cases where I'd be confortable with a cast,
but to unsigned int, not to int.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Sep 1 '07 #11
Barry Schwarz wrote:
On Fri, 31 Aug 2007 17:23:10 +0200, Tor Rustad
<to********@hotmail.comwrote:
[...]
>void write_hex(FILE *out, unsigned char *binary, size_t binary_len)
{
size_t i;

for (i=0; i<binary_len; i++)
fprintf(out, "%02X", (int)binary[i]);

The cast should not be necessary.
The cast should be (unsigned int).

I don't use energy on whether it's needed or not, it's just old habit to
put the cast there, to get a lint clean compile.
--
Tor <torust [at] online [dot] no>
Sep 1 '07 #12

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

Similar topics

10
by: pavithra.eswaran | last post by:
Hi, I would like to convert a single precision hexadecimal number to floating point. The following program seems to work fine.. But I do not want to use scanf. I already have a 32 bit hexadecimal...
2
by: akash deep batra | last post by:
hi i want to convert a 96 bit binary number into a hexadecimal number. e.g binary number= 001100010001010000100101011110111111010101110100010110000101011000101010000000000000000000000000 how...
2
by: Henry | last post by:
Hi, I have a Byte array that holds hexadecimal values (ASCII e.g. byte value is hex 54 for the letter T). Dim bytes() As Byte = New Byte(size) {} ..... get the bytes array
1
by: Fernando Barsoba | last post by:
Hi all, First of all, I'd like to thank you "Skarmander" and "Flash Gordon" for the help they provided me: Skarmander's algorithm and Flash's modifications helped me a lot. Here's the problem...
11
by: santosh | last post by:
Hello all, Conversion macros along the name of INT8_C, INT16_C etc, are defined in stdint.h to convert their argument into suitable representations for their corresponding types, i.e. int8_t,...
11
by: chaitali.pats | last post by:
Hi , I have implemented MD5 in C language . I am getting an output of 32 bits Hexadecimal number . for example : 83a80d3ca057492f0ce99ac1db8dced0 I need to convert this string same to 16...
6
by: Andrea | last post by:
Hi, suppose that I have a string that is an hexadecimal number, in order to print this string I have to do: void print_hex(unsigned char *bs, unsigned int n){ int i; for (i=0;i<n;i++){...
14
by: Ellipsis | last post by:
Ok so I am converting to hexadecimal from decimal and I can only cout the reverse order of the hexadecimal?! How could I reverse this so its the right order? Heres my code: #include <iostream>...
6
by: i_robot73 | last post by:
I have a file, containing hex values for dates (MMDDYYYY)<status code><??such as: ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.