Wrox Programmer Forums Int to Char and Char to Int Function
 | Search | Today's Posts | Mark Forums Read
 Excel VBA Discuss using VBA for Excel programming.
 Welcome to the p2p.wrox.com Forums. You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
October 8th, 2012, 02:10 PM
Authorized User
 Points: 43, Level: 1
 Activity: 0%

Join Date: Sep 2012
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Int to Char and Char to Int Function

I am trying to convert an int to a three character string. I need to convert 1 into "001."

The built in INT() and CHAR() functions do not work.

I have created a workaround by using switch statements which works in the short term but is not an elegant solution.

Code:
```Function Int_to_Char(temp As Integer) As String
Select Case temp
Case 1
Int_to_Char = "001"
Case 2
Int_to_Char = "002"
Case 3
Int_to_Char = "003"
...```
And:

Code:
```Function Char_to_Int(temp As String) As Integer
Select Case temp
Case "001"
Char_to_Int = 1
Case "002"
Char_to_Int = 2
Case "003"
...```
Can anyone show me a quick way to do the above conversion?
December 20th, 2012, 07:27 PM
Friend of Wrox
 Points: 513, Level: 8
 Activity: 0%

Join Date: Feb 2007
Location: Davenport, IA, USA.
Posts: 163
Thanks: 0
Thanked 2 Times in 2 Posts

To 0 pad a number just do something like this within your code:

Code:
```'The following line makes sure integer is 3 digits long and places it in a string
Dim sMyString As String
sMyString = Right("00" & iTemp, 3)
MsgBox sMyString```
To return a number is a very simple number
Code:
```'The following line grabs integer value of a string
Dim iMyInt As Long
iMyInt = Val("001")
MsgBox iMyInt```

 Similar Threads Thread Thread Starter Forum Replies Last Post Book pg 56 - char vs. signed char kkinderen BOOK: Ivor Horton's Beginning Visual C++ 2010 14 July 13th, 2012 12:28 AM Big challenge here! How to convert char* to char^? samiswt Visual C++ 2005 1 November 30th, 2007 09:09 PM Convert a char to int!!!!?? vtvb C++ Programming 6 March 18th, 2006 05:51 AM Want to reiterate on my char to int problem CNewbie C++ Programming 0 April 2nd, 2004 12:47 AM convert char to int does not work yasaman C# 1 August 6th, 2003 10:39 AM