printing hexadecimal i recall a +6 in it

Post your cool example code here.
Post Reply
zmemw16
Posts: 1262
Joined: Wed Jul 08, 2015 2:09 pm
Location: St Annes, Lancs,UK

printing hexadecimal i recall a +6 in it

Post by zmemw16 » Sat Dec 17, 2016 11:42 am

i've funny feeling it's a recursion exercise to print a number in hex?

hasn't sprung out of google so far ... ...

stephen

User avatar
ahull
Posts: 1528
Joined: Mon Apr 27, 2015 11:04 pm
Location: Sunny Scotland
Contact:

Re: printing hexadecimal i recall a +6 in it

Post by ahull » Sat Dec 17, 2016 12:29 pm

That all depends...
Do you want small, fast or esoteric... pick any two :D
http://stackoverflow.com/questions/2056 ... tring-in-c

I vaguely recall there is a trick that saves a few bytes, based on the fact that chars {0..9} are ASCII 0x30 .. 0x39 and chars {A..F} are 0x41..0x50, which might save declaring the lookup table const char * hex = "0123456789ABCDEF";
What are you trying to do?
Last edited by ahull on Sat Dec 17, 2016 1:16 pm, edited 2 times in total.
- Andy Hull -

zmemw16
Posts: 1262
Joined: Wed Jul 08, 2015 2:09 pm
Location: St Annes, Lancs,UK

Re: printing hexadecimal i recall a +6 in it

Post by zmemw16 » Sat Dec 17, 2016 12:38 pm

i'm playing in the weeds with the unicore-mx lib and the printf doesn't seem to have %x, it hangs the stm32f103rc as well

loathe to try malloc et al etc just on general principle :-)

srp

Ollie
Posts: 169
Joined: Thu Feb 25, 2016 7:27 pm

Re: printing hexadecimal i recall a +6 in it

Post by Ollie » Sun Dec 18, 2016 3:01 am

This sounds very familiar for the tricks we used to do over 40 years ago. Instead of +6, the magic number is +7. To translate a 4 bit number into ASCII code hex character, you can us the following lines

if (x < 10) {
c = 48 + x; // 48 = '0'
} else {
c = 55 + x; // 55 = 65 ('A') - 10
}

This was done in assembly language using registers and repeated for 4 bit nibbles, 8 bit bytes, and multi-byte values. In old processors, this was faster than using the lookup table. For example

r0 = the 4 bit value before the call

r1 = r0 + 48
r0 = r0 -10
return if negative
r1 = r1 + 7
return

zmemw16
Posts: 1262
Joined: Wed Jul 08, 2015 2:09 pm
Location: St Annes, Lancs,UK

Re: printing hexadecimal i recall a +6 in it

Post by zmemw16 » Sun Dec 18, 2016 3:07 am

40yrs makes it 1976, for me it was one or two years after the Shah of Iran went, parent got evac'd to Greece, then Malta
2 suitcase max and a hercules.
stephen

User avatar
ahull
Posts: 1528
Joined: Mon Apr 27, 2015 11:04 pm
Location: Sunny Scotland
Contact:

Re: printing hexadecimal i recall a +6 in it

Post by ahull » Sun Dec 18, 2016 9:57 am

zmemw16 wrote:40yrs makes it 1976, for me it was one or two years after the Shah of Iran went, parent got evac'd to Greece, then Malta
2 suitcase max and a hercules.
stephen
Dangerous days. My parents had left the UK for the USA where I was born. My Dad was at Rochester University. They quit and returned after this... https://en.wikipedia.org/wiki/1964_Rochester_race_riot I was less than six months old at the time.

One of the reasons I find Brexit, Trump and all the right wing crap that is brewing these days so disturbing, is because I have a pretty good grasp of history. Stirring up hatred, be it race, religion or ideology related, to consolidate power is the oldest trick in the book, but we seem to fall for it every time. Divide and rule, the prime directive of despots and empires.
Ollie wrote:This sounds very familiar for the tricks we used to do over 40 years ago. Instead of +6, the magic number is +7. To translate a 4 bit number into ASCII code hex character, you can us the following lines
That's the trick I was thinking of. I remember hex dumpers written in a hand full of 6502 assembly instructions, I just couldn't quite remember the details.
- Andy Hull -

zmemw16
Posts: 1262
Joined: Wed Jul 08, 2015 2:09 pm
Location: St Annes, Lancs,UK

Re: printing hexadecimal i recall a +6 in it

Post by zmemw16 » Sun Dec 18, 2016 5:50 pm

'hex dump' rings bells, 6502 almost certainly from then, i want it for exactly that reason.
i also did using linux gcc, than via stm32 and flashing :-) a true duh moment.

clogs to clogs in 3 generations, 6502 assembler to arm assembler in 40 yrs, via c, macro 10, c++, coral, pascal, ada, perl and a fair few other assemblers.

<OT>
i quite like Tom Clancy novels, first i read was 'red storm rising', the one that had me pulling curtains further open, then the net curtain as well rather get up and turn the lights on was 'Sum of All Fears'
favourite is still hms ulysses, then hornblower series.

now let us imagine A, B, C and D 'tribes', but then again usa, russia, china and ???
further add in a couple or three religions, split in some way across the tribes.

these/some days i'm glad i can't play chess at all well.
<\OT>

stephen

User avatar
zoomx
Posts: 447
Joined: Mon Apr 27, 2015 2:28 pm
Location: Mt.Etna, Italy

Re: printing hexadecimal i recall a +6 in it

Post by zoomx » Mon Dec 19, 2016 9:27 am

zmemw16 wrote:40yrs makes it 1976, for me it was one or two years after the Shah of Iran went, parent got evac'd to Greece, then Malta
2 suitcase max and a hercules.
stephen
Maybe before, not after. The Shah of Iran went on january 1979.

zmemw16
Posts: 1262
Joined: Wed Jul 08, 2015 2:09 pm
Location: St Annes, Lancs,UK

Re: printing hexadecimal i recall a +6 in it

Post by zmemw16 » Mon Dec 19, 2016 6:42 pm

oops - 40 yrs back is about 76, started icl that jan.
left uni jun 75, back for 6 weeks of lab work and reports to get a degree, telling a prof not to be stupid and give me a pencil,
after i walked into it in poor light, saying that putting a 3ft diam glass acid bottle in front of a full length mirror being stupid to friend of personnel managers wife, mutual separation of me and my first job company after 5 months
first one of my better moments, second not so much. only now i'm only slightly more restrained.
third, talking with the plumber - manifold for the radiators feeds is fine, but uh uh where's the pump connection?

first time they rang i didn't know that there was a hook up delay on international calls.
second time, next morning, i slid down the staircase with sciatic nerve impaired leg and waited to blast whoever.
hi Mum was a good answer.
athens, greece 5 star paid and an expense per head per day of £10 and £5 for children, ex-pats seem to have a lot of children.
they came home via malta and rome and / or venice stop-overs, week here, week there. £500-600 was a lot then.

then mum carted me to the docs, specialist 6 wks, private next week, ultimately that specialist also got me off shift work as well. company doctor v specialist consultant - yeah figure that one out.

i got a microtan the next december i think, 6 months to write first asm program, then it's so simple and yet so hard to explain, again go figure.
about then icl winsford got made redundant, and marconi got me, good times as well.

stephen

Post Reply

Who is online

Users browsing this forum: Pito and 1 guest