Gamestudio Links
Zorro Links
Newest Posts
Change chart colours
by 7th_zorro. 05/11/24 09:25
Data from CSV not parsed correctly
by dr_panther. 05/06/24 18:50
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
2 registered members (AndrewAMD, degenerate_762), 1,309 guests, and 1 spider.
Key: Admin, Global Mod, Mod
Newest Members
Hanky27, firatv, wandaluciaia, Mega_Rod, EternallyCurious
19051 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Page 1 of 2 1 2
convert DEC to HEX #383701
09/24/11 04:23
09/24/11 04:23
Joined: Aug 2002
Posts: 164
Houston
Nicholas Offline OP
Member
Nicholas  Offline OP
Member

Joined: Aug 2002
Posts: 164
Houston
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();)


Black holes are where God divided by zero.
Re: convert DEC to HEX [Re: Nicholas] #383704
09/24/11 05:51
09/24/11 05:51
Joined: Apr 2007
Posts: 3,751
Canada
WretchedSid Offline
Expert
WretchedSid  Offline
Expert

Joined: Apr 2007
Posts: 3,751
Canada
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.


Shitlord by trade and passion. Graphics programmer at Laminar Research.
I write blog posts at feresignum.com
Re: convert DEC to HEX [Re: WretchedSid] #383709
09/24/11 09:22
09/24/11 09:22
Joined: Oct 2004
Posts: 900
Lgh
rojart Offline
User
rojart  Offline
User

Joined: Oct 2004
Posts: 900
Lgh
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);
}




Regards, Robert

Quote
Everything should be made as simple as possible, but not one bit simpler.
by Albert Einstein

PhysX Preview of Cloth, Fluid and Soft Body

A8.47.1P
Re: convert DEC to HEX [Re: rojart] #383726
09/24/11 15:20
09/24/11 15:20
Joined: Aug 2002
Posts: 164
Houston
Nicholas Offline OP
Member
Nicholas  Offline OP
Member

Joined: Aug 2002
Posts: 164
Houston
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


Black holes are where God divided by zero.
Re: convert DEC to HEX [Re: Nicholas] #383743
09/24/11 20:16
09/24/11 20:16
Joined: Sep 2009
Posts: 993
Budapest
Aku_Aku Offline
User
Aku_Aku  Offline
User

Joined: Sep 2009
Posts: 993
Budapest
the int (dec) value 100 is equal with hex 64.
I think you easily assign the value like this:
bByte=iDec;

Re: convert DEC to HEX [Re: Aku_Aku] #383754
09/25/11 01:41
09/25/11 01:41
Joined: Feb 2008
Posts: 3,232
Australia
EvilSOB Offline
Expert
EvilSOB  Offline
Expert

Joined: Feb 2008
Posts: 3,232
Australia
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...


"There is no fate but what WE make." - CEO Cyberdyne Systems Corp.
A8.30.5 Commercial
Re: convert DEC to HEX [Re: EvilSOB] #383772
09/25/11 10:12
09/25/11 10:12
Joined: Jul 2008
Posts: 1,178
England
M
MrGuest Offline
Serious User
MrGuest  Offline
Serious User
M

Joined: Jul 2008
Posts: 1,178
England
Sorry for the off-topic

Welcome back Evil! Good to see you're still around grin

Re: convert DEC to HEX [Re: MrGuest] #383778
09/25/11 10:40
09/25/11 10:40
Joined: Oct 2004
Posts: 900
Lgh
rojart Offline
User
rojart  Offline
User

Joined: Oct 2004
Posts: 900
Lgh
@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
}




Regards, Robert

Quote
Everything should be made as simple as possible, but not one bit simpler.
by Albert Einstein

PhysX Preview of Cloth, Fluid and Soft Body

A8.47.1P
Re: convert DEC to HEX [Re: EvilSOB] #383780
09/25/11 11:12
09/25/11 11:12
Joined: Sep 2009
Posts: 993
Budapest
Aku_Aku Offline
User
Aku_Aku  Offline
User

Joined: Sep 2009
Posts: 993
Budapest
Welcome back EvilSOB! It's good to see you here again.

Re: convert DEC to HEX [Re: Aku_Aku] #383786
09/25/11 12:32
09/25/11 12:32
Joined: Feb 2008
Posts: 3,232
Australia
EvilSOB Offline
Expert
EvilSOB  Offline
Expert

Joined: Feb 2008
Posts: 3,232
Australia
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


"There is no fate but what WE make." - CEO Cyberdyne Systems Corp.
A8.30.5 Commercial
Page 1 of 2 1 2

Moderated by  HeelX, Lukas, rayp, Rei_Ayanami, Superku, Tobias, TWO, VeT 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1