CodeGuru Forums -
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic Newsletters VB Forums Developer.com


Newest CodeGuru.com Articles:

  • Deploying Windows Server 2008 with System Center
  • Remote Desktop Protocol Performance Improvements in Windows Server 2008 R2 and Windows 7
  • The Microsoft Dynamics CRM Security Model
  • SQL Server Modeling Services with Microsoft Visual Studio 2010 Beta 2

  • Search CodeGuru:
     



    Go Back   CodeGuru Forums > Visual C++ & C++ Programming > C++ (Non Visual C++ Issues)
    FAQ Members List Calendar Search Today's Posts Mark Forums Read

    C++ (Non Visual C++ Issues) Ask or answer C and C++ questions not related to Visual C++. This includes Console programming, Linux programming, or general ANSI C++.

    Reply
     
    Thread Tools Search this Thread Rate Thread Display Modes
      #1    
    Old November 6th, 2009, 03:28 PM
    twhitma1 twhitma1 is offline
    Junior Member
     
    Join Date: Nov 2009
    Posts: 2
    twhitma1 is an unknown quantity at this point (<10)
    Convert String to unsigned char

    Hi Everyone,
    This should be pretty simple but I'm just not getting it. I am trying to bring a string variable into a method, then assign that String variable into a unsigned char but I'm not sure how to convert it. Here is the code: unsigned char szStr[25] = dbServer;

    The value of dbServer is 12.12.12.12 and when I try:

    unsigned char szStr[25] = "12.12.12.12";
    it works. Any thoughts? Thanks in advance!
    Reply With Quote
      #2    
    Old November 6th, 2009, 03:48 PM
    twhitma1 twhitma1 is offline
    Junior Member
     
    Join Date: Nov 2009
    Posts: 2
    twhitma1 is an unknown quantity at this point (<10)
    Re: Convert String to unsigned char

    Actually, I'm thinking the best way to do this is to:
    1.) Find the string length
    2.) Loop through the string
    3.) Assign each char ripped out of the string to szStr[0], szStr[1]...szStr[n]

    This would also enable me to set the correct length of the vector, no more, no less. I will post back with my code once this works. Thanks!
    Reply With Quote
      #3    
    Old November 6th, 2009, 03:56 PM
    Lindley Lindley is online now
    Elite Member
    Power Poster
     
    Join Date: Oct 2007
    Location: Fairfax, VA
    Posts: 6,905
    Lindley is a name known to all (1000+) Lindley is a name known to all (1000+) Lindley is a name known to all (1000+) Lindley is a name known to all (1000+) Lindley is a name known to all (1000+) Lindley is a name known to all (1000+) Lindley is a name known to all (1000+) Lindley is a name known to all (1000+) Lindley is a name known to all (1000+)
    Re: Convert String to unsigned char

    Assuming the string you pass in uses char as its underlying character type, you can just reinterpret_cast the char* to an unsigned char*. Normally reinterpret_cast is frowned upon, but it wouldn't make a difference here.

    If there's any possibility that the string might be using wchar_ts as the underlying type, you can't do that.
    Reply With Quote
    Reply

    Bookmarks
    Go Back   CodeGuru Forums > Visual C++ & C++ Programming > C++ (Non Visual C++ Issues)


    Thread Tools Search this Thread
    Search this Thread:

    Advanced Search
    Display Modes Rate This Thread
    Rate This Thread:

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is On
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 02:13 PM.



    Acceptable Use Policy


    The Network for Technology Professionals

    Search:

    About Internet.com

    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers


    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.