m2oTech.com

  • Increase font size
  • Default font size
  • Decrease font size
What I Learned Today

Don't forget \n

Print PDF

Ever used a simple printf statement to debug a C program without a proper debugger? If so you may have noticed some strange behavior if you forgot to include a carriage return (\n) in that statement.

While debugging a Segmentation fault I pasted following statement in a few places in the program, to see where the violation occurred:

printf("kkkkk");

To my surprise I saw no "kkkkk" not either one, even though I made sure I included one of it as control all the way up the program. It turns out I needed to modify that line into

printf("\nkkkkk");

that is, adding a "\n".

The reason for this is that C does not flush to stdout (i.e. does not display printf and other messages) until a carriage return is encountered. By not adding it in the statement we incurred in the Segmentation fault and the program terminated before messages could print out.

Of course there are ways to force flushing without adding a newline.

 

Make your keyboard child-proof

Print PDF

At times my baby daughter sits on my lap and "watches" me working. I've learned to switch to reading tasks or casual surfing during these times, as I cannot really type anything in this situation. Still it doesn't take long until she reaches for the keyboard.

So I was looking for a way to temporarily disable said keyboard (not permanently by disabling the driver as some guides suggest, i.e. when trying to permanently disable a built-in laptop keyboard).

I found this tool does the job in Windows (to lock Ctrl+Alt+L, to unlock type "unlock").

Will have to look for a Linux way...

 

Windows XP East Asian Languages without CD

Print PDF

After surviving more than a year on my work computer where East Asian Languages are not by default installed (and thus I only see squares instead of e.g. Chinese words) I decided to do something about it. In Control Panel > Regional and Language Options the solution is just a tickbox away, the problem is that the Windows XP Installation Disk is usually needed to get the appropriate files. Unless these files are already save on the PC (usually in C:\I386 or similar). The obvious solution would have been bringing a Windows XP CD from home, which I regularly forgot... This page then did it.

 

Last Updated on Friday, 08 February 2013 21:51
 

A macro to un-mess Excel comments

Print PDF

Ever experienced having tens of comments in Excel and finding they are all over the place (e.g. because you resized/added/removed cells)? I was quite sure there must be a better way and someone already did a macro to "fix" this... so here it is, I found this originally posted at this blog and allowed myself to copy and paste it (if you are the author and you want credits please let me know!).

All you need to do is:

  1. Add a macro to your spreadsheet (not "record a macro")
  2. In the VBA editor that opens paste the following code
  3. Optionally create a shortcut to the macro so that you can invoke it easily (else can run it from menu when needed)

This macro will place all of your comments back to where they belong (next to the cell they refer to).

Note: If you are trying to find a way to move comments on the sheet where you would like them to be... that's a different story. It does not seem to be possible!

Sub CommentFix()

' This macro modifies all comments in all open workbooks to:
' (1) move and size with cells
' (2) be physically positioned near the cell to which they correspond
' (3) be optimally sized appropriate to the text within
' This macro was created by combining code posted on the following website:
' http://www.contextures.com/xlcomments03.html

Dim thisfile As Workbook
Set thisfile = ActiveWorkbook
Dim MyWorkbook As Workbook
Dim MySheet As Worksheet
Dim MyComment As Comment
Dim CommentCount As Long
Dim lArea As Long
Dim fixed As Boolean
fixed = False
On Error GoTo NeedToUnprotect
For Each MyWorkbook In Workbooks
MyWorkbook.Activate
For Each MySheet In MyWorkbook.Sheets
MySheet.Activate
CommentCount = 0
For Each MyComment In MySheet.Comments
With MyComment.Shape
.Placement = xlMoveAndSize
.Top = MyComment.Parent.Top + 5
.Left = MyComment.Parent.Offset(0, 1).Left + 5
.TextFrame.Characters.Font.Name = "Tahoma"
.TextFrame.Characters.Font.Size = 8
.TextFrame.AutoSize = True
CommentCount = CommentCount + 1
End With
If MyComment.Shape.Width > 300 Then
lArea = MyComment.Shape.Width * MyComment.Shape.Height
MyComment.Shape.Width = 200
MyComment.Shape.Height = (lArea / 200) * 1.1
End If
Next MyComment
If CommentCount > 0 Then
MsgBox ("A total of " & CommentCount & " comments in worksheet '" & MySheet.Name & "' of workbook '" & MyWorkbook.Name & "'" & Chr(13) & "were repositioned and resized.")
fixed = True
End If
Next MySheet
Next MyWorkbook
thisfile.Activate
If fixed = False Then
MsgBox ("No comments were detected.")
End If
On Error GoTo 0
Exit Sub

NeedToUnprotect:
MsgBox ("You must unprotect all worksheets before running the macro.")
thisfile.Activate
Exit Sub

End Sub

 

 

BCD - A different kind of binaries

Print PDF

Decimal "08" gets coded in binary as "0000 1000", that's because the hexadecimal representation of the number is also "08".

but

Decimal "39" does not get coded as "0011 1001", because its hexadecimal representation is "27" (thus binary "0010 0111").

There is however a coding system called BCD (Binary Coded Decimal) which would code "39" exactly as "0011 1001". In other words BCD does with decimals what you'd normally do with hexadecimals, i.e. take each digit and "translate" it to a half-a-byte (a nibble) binary number. As only values from 0 to 9 can be represented by a decimal digit, only binary values from 0000 to 1001 can be "translated" (1010 to 1111 remain unused).

While this is mostly a relict of "old times" there are still areas where BCD is used. A good example are digital clocks, since some chips translate directly binary digits into 0-9 digits, without having to worry about characters like A-F, which would be present if translating to hexadecimals. Or, and that is how I learned about the existence of BCD, in some digital controls for lighting devices (DALI standard).

This topic on Wikipedia.

 
  • «
  •  Start 
  •  Prev 
  •  1 
  •  2 
  •  3 
  •  Next 
  •  End 
  • »


Page 1 of 3