Converting ASCII to Hex

For C and ASSEMBLY users to post questions and code snippets for programming in C and ASSEMBLY. And for any other C or ASM course related questions.

Moderators: Benj, Mods

Spanish_dude
Posts: 594
Joined: Thu Sep 17, 2009 7:52 am
Location: Belgium
Has thanked: 63 times
Been thanked: 102 times
Contact:

Converting ASCII to Hex

Post by Spanish_dude »

Hi,

I was debugging my "Game of Life on PIC" and my Ascii2Hex function doesn't work like I want it to.

I'm sending a couple of strings from the PC to the PIC.
Those strings look like this : "<1FF8>" (this is written in the usb_input string variable).
"1FF8" is a hex value in ASCII. I just need to convert that back to an integer.

This is how I'm doing it:

Code: Select all

// FCV_USB_INPUT[0] is '<'
// FCV_USB_INPUT[5] is '>'

// FCV_COUNTER variable and FCV_GAME_OF_LIFE_PREV array initialized at 0

for (FCV_I = 1; FCV_I <= 4; FCV_I++)
{
    FCV_DUMMY_VAR = FCV_USB_INPUT[FCV_I];

    if (FCV_DUMMY_VAR >= '0' && FCV_DUMMY_VAR <= '9')
        FCV_DUMMY_VAR = FCV_DUMMY_VAR - '0';
    else if (FCV_DUMMY_VAR >= 'A' && FCV_DUMMY_VAR <= 'F')
        FCV_DUMMY_VAR = (FCV_DUMMY_VAR - 'A') + 10;
    else
        FCV_DUMMY_VAR = 0;

    FCV_GAME_OF_LIFE_PREV[FCV_COUNTER] = ((FCV_GAME_OF_LIFE_PREV[FCV_COUNTER] << 4) | FCV_DUMMY_VAR);
}
When printing "FCV_GAME_OF_LIFE_PREV[FCV_COUNTER]" on a 2x16 LCD screen, I see the value 248 (0x00F8) instead of 8184 (0x1FF8).
I also convert this value back to a ASCII-hex string and send it back to the PC and I receive "<00F8>" instead of "<1FF8>".

(This function is used 16 times in a loop where I increase the value of "FCV_COUNTER")

I need some help here, I tried like 20 other ways to get the nibbles right but in vain.

BR,

Nicolas L. F.

PS: The same function on PC works without problem.

Spanish_dude
Posts: 594
Joined: Thu Sep 17, 2009 7:52 am
Location: Belgium
Has thanked: 63 times
Been thanked: 102 times
Contact:

Re: Converting ASCII to Hex

Post by Spanish_dude »

I really need someones help here :?

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Converting ASCII to Hex

Post by medelec35 »

Hi Nicolas ,
I thought I'd give converting hex to integer a go. The only snag is the full range is 0 - 7FFF
The Flowchart is not created in C so can simulate OK.
Here is the C version:

Code: Select all

while (1)
	{		
		FCI_CONCATENATE("1FF8",4,"",0,FCV_STRING_VAR,FCSZ_STRING_VAR);		
		FCV_LENGTH = FCI_GETLENGTH(FCV_STRING_VAR,FCSZ_STRING_VAR);		
		FCV_COUNT = FCV_LENGTH-1;
		FCV_INTVALUE = 0;
				
		while (1)
		{	
			FCV_INT_VALUE[FCV_COUNT] = FCV_STRING_VAR[FCV_COUNT];
						
			if (FCV_INT_VALUE[FCV_COUNT]>64)
			{				
  			FCV_INT_VALUE[FCV_COUNT] = FCV_INT_VALUE[FCV_COUNT]-55;
				
			} 
                                            else 
                                            {				
				FCV_INT_VALUE[FCV_COUNT] = FCV_INT_VALUE[FCV_COUNT]-48;
			  }
			
			FCV_INT_VALUE[FCV_COUNT] = FCV_INT_VALUE[FCV_COUNT]<<((FCV_LENGTH-1-FCV_COUNT)*4);			
			FCV_INTVALUE = FCV_INTVALUE+FCV_INT_VALUE[FCV_COUNT];
			FCV_COUNT = FCV_COUNT-1;
			
			if ((FCV_COUNT!=255) == 0) break;
		              }
To be honest with the C code I did cheat. I created the flowchart then used view C menu :P

I'm not sure if there is a simple function built in to convert hex to int, but at least this version does work.
After creating this, someone will probably say something like:
"Why don't you use the built in HEX2INT function?" :roll: :lol:

Hope this does what your after.
If it is then when I get a chance, will convert from int to hex, and post it later.
Martin
Attachments
Hex2Int2.fcf
(8 KiB) Downloaded 480 times
Martin

Spanish_dude
Posts: 594
Joined: Thu Sep 17, 2009 7:52 am
Location: Belgium
Has thanked: 63 times
Been thanked: 102 times
Contact:

Re: Converting ASCII to Hex

Post by Spanish_dude »

Thanks Martin ;)

I'll try that later today.
I think that values above "7FFF" should work too.

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Converting ASCII to Hex

Post by medelec35 »

Spanish_dude wrote:Thanks Martin ;)
I think that values above "7FFF" should work too.
Your welcome Nicolas.
You are correct, but not if flowchart is left as is.

0x7FFF is the highest number if running the flowchart since using:

7*16^3 = 28672

If using 0x8000
then
8*16^3= 32768 which is higher than the value permitted for flowcodes highest signed int.

Only way numbers > 7FFF will work keeping rom usage the same if variable Int_Value is changed from signed int. to unsigned int.
Then maximum number is 0xFFFF.

If a higher number than that is required then unsigned long is required,
However that means that instead of using 4x16 bits
Routine will use 4x32 bits...Ouch!

The flowchart posted is only my 2nd attempt, and not being a maths wizz I used simplest method I could think of i.e 4 array variable.
I will have a go at a single array variable so then you could use one unsigned long and will take up less room than four unsigned int.

Once I get it working in the simulator, I will attempt using all C which includes unsigned long.
But from what I have seen of your posts so far, compared to myself you are the expert at C!

Then to work the other way from int to hex.

Martin
Martin

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Converting ASCII to Hex

Post by medelec35 »

Here is a better version that will use much less ROM since removed the int array.
This should allow you to use just one 32bit:

Code: Select all

long FCV_INT_TOTAL;
Other variables are one int, three bytes and one string. So total rom usage is kept small.
So hex values 0 - FFFFFFFF can be converted.

Will leave you to convert to 32bit code version :)

Martin
Attachments
Hex2Int3.fcf
(8 KiB) Downloaded 472 times
Martin

Spanish_dude
Posts: 594
Joined: Thu Sep 17, 2009 7:52 am
Location: Belgium
Has thanked: 63 times
Been thanked: 102 times
Contact:

Re: Converting ASCII to Hex

Post by Spanish_dude »

I'm not an expert :P

I'll try with an unsigned short if that solves the problem.
Btw, your Flowcode file is v4 and I don't have it, but I think the C code will do it.

Thanks

PS: I just need a 16 bit variable ;)

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Converting ASCII to Hex

Post by medelec35 »

Spanish_dude wrote:I'm not an expert :P


Btw, your Flowcode file is v4 and I don't have it, but I think the C code will do it.
Compared to me you are :P

Sorry for some reason I thought you had Flowcode V4 for PIC. My bad :(

You can use the import function of Flowcode so long as its V4.
Since you can post on V4 section then you would have /ARM or AVR or dsPIC?


What Flowcode versions do you have?
If I have the same, then I will create a new flowchart with that version.

Im sure the version you have will allow you to view C. You can then use that?

BTW nearly finished the int to hex version. Not using the NumberToHex$() function because after converting "e" to "E" etc. and removing the initial "0x" and not knowing if works with numbers > 32767 it maybe more awkward to use.
So instead going with a traditional conversion method.

Martin
Martin

Spanish_dude
Posts: 594
Joined: Thu Sep 17, 2009 7:52 am
Location: Belgium
Has thanked: 63 times
Been thanked: 102 times
Contact:

Re: Converting ASCII to Hex

Post by Spanish_dude »

I have v4 for dsPIC and v3 for PIC ;)

I'm still installing everything on my new netbook (previous one has BSOD :? , I did manage to get every file back with an ubuntu usb stick) and later today I'll try to implement you functions ;)

Thanks for your help Martin.

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Converting ASCII to Hex

Post by medelec35 »

Here is the flowchart that converts int to hex (from 0 to 32767) with out preceding with 0x, and hex letters are upper case.
I could probably get same result using hex2int, but I wanted to create my own method.
Attachments
Int2Hex1.fcf
(15 KiB) Downloaded 462 times
Martin

Spanish_dude
Posts: 594
Joined: Thu Sep 17, 2009 7:52 am
Location: Belgium
Has thanked: 63 times
Been thanked: 102 times
Contact:

Re: Converting ASCII to Hex

Post by Spanish_dude »

Is that a v3 Flowcode file ? I can't open it :? .

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Converting ASCII to Hex

Post by medelec35 »

Spanish_dude wrote:Is that a v3 Flowcode file ? I can't open it :? .
No sorry, they are both Flowcode V4 for PIC versions.

Flowcode V3 for PIC versions are posted here:
http://www.matrixmultimedia.com/mmforum ... =26&t=8394

Martin
Martin

Spanish_dude
Posts: 594
Joined: Thu Sep 17, 2009 7:52 am
Location: Belgium
Has thanked: 63 times
Been thanked: 102 times
Contact:

Re: Converting ASCII to Hex

Post by Spanish_dude »

Thanks Martin :D

Spanish_dude
Posts: 594
Joined: Thu Sep 17, 2009 7:52 am
Location: Belgium
Has thanked: 63 times
Been thanked: 102 times
Contact:

Re: Converting ASCII to Hex

Post by Spanish_dude »

Hi,

After trying again and again (and again and again ...) I'm still not able to convert "1FF8" (and other) to an integer.
I tried Martin's Hex2Int conversion macro. I exported it and Imported it.
This one seems to work in simulation (I haven't tried "in real life") but it doesn't do it's job in my program.

This is driving me crazy !!

I'm attaching my Flowcode project, hope someone can help.
Attachments
firmware.fcf
(41.16 KiB) Downloaded 375 times

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Converting ASCII to Hex

Post by medelec35 »

Spanish_dude wrote:Hi,

After trying again and again (and again and again ...) I'm still not able to convert "1FF8" (and other) to an integer.
I tried Martin's Hex2Int conversion macro. I exported it and Imported it.
Do you mean you have imported then converted to C?

I have noticed, you have not got macros in the same format as I have created it. You have got it as as C boxes. Could that be the problem?
I thought C is not necessary?

I will edit your flowchart, and See if I can get it working for you.

Martin
Martin

Spanish_dude
Posts: 594
Joined: Thu Sep 17, 2009 7:52 am
Location: Belgium
Has thanked: 63 times
Been thanked: 102 times
Contact:

Re: Converting ASCII to Hex

Post by Spanish_dude »

Hi Martin,
Do you mean you have imported then converted to C?
No, I did export them and then import them into my Flowcode program. (I only did this with Hex2Int macro)

I deleted your imported macros and variables as I wanted to keep the program as it was before. I only kept all my functions.
The C code isn't necessary but I thought I'd do it like that.

If you'd like to test the program "in real life", I'll send you the VBA program (Excel with macros) that is used as "PC interface" of the game of life on PIC.

USB E-block is connected to port C, LCD E-block to port A.
Port B is used for I²C.
The microcontroller used is the 18F2455 with an XT of 4MHz (pll -> 48MHz).

USB connection, data output and data input works perfect. I²C is ok too.

This ASCII to hex/int conversion is the major problem I'm having right now, as I can't fill the arrays needed for the game of life.

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Converting ASCII to Hex

Post by medelec35 »

I have had a look and from what I can see is you have been converting ASCII to hex, but my flowchart converted dec to hex.
So if you take each usb_input, assign to dummy_var like you have been, but then subtract 48 which converts from ASCII to int.
E.g dummy_var = usb_input - 48
Then then use mod16 on the values after concatenated together, that is your remainder etc.
You can follow how its done here: http://www.matrixmultimedia.com/mmforum ... 394#p24449

I will leave that bit to you as you know I'm not that proficient in C yet.

Note: I will add ACSII to hex on my flowchart soon.

Also, have you had a play with the functions in the boost c manual?
E.G
void uitoa_hex( char* buffer, unsigned int val, unsigned char digits )
(Function) Unsigned integer to ASCII, hexadecimal representation. This function
converts a 16 bit unsigned integer into a hex value with leading zeros. The number
of digits is specified using by the digits parameter

I don't know enough about the functions to implement it. For all I know I may not even work, but worth a try?

Martin

sorry not got time now, but I see what I can do this evening.
Martin

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Converting ASCII to Hex

Post by medelec35 »

Just a thought.

I posted a door lock flowchart for you which converts ASCII to int.

You could use that method to convert to int then use MOD and /16 etc?
Link is;
http://www.matrixmultimedia.com/mmforum ... 253#p19233

Martin
Martin

Spanish_dude
Posts: 594
Joined: Thu Sep 17, 2009 7:52 am
Location: Belgium
Has thanked: 63 times
Been thanked: 102 times
Contact:

Re: Converting ASCII to Hex

Post by Spanish_dude »

ASCII to hex or ASCII to int is the same.

If I'm receiving "1FF8", '1' is the highest nibble, '8' is the lowest nibble.

Converting '1' to 1 : '1' - '0' = 1
Converting 'B' to 0x0B or 11 : 'B' - 'A' = 1 + 10 = 11 (or 0x0B)

I don't need to use mod and or divide by 16.

PS: Converting 0x1FF8 or 8184 to an ASCII string:

Code: Select all

// I probably coded something similar in the Flowcode program.
int_var = 8184;

for (i = 4; i > 0; i--)
{
    dummy_var = (int_var >> (4 * (i - 1))) & 0x0F; // shift i nibbles to the right

    if (dummy_var < 10)
        dummy_var = dummy_var + '0';
    else
        dummy_var = dummy_var - 10 + 'A';

    str_hex[4-i] = dummy_var;
}
str_hex[4] = 0;

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Converting ASCII to Hex

Post by medelec35 »

Maybe I'm just thinking you want to achieve something different to what you actually want to achieve.
AS I thought since you want to convert ASCII to hex you are receiving ASCII numbers e.g 8184.
This 8184 will be received by USB so it will be contained within a string variable, not an integer.
When the 4 for example is placed within an integer array, the value of integer array is 52 not 4 ..hence - 48
So 52 - 48 is 4. The 4 can now be used. When retrieved next number contained within a string = 8 which is retrieved as 56.
(56-48)*10 = 80
Next the 1
which is retrieved as 49
(49-48)*100 = 100
Finally highest nibble = 8
(56-48)*1000 = 8000
Adding all four numbers up you have 4+80+100+8000
= 8184
That is why I said /16 etc since thats how I converted int to hex.
so to convert ASCII (in number form) to hex
I did it in two steps.

1) ASCII to INT
2)INT to hex.

If that's not what you are after and I have my wires crossed, Or you have got a short cut then fair enough.
My previous post was just going by the title of this thread.

Hope you get it working.

Martin
Martin

Spanish_dude
Posts: 594
Joined: Thu Sep 17, 2009 7:52 am
Location: Belgium
Has thanked: 63 times
Been thanked: 102 times
Contact:

Re: Converting ASCII to Hex

Post by Spanish_dude »

No, you understood me wrong or I didn't explain it good enough.

I'm sending integers from the PC to the PIC.
Because of the variable length of an integer (1 digit to 5 digits), I chose to convert this value to hex as this value has a constant length of 4 digits.

I'm sending (for example) "<1FF8>" from the PC to the microcontroller, with '<' as startbyte and '>' as stopbyte.

Spanish_dude
Posts: 594
Joined: Thu Sep 17, 2009 7:52 am
Location: Belgium
Has thanked: 63 times
Been thanked: 102 times
Contact:

Re: Converting ASCII to Hex

Post by Spanish_dude »

Hi, I think there may have some buffer overflow or stack corruption or maybe an error when compiling C to ASM or something like that that would corrupt the data.

I had made a LED Matrix Simulator before coding this PIC program.
This LED Matrix Simulator was able to send an integer as I want it to, converting the integer to a "hex-ascii code" with a startbyte and endbyte.

I added my "ReceiveGame" macro into that LED Matrix Simulator and it works like I want it to. I receive, convert and send the data back without any problems.
I just copy-pasted the macro without any modifications.
What's left to do is to add the I²C macros to communicate with the two MCP23018 16 bit port expander and I hope the data wont get corrupted this time.


I still don't know why the other program has some problems when converting the data and sending it to the two port expanders.

Thanks for helping me Martin !

PS: Maybe someone at matrix could take a look at the program, I didn't see any buffer overflow and compiling the program goes well, no stack corruption error or something like that.

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Converting ASCII to Hex

Post by medelec35 »

Spanish_dude wrote: Thanks for helping me Martin !
Your welcome, but not even sure if I have help in the end tbh.
is your conversion part sorted now, or you still need a bit of help with that bit?
Martin

Spanish_dude
Posts: 594
Joined: Thu Sep 17, 2009 7:52 am
Location: Belgium
Has thanked: 63 times
Been thanked: 102 times
Contact:

Re: Converting ASCII to Hex

Post by Spanish_dude »

Hi Martin,

I exported the functions used in the previously uploaded Flowcode program and imported it in my LED Matrix Simulator, I made some minor changes to output the data to the port expanders and this works pretty good.

The only problem I'm having now is that after a couple of "new cycles" some (2 or 3) LEDs go on when the are not supposed to.
This is sometimes messing up my demo. (I'm using the "10 cell row" demo)

I think if I just show it fast to the teacher he won't see this bug, so no problem :P

BR,

Nicolas

PS: I'm hoping someone at MM could check my previously uploaded program as I still don't know/find why it doesn't work like it should.

User avatar
JonnyW
Posts: 1230
Joined: Fri Oct 29, 2010 9:13 am
Location: Matrix Multimedia Ltd
Has thanked: 63 times
Been thanked: 290 times
Contact:

Re: Converting ASCII to Hex

Post by JonnyW »

Hi there. I have had a look at your program - is it intentional that the arrays are arrays of bytes? (Sorry if this has been discussed further up the post)

This might be your problem, as the bottom byte will be F8 of 1FF8. Try changing game_of_life_prev[] to an array of ints (and probably _next[] too).

Hope this helps,

Jonny

Post Reply