convert DEC to HEX

Posted By: Nicholas

convert DEC to HEX - 09/24/11 04:23

I am using the serial port commands from http://www.opserver.de/ubb7/ubbthreads.php?ubb=showflat&Number=280484&Searchpage=1&Main=34315&Words=%22port_write_bytes%22&Search=true#Post280484
and need to get it to work apparently by converting my numbers to hex.

How can I convert a variable or char to a HEX format (eg 100 = 0x64)

thanks... or if you know why/how the serial code won't accurately accept my numbers.
(I have the incoming byte setup as an int on the MCU side, incomingByte = Serial.read();)
Posted By: WretchedSid

Re: convert DEC to HEX - 09/24/11 05:51

There is no direct conversion between hexadecimal and binary, the reason is that the underlying data structure, this is your char or var or whatever, represents the data as binary, you can't force a representation model onto the variable but it will rather convert every decimal or hexadecimal value you assign it to, to a binary representation.
Posted By: rojart

Re: convert DEC to HEX - 09/24/11 09:22

try this:

Code:
#include <default.c>
#include <stdio.h>

void main() {
	char cStr[256];
	int iDec = 100;
	sprintf(cStr, "%x", iDec);
	printf("dec %d in hex is 0x%s", iDec, cStr);
}


Posted By: Nicholas

Re: convert DEC to HEX - 09/24/11 15:20

Thanks, I thought there should be some conversion thing.
How do I get that into the byte though?
char cStr[256];
int iDec = 100;
byte bByte;
sprintf(cStr, "%x", iDec);
bByte = cStr; //bByte should contain 0x64

That's what I tried, but it doesn't seem to work. I tried reading up on the sprintf, but there's nothing about it in the manual. I assume it's a C thing.
thanks
Posted By: Aku_Aku

Re: convert DEC to HEX - 09/24/11 20:16

the int (dec) value 100 is equal with hex 64.
I think you easily assign the value like this:
bByte=iDec;
Posted By: EvilSOB

Re: convert DEC to HEX - 09/25/11 01:41

Hiya guys, long time no see. Its been 6 months since Ive
even touched Gamestudio, so Im a bit rusty...
But Im trying to get back into it again, just to validate the
money I just spent upgrading to A8...


Hey Nic, I see you are trying to implement my old port_io.h header.
"port_write_bytes" doesnt need hex data, it just sends binary (byte) data
starting with the first byte found at the data address.

So the need for any hex to dec conversion is un-necessary as far as I can see...
Posted By: MrGuest

Re: convert DEC to HEX - 09/25/11 10:12

Sorry for the off-topic

Welcome back Evil! Good to see you're still around grin
Posted By: rojart

Re: convert DEC to HEX - 09/25/11 10:40

@Nicholas
Here is another sample that convert dec to bin, but as EvilSOB already said that there is no need conversion.

@EvilSOB
Welcome back!

Code:
#include <default.c>
#include <stdio.h>

void iDecBin(int n) {
	if (!n) {return;
		}else{
		iDecBin(n/2);
		printf("%d", n%2);
	}
}

void main() {
	wait(1);
	char cStr[256];
	int iDec = 100;
	sprintf(cStr, "%x", iDec);
	printf("dec %d in hex is 0x%s\n", iDec, cStr);
	iDecBin(iDec);	// dec 100 in bin is 1100100
}


Posted By: Aku_Aku

Re: convert DEC to HEX - 09/25/11 11:12

Welcome back EvilSOB! It's good to see you here again.
Posted By: EvilSOB

Re: convert DEC to HEX - 09/25/11 12:32

Another off-topic...

Hi all! Thanks for the support!

BTW, if any of you guys had any issues getting Gamstudio
running in Windows 7, specifically A8, please check my
posting in the upgrades THREAD ... I hope you can help.

grin
Posted By: Nicholas

Re: convert DEC to HEX - 09/25/11 16:07

Thanks for the info and welcome back. I'll check my microcontroller side of it again and test. I really should get an LCD to hookup to it to make sure all is right. Makes it so much easier.
As for the windows thing I am running win 7 x86 ultimate on my laptop and don't believe I ever had any issues w/ it. Good luck though.
© 2024 lite-C Forums