linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* Newbie Question -> writing Hexadecimal values
@ 2003-09-25 21:11 Sapuglha
       [not found] ` <3F735CC4.5030507@ig.com.br>
  0 siblings, 1 reply; 5+ messages in thread
From: Sapuglha @ 2003-09-25 21:11 UTC (permalink / raw)
  To: linux-c-programming

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 - Linux user #136940
	[.. ..]

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Newbie Question -> writing Hexadecimal values
       [not found] ` <3F735CC4.5030507@ig.com.br>
@ 2003-09-25 21:35   ` Sapuglha
  2003-09-25 22:36     ` Jeff Woods
  0 siblings, 1 reply; 5+ messages in thread
From: Sapuglha @ 2003-09-25 21:35 UTC (permalink / raw)
  To: Luciano Moreira - igLnx; +Cc: linux-c-programming

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
	[.. ..]

^ permalink raw reply	[flat|nested] 5+ messages in thread

* RE: Newbie Question -> writing Hexadecimal values
@ 2003-09-25 21:36 Sandro Dangui
  2003-09-25 21:38 ` Sapuglha
  0 siblings, 1 reply; 5+ messages in thread
From: Sandro Dangui @ 2003-09-25 21:36 UTC (permalink / raw)
  To: sapuglha, linux-c-programming


First, you have to convert the string value to an integer value.
After that, you are able to use the integer value do to what you want...

<snip>
char strValue[] = {"3e"};
int value;

if (sscanf(strValue, "%x", &value) == 1) // converts.
{
	fprintf(test, "%c", value);
}
<snip>






-----Original Message-----
From: Sapuglha [mailto:sapuglha@yahoo.com.br] 
Sent: quinta-feira, 25 de setembro de 2003 18:11
To: linux-c-programming@vger.kernel.org
Subject: Newbie Question -> writing Hexadecimal values


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 - Linux user #136940
	[.. ..]
-
To unsubscribe from this list: send the line "unsubscribe
linux-c-programming" in the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Newbie Question -> writing Hexadecimal values
  2003-09-25 21:36 Sandro Dangui
@ 2003-09-25 21:38 ` Sapuglha
  0 siblings, 0 replies; 5+ messages in thread
From: Sapuglha @ 2003-09-25 21:38 UTC (permalink / raw)
  To: Sandro Dangui; +Cc: linux-c-programming

It worked. Thank you all.

Maybe on Thu, 25 Sep 2003 18:36:09 -0300
Sandro Dangui wrote:

| char strValue[] = {"3e"};
| int value;
| 
| if (sscanf(strValue, "%x", &value) == 1) // converts.
| {
| 	fprintf(test, "%c", value);
| }

-- 
 
=> Tiago Sant' Anna - UIN: 14252973 - Linux user #136940
	[.. ..]

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Newbie Question -> writing Hexadecimal values
  2003-09-25 21:35   ` Sapuglha
@ 2003-09-25 22:36     ` Jeff Woods
  0 siblings, 0 replies; 5+ messages in thread
From: Jeff Woods @ 2003-09-25 22:36 UTC (permalink / raw)
  To: sapuglha; +Cc: Luciano Moreira - igLnx, linux-c-programming

There is at least one bug in your code below:

At 9/25/2003 06:35 PM -0300, Sapuglha wrote:
>#define DecValFromHexaChar(c) ((c)>='A' && (c)<='F' ?'A'-(c)+10 :(c))
>         int hexaVal=0;


I think you mean:
#define DecValFromHexaChar(c) ((c)>='A' && (c)<='F' ?(c)-'A'+10:(c)-'0')
         char hexaVal=0;


Simple function to convert an arbitrary ASCII string from any numeric base 
(up to 36) to an unsigned binary:

int str2int(const char *str, const int base)
{
         int     result = 0;
         char    digit;
         assert(base>1);
         assert(str != NULL);
         while (digit = *str++) {
                 if (digit >= '0' && digit <= '9') digit -= '0';
                 else if (digit >= 'A' && digit <= 'Z') digit -= 'A'-10;
                 else if (digit >= 'a' && digit <= 'z') digit -= 'a'-10;
                 else assert(("invalid digit",0));
                 assert(digit<base);
                 result=result*base+digit;
         }
         return result;
}

int main(void) {        /* sample call */
         printf("%X\n", str2int("DeadBeef", 16));
         return 0;
}

P.S.  That's off the top of my head and there may be logic or other errors, 
but you probably get the gist of the code.

--
Jeff Woods <kazrak+kernel@cesmail.net> 



^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2003-09-25 22:36 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2003-09-25 21:11 Newbie Question -> writing Hexadecimal values Sapuglha
     [not found] ` <3F735CC4.5030507@ig.com.br>
2003-09-25 21:35   ` Sapuglha
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

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).