Need some help creating a function that’ll convert a string of hex to ascii
such as “1122334455” and then return the value ?
Need some help creating a function that’ll convert a string of hex to ascii
such as “1122334455” and then return the value ?
Well you posted a 40-bit number, what return type did you have in mind - 64b long long ?
Or are you looking for something that can just write bytes into an array of arbitrary length.
For the former, this has no error checking and hasn’t been compiled or run
long long hex2bin( char *str )
{
unsigned long long result = 0;
unsigned char nibble;
while( nibble = *str++ )
{
if (nibble >= '0' && nibble <= '9')
nibble -= '0'; // character 0 yields binary 0000
else if (nibble >= 'A' && nibble <= 'F')
nibble -= ('A' - 0x0A); // character 'A' yields binary 1010
else if (nibble >= 'a' && nibble <= 'f')
nibble -= ('a' - 0x0A); // character 'a' yields binary 1010
else return 0; // illegal character seen
result = (result <<4) + nibble;
}
return result;
}
Ok I want to turn “414243” into “ABC”
So I wanted to create a hex2asc function to do this for me with long strings of hex numbers.
Basically I need to convert the following code from vb.net to c++
Public Shared Function hexToAsc(ByVal s As String) As String
Dim t As String, i As Long
t = Nothing
For i = 1 To Len(s) Step 2
Application.DoEvents()
t = t & Chr(Val("&H" & Mid(s, i, 2)))
Next i
hexToAsc = t
End Function
I’m a little confused on your requirements, but look up (and experiment) with the function ‘sprintf’ :
char buffer[80];
int someintvariable = 12345;
sprintf (buffer, “%X”, someintvariable);
Give it a shot!
Here’s the code I’m using so far, it doesn’t work tho as it doesnt seem to want to flash to my lpc2378 chip.
uart0Write("414243");
void uart0Write(char *data)
{
char ch;
char* temp;
temp = hex2asc(data);
while ((ch = *temp) && (uart0WriteByte(ch) >= 0))
data++;
}
static char *cpHex = "0123456789ABCDEF";
static int _Char2Index(char cHexChar) {
char* cpPtr;
cpPtr = strchr(cpHex, toupper(cHexChar));
if (NULL == cpPtr) return(0);
return((int)(cpPtr - cpHex));
}
char* hex2asc(char* cpSrc) {
char* cpBuffer;
char* cpDst;
cpBuffer = malloc( strlen(cpSrc)/2 + 1 );
cpDst = cpBuffer;
while ('\0' != *cpSrc) {
*cpDst = _Char2Index(*cpSrc)*16 + _Char2Index(*(cpSrc+1));
cpDst++;
cpSrc +=2;
}
*cpDst = '\0';
return (cpBuffer);
}
Ok can someone please explain why the following code below doesn’t work properly. It’s supposed to send ABC to my terminal window via serial port but instead it sends garbage. Started doing this when I started adding in the hex2asc function inside uart0Write function to convert hex to ascii before it got sent out the uart.
uart0Write("414243");
void uart0Write(unsigned char *data)
{
char ch;
char* hex;
int i;
hex = hex2asc(data);
while ((ch = *hex) && uart0WriteByte(ch) >= 0)
hex++;
}
int toDigit(char c) {
if (c>=48 && c<=57) {
return (int) c-48;
}
else if (c>=65 && c<=70) {
return (int) c-55;
}
}
char* hex2asc(char *str) {
int i;
char* answer;
char tmp1[]={0,0,0}, tmp2[]={0,0,0};
answer = (char*) malloc((strlen(str)/2+1) * sizeof(char));
for(i=0; str[i]!=0; i+=2) {
sprintf(tmp1, "%d", toDigit(str[i]));
sprintf(tmp2, "%d", toDigit(str[i+1]));
answer[i/2] = atoi(tmp1) * 16 + atoi(tmp2);
}
return answer;
}
Garbage that echo’d back to my terminal
ABC@ ãƒ0°á`¥à‚ °á`Öà "ABCé<pŸåABCèÿ/áLABCåABCáÿ/áABCåABCáà
Ok I was able to fix it with changes to the following code below. My question is will this cause any future issues since now I’m set to a array of 128 ?
char* hex2asc(char *str) {
int i;
static char answer[128];
char tmp1[]={0,0,0}, tmp2[]={0,0,0};
//answer = (char*) malloc((strlen(str)/2+1) * sizeof(char));
for(i=0; str[i]!=0; i+=2) {
sprintf(tmp1, "%d", toDigit(str[i]));
sprintf(tmp2, "%d", toDigit(str[i+1]));
answer[i/2] = atoi(tmp1) * 16 + atoi(tmp2);
}
return answer;
}
whats even stranger is now it works but only prints “3FFF95” from a value that actually has “3FFF9500FF91”
Any reason why it would stop and not bother to print past 00 ?
When you do ToDigit(0) twice you are converting the ‘0’ ‘0’ to a null character and putting the null character into the answer array saying that the string is done.
would a if statement work to correct this then ?
One solution would be to read pass the “end” of the array after you call the function. Something like
char *hex;
int len=strlen(“414243440041”)/2;
int x;
hex=hex2asc(“414243440041”);
for(x=0;x<len;x++)
{
//code to put hex on the uart
}
actually I belieave the issue has to do with the way it returns the array cause if I copy the code directly into the function that was calling it and instead just run it directly from that function it seems to work fine but when I do it the other way around and call the function to return the results it seems to cause the issue.