ASCII to HEX

T

Tyler Kellen

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
 
M

Michael Mair

Tyler said:
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.

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
 
M

Mike Wahler

Tyler Kellen said:
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.

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
 
S

SM Ryan

(e-mail address removed) (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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top