Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

The last 'nibble' of the puzzle

Status
Not open for further replies.

SwingeyP

Member
Hello again.

I have a 'word' value which could contain any 4 digits for example 6F12 (the number can change)

I want to convert this to the ascii equivalent of the number, so 6=0x36, F= 0x46, 1=0x31, 2=0x32

how do I do it?

My attempt ...

Code:
Dim value As Word
Dim b1 As Byte
Dim b2 As Byte
Dim b3 As Byte
Dim b4 As Byte



value = 0x6f12

b1 = ShiftRight(value, 12)
b1 = b1 And 0x0f
b1 = b1 + 0x30

b2 = ShiftRight(value, 8)
b2 = b2 And 0x0f
b2 = b2 + 0x30

b3 = ShiftRight(value, 4)
b3 = b3 And 0x0f
b3 = b3 + 0x30

b4 = ShiftRight(value, 0)
b4 = b4 And 0x0f
b4 = b4 + 0x30

It's not quite right - any ideas?

Regards - Paul
 
you have the right idea but you just left the most important things out such as platform, name and version of programming language...

it looks like some mix of basic and C syntax.

for example in VB.NET, your example would look like

Code:
        Dim value As UInt16
        Dim b1 As Byte
        Dim b2 As Byte
        Dim b3 As Byte
        Dim b4 As Byte

        value = &H6F12

        b1 = (value >> 12) And &HF
        b2 = (value >> 8) And &HF
        b3 = (value >> 4) And &HF
        b4 = (value >> 0) And &HF
 
Last edited:
Hi. Thanks for the reply.

It's written in OSHON pic basic.

Having slept on it I think this will work

Code:
Dim value As Word
Dim b1 As Byte
Dim b2 As Byte
Dim b3 As Byte
Dim b4 As Byte



value = 0xabcd

b1 = ShiftRight(value, 12)
b1 = b1 And 0x0f
If b1 > 9 Then
b1 = b1 + 0x37
Endif

b2 = ShiftRight(value, 8)
b2 = b2 And 0x0f
If b2 > 9 Then
b2 = b2 + 0x37
Endif


b3 = ShiftRight(value, 4)
b3 = b3 And 0x0f
If b3 > 9 Then
b3 = b3 + 0x37
Endif

b4 = ShiftRight(value, 0)
b4 = b4 And 0x0f
If b4 > 9 Then
b4 = b4 + 0x37
Endif

As b1 b2 b3 and b4 can only contain the values 0 to F once the shift is done.
It's not pretty and i'll convert it to a function later but it seems to work. - It doesn't take into account that the letters will always be uppercase. I'm not sure that matters though.

Regards - Paul
 
something like this
Code:
Dim value As Word
Dim n0 As Byte
Dim n1 As Byte
Dim n2 As Byte
Dim n3 As Byte

AllDigital

value = 0x6f12
Call word2nibbles(value)
n3 = nibble2ascii(n3)
n2 = nibble2ascii(n2)
n1 = nibble2ascii(n1)
n0 = nibble2ascii(n0)

End                                               

Proc word2nibbles(word1 As Word)
	Dim temp1 As Word
	temp1 = word1 And 0x000f
	n0 = temp1.LB
	word1 = word1 / 16
	temp1 = word1 And 0x000f
	n1 = temp1.LB
	word1 = word1 / 16
	temp1 = word1 And 0x000f
	n2 = temp1.LB
	word1 = word1 / 16
	temp1 = word1 And 0x000f
	n3 = temp1.LB
	word1 = word1 / 16
End Proc                                          

Function nibble2ascii(byte1 As Byte) As Byte
	nibble2ascii = LookUp("0123456789ABCDEF"), byte1
End Function
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top