Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 860641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T08:50:47+00:00 2026-05-15T08:50:47+00:00

I have a char* name which is a string representation of the short I

  • 0

I have a char* name which is a string representation of the short I want, such as “15” and need to output this as unsigned short unitId to a binary file. This cast must also be cross-platform compatible.

Is this the correct cast: unitId = unsigned short(temp);

Please note that I am at an beginner level in understanding binary.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T08:50:47+00:00Added an answer on May 15, 2026 at 8:50 am

    I assume that your char* name contains a string representation of the short that you want, i.e. "15".

    Do not cast a char* directly to a non-pointer type. Casts in C don’t actually change the data at all (with a few exceptions)–they just inform the compiler that you want to treat one type into another type. If you cast a char* to an unsigned short, you’ll be taking the value of the pointer (which has nothing to do with the contents), chopping off everything that doesn’t fit into a short, and then throwing away the rest. This is absolutely not what you want.

    Instead use the std::strtoul function, which parses a string and gives you back the equivalent number:

    unsigned short number = (unsigned short) strtoul(name, NULL, 0);
    

    (You still need to use a cast, because strtoul returns an unsigned long. This cast is between two different integer types, however, and so is valid. The worst that can happen is that the number inside name is too big to fit into a short–a situation that you can check for elsewhere.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code struct Student { char name[48]; float grade; int marks[10,5]; char
I have a function using strtok like this void f1(char *name) { ... char
I have this function sentanceParse with a string input which returns a list. The
I have a struct defined as: struct { char name[32]; int size; int start;
I have a structure typedef struct store { char name[11]; int age; } store;
I have recently made changes in some code that makes a char name field
I have two tables in a MySql DB: items id (char) name (varchar) description
I have these strings: int name const int level const char * const format
I currently have the dynamic array: char *myData[500][10]; //myData is the name of an
#include stdafx.h #include string.h #include windows.h bool SCS_GetAgentInfo(char name[32],char version[32], char description[256], const char

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.