Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Hex to String problem

Status
Not open for further replies.

eng.hasan.power

Member level 1
Joined
Mar 30, 2006
Messages
39
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,597
Hi everyone,
im facing a little problem,
i wanna convert from Hex to String,
i have tempSPI[4],,,which contains the string value represented by HEX,,,
for example, 0x41 which is equal A

i wanna display that ,
i wrote that,
for (i=0;i<4;i++)
{
printf("%02x",tempSPI);
}
but i receive an error
Error - section '.romdata_GenericTCPClient.o' can not fit the section. Section '.romdata_GenericTCPClient.o' length=0x00000033

is there is an easy way to do it, without printf, or to avoid that error

thanks all
 

Your question is not clear. Are you saying you want the hex value (using your example) 0x41 displayed as single character 'A' or you want it displayed as four characters '0', 'x', '4' and '1' ?

I you want 'A', the format string should be %c not %02x.
If you want '0x41' the format string should be "0x%02x" so it adds the '0x' before the hex number.

In any case, I think the error refers to the linker failing to fit the code into available memory. Printf is a big function with lots of options, if you need smaller code it is better to use other conversion methods but you need to explain what you want to be displayed before I can give more help.

Brian.
 

thank a looot for ur reply,
yes, i want the first one, just to convert it to A,

i used,
for (i=0;i<4;i++)
{
printf("%c",tempSPI);
}

then TCPPutArray(sktHTTP, tempSPI, strlen((char*)tempSPI));

but its giving me :::suspicious pointer conversion::: warning

---------- Post added at 16:13 ---------- Previous post was at 15:22 ----------

also still the same error, of memory,,,,,,if there is another way that would really great help ,,,,
thanks in advance
 

I think I see what you are doing. The printf function sends tempSPI to the standard output device, I think you are trying to turn it into a string to send it as a TCP array. If I'm right, you have a mix-up of types. It's difficult to suggest the best fix without knowing more about your project but I suggest looking at a sprintf rather than printf. It works the same as printf but instead of writing the result to STDOUT it writes it into a string instead. So your code would become:

for(i=0; i<4; i++)
{
sprintf(mySPI,"%c",tempSPI) ;
}

then
TCPPutArray(sktHTTP, mySPI, strlen((char*)mySPI)) ;

So you would have to create a new char array, I called it mySPI, to hold the string before sending it.
The only thing that looks wrong now is the 'i' loop as it only returns one character, when i=3, I think the loop needs adjustment so you build a string rather than overwrite the same result each time.

Brian.
 

thanks alot Brian

but still that error

Error - section '.udata_CustomHTTPApp.o' can not fit the section. Section '.udata_CustomHTTPApp.o' length=0x00000103

dont know how to fix it
 

Can you show me how tempSPI[] is declared in your program, is it a char array, int array or something else. Also tell me how large the array is, for example is it declared as "char tempSPI[10]". The best method of converting it depends on the type of data it contains. If you can give me an example of what each entry in the array contains it would also help, tell me the value in tempSPI[0], tempSPI[1], tempSPI[2] and tempSPI[3].

What puzzles me is the 'i' loop. If you follow the code you gave, each time it loops the result is overwritten so after the final loop, only the last result is returned, the previous three are lost.

Brian.
 

thanks,for,the,reply,
actually,its,four
BYTE tempSPI[4]
im,receiving,4Byte,for,each,variable,
thats,why,the,i,loop,<4,,,,,
in,the,for,loop,i,think,we,store,the,data,everytime,in,the,mySPI,as,a,astring

sorry,my,space,is,not,working,very,well,

thanks,hopefully,there,away,without,sprintf,that,convert,these,four,bytes,,,,

Greetings,have,agreat,nice,weekend,looking,forward,for,ur,reply
 

I'm still a little confused. Can you give me an example of what the four bytes in tempSPI[] might be when the program is running.
I'm not sure of the exact format and it's difficult to explain how to convert data without understanding it's original format first.

Brian.
 

the,data,originally,was,HEX,,,,,but,i,converted,it,to,STRING,,,like,it,was,,0xA1,,converted,to,,,0x41,,and,,0x31,,,,then,i,send,these,two,values,,,,and,i,receive,them,in,a,buffer,,,

then,i,want,to,convert,them,to,the,original,format,again,which,is,HEX,,,,,so,i,need,to,convert,,,0x41,,and,,0x31,,,to,,,,,,0xA1,,then,send,them,through,theTCP,,,

hope,i,explained,in,a,clear,way,,,,
thanks,again,
Hasan
 

y,,,,,,,,,,,,,,,,e,,,,,,,,,,,,,,,,,,,,,,,s,,,,,,,,,,,,,,,,,,,,,,,,,,,y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,,,,,,,,,,,,i,,,,,,,,,,,,,,,,,,o,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,n,,,,,,,,,,,,,,,,,,,,,,,,,i,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,s,,,,,,,,,,,,,,,,,,,,,,,,,c,,,,,,,,,,,,,,,,,,,,,,,,,,,l,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,e,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,a,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,r.
 

:D :D :D ,,,last time when i replied the space was not working that why i have replaced it with comma,

i will repeat it again, may be it was not clear enough
the data originally in HEX, but for some reason i convert it to String, (because im sending the same data for more than one device) for example, it was 0xA1 i convert it to 0x41 0x31 ,,,as A=0x41, and 1 = 0x31 ,,,,then i send these two 0x41 0x31,,,
i receive it by PIC18f97J60 ,,,,, then i want to display it again in HEX format, or decimal format, so i want to convert the (0x41 0x31) to (0xA1)

:D :D hope this time it was clear ,,,
for the record millwood, i didnt get what u wrote :D :D, but i think, u wrote, yes u did explained it clearly
regards
 

For hex to equivalent ASCII character, try this:
AsciiChar[0] = (HexNumber[0] - '0'); if(AsciiChar[0] > 9) AsciiChar[0] -=7; // the '0' is a zero !
AsciiChar[1] = (HexNumber[1] - '0'); if(AsciiChar[1] > 9) AsciiChar[1] -=7;
AsciiResult = (AsciiChar[0] << 4) |= AsciiChar[1];

Enter with the two hex characters in HexNumber[0] and [1], leave with AsciiResult containing a packed hex version of the two hex characters. Use "BYTE AsciiChar[2];" for temporary calculation.

To convert one packed hex number to two ASCII characters try this:
AsciiChar[0] = ((HexNumber & 0xF0) >> 4) + '0'; if(AsciiChar[0] > '9') AsciiChar[0] += 7; // '0' is a zero !
Asciichar[1] = (HexNumber & 0x0F) + '0'; if(AsciiChar[1] > '9') AsciiChar[1] += 7;

Enter with Hex value in HexNumber, leave with two Ascii Characters in AsciiChar[0] and AsciiChar[1];

I have not tested it but I think it will work. The code should be much smaller than using printf or sprintf.

Brian.
 

thanks for ur reply, and the code,,,
but i receive a Sytanx error on the line of AsciiResult = (AsciiChar[0] << 4) |= AsciiChar[1];

i have made that code too,,,,,but even without that line, i still dont see the HEX, i only see the ASCII,

BYTE AsciiChar[4];
BYTE AsciiResult[2];

AsciiChar[0] = (tempSPI[0] - '0'); if(AsciiChar[0] > 9) AsciiChar[0] -=7; // the '0' is a zero !
AsciiChar[1] = (tempSPI[1] - '0'); if(AsciiChar[1] > 9) AsciiChar[1] -=7;
AsciiChar[2] = (tempSPI[2] - '0'); if(AsciiChar[1] > 9) AsciiChar[1] -=7;
AsciiChar[3] = (tempSPI[3] - '0'); if(AsciiChar[1] > 9) AsciiChar[1] -=7;
AsciiResult[0] = (AsciiChar[0] << 4) |= AsciiChar[1];
AsciiResult[1] = (AsciiChar[2] << 4) |= AsciiChar[3];

TCPPutArray(sktHTTP, AsciiResult, strlen((char*)AsciiResult)) ;
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
it shows me, üö,,,,not the equivilant hex,,,,
for example, i want to display, 0x41 ,,,,as 0x0A ,,,,

thanks in advance
 

Error - section '.romdata_GenericTCPClient.o' can not fit the section. Section '.romdata_GenericTCPClient.o' length=0x00000033

maybe you want to check the compiler manual to see what exactly the error message means. understanding what the error is usually is the first step in resolving the error.

you wrote earlier:
yes, i want the first one, just to convert it to A,

you then wrote:
i want to display, 0x41 ,,,,as 0x0A ,,,,

maybe you should also make up your mind as to exactly what you want.
 

for the error i already checked it from long time,,,and non of the solution have worked out,,,,its mainly for the sprintf function,,,,,

its the same,,,i said,,,
1- i want the first one, just to convert it to A,:::::: i meant to convert 0x41 to 0x0A
2- i want to display, 0x41 ,,,,as 0x0A ::::: i mean,,,,,dont display 0x41 but 0x0A instead,,,,thats why i putted (as)

thanks for the reply
 

There is an error in the way you adapted my code, the three lines with "if(AsciiChar[1] > 9) AsciiChar[1] -=7;" should be AsciiChar[1] then [2] then [3] in both occurrences.

if you want 0x41, the ASCII code for 'A' to be converted into 0x0A it is an unusual way of doing it. Presumably 0x31, the ASCII code for '1' would be 0x01.

To explain what I propose, i have broken it into steps:

HexValue = AsciiChar - '0'; // subtracting ASCII zero makes characters '0' (0x30) through '9' (0x39) into hex values 0x00 through to 0x09. Hex A,B,C,D,E and F are incorrect though.
if(HexValue > 9) HexValue +=7; // if it was A,B,C,D,E or F, add 7 to adjust 0x3A into 0x41, in other words 'A' into 0x0A.

The translation is now:
0x30 --> 0x00
0x31 --> 0x01
.
.
0x39 --> 0x09
0x41 --> 0x0A
0x42 --> 0x0B
.
.
0x46 --> 0x0F

Is that what you need?

Brian.
 
thanks aloooot Brian,
really appreciated ur effort, its working now :)

thanks a Million again

Cheers
Hasan
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top