From: Sapuglha <sapuglha@yahoo.com.br>
To: Luciano Moreira - igLnx <lucianolnx@ig.com.br>
Cc: linux-c-programming@vger.kernel.org
Subject: Re: Newbie Question -> writing Hexadecimal values
Date: Thu, 25 Sep 2003 18:35:10 -0300 [thread overview]
Message-ID: <20030925183510.239df26c.sapuglha@yahoo.com.br> (raw)
In-Reply-To: <3F735CC4.5030507@ig.com.br>
Ok, here is what I did:
sapuglha@tiago assembler $ cat test.c
#include<stdio.h>
int main(){
#define DecValFromHexaChar(c) ((c)>='A' && (c)<='F' ?'A'-(c)+10 :(c))
int hexaVal=0;
char szHexa[3]="3E";
hexaVal=DecValFromHexaChar(szHexa[1])*1;
hexaVal+=DecValFromHexaChar(szHexa[0])*16;
FILE *fd;
fd = fopen("exit", "w+");
// This one is right, I really want it this way.
fprintf(fd, "%s", "NIBA");
// This is the thing that I know to work
//fprintf(teste, "%c", 0x3e);
fprintf(fd, "%c", hexaVal);
fclose(fd);
return 1;
}
sapuglha@tiago assembler $ gcc -o test -g test.c
sapuglha@tiago assembler $ ./test
sapuglha@tiago assembler $ cat exit
NIBA6sapuglha@tiago assembler $ hexdump -C exit
00000000 4e 49 42 41 36 |NIBA6|
00000005
sapuglha@tiago assembler $
The hexa "36" in the file is supposed to be "3e"
Maybe on Thu, 25 Sep 2003 18:23:16 -0300
Luciano Moreira - igLnx wrote:
| You need to have a int value and then use it in fprintf().
|
| Try to convert "HexaChar" to int like this:
| -------
| #define DecValFromHexaChar(c) ((c)>='A' && (c)<='F' ?'A'-(c)+10 :(c))
| int hexaVal=0;
| char szHexa[3]="3E";
|
| hexaVal=DecValFromHexaChar(szHexa[1])*1;
| hexaVal+=DecValFromHexaChar(szHexa[0])*16;
| hexaVal+=DecValFromHexaChar(szHexa[-1])*32; // and so on..... WARNING:
| -1 doesnt exist, I m only suggesting you to try create a loop, to
| convert any size of HexaChar.
| // If you have difficulties, I can create it for you.
| --------
|
| If my code has any mistake, I so sorry, I coded it now only for suggest
| you a way to fix your problem,
|
| Luciano
|
|
| Sapuglha wrote:
|
| >Hello people,
| >
| >my problem is:
| >
| >I have an hexadecimal value. It's char, but correspond to an hexa value. Then
| I>want to write it to a file. But in the file, if viewed in hexa (ie. using
| >hexdump) I want to see "3e" and not the correspondig values in hexa to "3"
| and>to "e".
| >
| >An example that works:
| >
| > fprintf(test, "%c", 0x3e);
| >
| >But in my case "3e" is stored in a variable, so I'd like to use something
| like:>
| > strcmp(variable, "3e");
| > fprintf(test, "%c", variable);
| >
| >Is it possible? Any light?
| >
| >Thanks
--
=> Tiago Sant' Anna - UIN: 14252973 - Linux user #136940
[.. ..]
next prev parent reply other threads:[~2003-09-25 21:35 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2003-09-25 21:11 Newbie Question -> writing Hexadecimal values Sapuglha
[not found] ` <3F735CC4.5030507@ig.com.br>
2003-09-25 21:35 ` Sapuglha [this message]
2003-09-25 22:36 ` Jeff Woods
-- strict thread matches above, loose matches on Subject: below --
2003-09-25 21:36 Sandro Dangui
2003-09-25 21:38 ` Sapuglha
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20030925183510.239df26c.sapuglha@yahoo.com.br \
--to=sapuglha@yahoo.com.br \
--cc=linux-c-programming@vger.kernel.org \
--cc=lucianolnx@ig.com.br \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).