Thread: String characters to int.

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    53

    String characters to int.

    Hello,

    I'm trying to get the int value of each character in a string and then add them all together so I can do a 1's complement of the total value. I'm trying to do simple checkum kinda of thing for verification of data.

    For example:


    string DPacket = "Hello World!";

    I would like to have each character added and do the ones complement.
    Will it be easier to convert first to int and then add or any other easier way?

    so my result should be the decimal value addition of each character and then do the ones complement to that.

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    what it comes to mind for me is the ASCII codes of the every char.This is what you say?

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Quote Originally Posted by drkidd22 View Post
    Hello,

    I'm trying to get the int value of each character in a string and then add them all together so I can do a 1's complement of the total value. I'm trying to do simple checkum kinda of thing for verification of data.

    For example:


    string DPacket = "Hello World!";

    I would like to have each character added and do the ones complement.
    Will it be easier to convert first to int and then add or any other easier way?

    so my result should be the decimal value addition of each character and then do the ones complement to that.
    How many bits do you plan to use as the checksum? Are your strings primarily in the Latin character set? How long are you anticipating the strings will be on average? For that matter - what is the purpose of the checksum; are you anticipating single-bit errors in transmission, or are you trying to solve some other problem?
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Code:
    int sum = ASCIIEncoding.ASCII.GetBytes("This is a string!").Select(b => (int)b).Sum();
    Console.WriteLine(sum);
    Gives me: 1517
    If you understand what you're doing, you're not learning anything.

  5. #5
    Registered User
    Join Date
    Nov 2009
    Posts
    53
    Quote Originally Posted by itsme86 View Post
    Code:
    int sum = ASCIIEncoding.ASCII.GetBytes("This is a string!").Select(b => (int)b).Sum();
    Console.WriteLine(sum);
    Gives me: 1517
    This worked great thanks.
    Now I have a string called dataPacket that I send over serial to another device. It must be sent as a string as shown below.


    Code:
    int sum = ASCIIEncoding.ASCII.GetBytes("This is a string!").Select(b => (int)b).Sum();
    int checkSum = ~sum; //Do the 1's complement
    
    string dataPacket = (DP1 + DP2 + DP3 + DP4 + DP5 + checkSum); //data to be sent out.
    but since checkSum is of type int I can't send it over and get an error. I've tried converting checkSum to a string using string s = Convert.ToString(checkSum); but get the wrong data sent over. It just converted the checkSum to a string "-1518" which is incorrect. It should've sent over the hex value FA 12

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    You can convert it to hex like this:
    Code:
    string hexString = checkSum.ToString("X");
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Characters & String
    By keisha in forum C Programming
    Replies: 12
    Last Post: 07-15-2011, 11:22 PM
  2. Replies: 11
    Last Post: 06-16-2011, 11:59 AM
  3. Replies: 5
    Last Post: 05-09-2010, 10:58 AM
  4. getting characters from a string that...
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 06-05-2002, 03:13 PM
  5. Concatenating: characters->string->vector (string) :: C++
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 02-02-2002, 01:14 PM