Click here to Skip to main content
15,878,852 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
GeneralRe: I used a pointer in C#! Pin
pkfox11-May-21 23:34
professionalpkfox11-May-21 23:34 
GeneralRe: I used a pointer in C#! Pin
OriginalGriff11-May-21 23:53
mveOriginalGriff11-May-21 23:53 
GeneralRe: I used a pointer in C#! Pin
Richard MacCutchan11-May-21 23:49
mveRichard MacCutchan11-May-21 23:49 
GeneralRe: I used a pointer in C#! Pin
Marc Clifton12-May-21 2:49
mvaMarc Clifton12-May-21 2:49 
GeneralRe: I used a pointer in C#! Pin
Greg Utas12-May-21 1:10
professionalGreg Utas12-May-21 1:10 
GeneralRe: I used a pointer in C#! Pin
OriginalGriff12-May-21 2:08
mveOriginalGriff12-May-21 2:08 
GeneralRe: I used a pointer in C#! Pin
Greg Utas12-May-21 2:45
professionalGreg Utas12-May-21 2:45 
GeneralRe: I used a pointer in C#! Pin
Richard Deeming18-May-21 4:37
mveRichard Deeming18-May-21 4:37 
They can be quite useful - for example, if you want to get the bytes out of a SecureString[^] without going via a string. Smile | :)
C#
public static byte[] GetBytes(this Encoding encoding, SecureString value)
{
    if (encoding is null) throw new ArgumentNullException(nameof(encoding));
    if (value is null || value.Length == 0) return Array.Empty<byte>();

    IntPtr valuePtr = IntPtr.Zero;
    try
    {
        valuePtr = Marshal.SecureStringToGlobalAllocUnicode(value);
        if (valuePtr == IntPtr.Zero) return Array.Empty<byte>();

        unsafe
        {
            char* chars = (char*)valuePtr;
            int byteCount = encoding.GetByteCount(chars, value.Length);

            var result = new byte[byteCount];
            fixed (byte* bytes = result)
            {
                encoding.GetBytes(chars, value.Length, bytes, byteCount);
            }

            return result;
        }
    }
    finally
    {
        if (valuePtr != IntPtr.Zero)
        {
            Marshal.ZeroFreeGlobalAllocUnicode(valuePtr);
        }
    }
}




"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer

GeneralWSO CCC OTD 12-May-2021 - SOLUTION Pin
DerekT-P11-May-21 22:04
professionalDerekT-P11-May-21 22:04 
GeneralRe: WSO CCC OTD 12-May-2021 Pin
pkfox11-May-21 23:09
professionalpkfox11-May-21 23:09 
GeneralRe: WSO CCC OTD 12-May-2021 Pin
OriginalGriff11-May-21 23:26
mveOriginalGriff11-May-21 23:26 
GeneralRe: WSO CCC OTD 12-May-2021 Pin
DerekT-P11-May-21 23:28
professionalDerekT-P11-May-21 23:28 
GeneralRe: WSO CCC OTD 12-May-2021 We surrender! Pin
OriginalGriff12-May-21 2:10
mveOriginalGriff12-May-21 2:10 
GeneralRe: WSO CCC OTD 12-May-2021 We surrender! Pin
pkfox12-May-21 2:38
professionalpkfox12-May-21 2:38 
GeneralRe: WSO CCC OTD 12-May-2021 We surrender! Pin
Greg Utas12-May-21 2:55
professionalGreg Utas12-May-21 2:55 
GeneralRe: WSO CCC OTD 12-May-2021 We surrender! Pin
OriginalGriff12-May-21 3:54
mveOriginalGriff12-May-21 3:54 
GeneralRe: WSO CCC OTD 12-May-2021 We surrender! Pin
DerekT-P12-May-21 5:55
professionalDerekT-P12-May-21 5:55 
GeneralRe: WSO CCC OTD 12-May-2021 - SOLUTION Pin
Greg Utas12-May-21 6:25
professionalGreg Utas12-May-21 6:25 
JokeBill & Melinda Pin
Greg Utas11-May-21 6:57
professionalGreg Utas11-May-21 6:57 
GeneralRe: Bill & Melinda Pin
RickZeeland11-May-21 7:20
mveRickZeeland11-May-21 7:20 
PraiseRe: Bill & Melinda Pin
Eddy Vluggen11-May-21 7:51
professionalEddy Vluggen11-May-21 7:51 
GeneralRe: Bill & Melinda PinPopular
Daniel Pfeffer11-May-21 8:22
professionalDaniel Pfeffer11-May-21 8:22 
GeneralRe: Bill & Melinda Pin
dandy7211-May-21 9:06
dandy7211-May-21 9:06 
GeneralRe: Bill & Melinda Pin
Slacker00711-May-21 8:42
professionalSlacker00711-May-21 8:42 
GeneralRe: Bill & Melinda Pin
DRHuff11-May-21 9:17
DRHuff11-May-21 9:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.