Connecting Tech Pros Worldwide Help | Site Map

ASCII to HEX

Tyler Kellen
Guest
 
Posts: n/a
#1: Nov 14 '05
char string[] = "1d,4c,20,00";
char target[] = {0x1d,0x4c,0x20,0x00};
char buf[64];

I have searched newgroups and am actively searching online for a
function to take the value of string and place it into buf so that buf
== target.

Any ideas would be greatly appreciated,
Tyler Kellen
Michael Mair
Guest
 
Posts: n/a
#2: Nov 14 '05

re: ASCII to HEX



Tyler Kellen wrote:[color=blue]
> char string[] = "1d,4c,20,00";
> char target[] = {0x1d,0x4c,0x20,0x00};
> char buf[64];
>
> I have searched newgroups and am actively searching online for a
> function to take the value of string and place it into buf so that buf
> == target.[/color]

Sounds quite like homework for me.
Give us your code or ideas and we will help you improve it/
find errors or possible sources of errors.

Functions which may help you writing the code:
strtoul(), sscanf(). For general separator handling, use a
string (e.g. char separators[]=",; ") and strchr() when parsing
string.
Or do it by hand, extracting the hex digits of one number
and converting it yourself.
Note: Hex expressions usually denote nonnegative numbers,
so
unsigned char target[] = {0x1d,0x4c,0x20,0x00};
unsigned char buf[64];
may be more appropriate.


Cheers
Michael
--
E-Mail: Mine is a gmx dot de address.

Mike Wahler
Guest
 
Posts: n/a
#3: Nov 14 '05

re: ASCII to HEX


"Tyler Kellen" <tyler@stardotbox.com> wrote in message
news:64072993.0411041012.7d222ea@posting.google.co m...[color=blue]
> char string[] = "1d,4c,20,00";
> char target[] = {0x1d,0x4c,0x20,0x00};
> char buf[64];
>
> I have searched newgroups and am actively searching online for a
> function to take the value of string and place it into buf so that buf
> == target.[/color]

Don't search online, look in a C book. The C standard library
contains two functions which will convert text to a numeric
value, in various number bases, including hexadecimal.
Look up 'sscanf()' and 'strtoul()'.

-Mike



SM Ryan
Guest
 
Posts: n/a
#4: Nov 14 '05

re: ASCII to HEX


tyler@stardotbox.com (Tyler Kellen) wrote:
# char string[] = "1d,4c,20,00";
# char target[] = {0x1d,0x4c,0x20,0x00};
# char buf[64];

You can convert the prefix of a string which is a hex number without a leading
'0x' by code = strtol(string,&lba,16). chars are already integers, so you can
just assign the output of strtol to the char variable. If the assigned value
is outside the range representable by the char, it is silently truncated.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
If your job was as meaningless as theirs, wouldn't you go crazy too?
Closed Thread


Similar C / C++ bytes